├── .gitignore ├── Main.java ├── algorithm-engagements ├── algorithm-engagements-2006 │ ├── lii.cpp │ └── wie.cpp └── algorithm-engagements-2012 │ └── obr.cpp ├── bzoj ├── p1036.cpp ├── p2757.cpp ├── p3293.cpp └── p3294.cpp ├── codechef ├── APRIL12 │ ├── DOUBLE.cpp │ ├── DUMPLING │ │ └── Main.java │ ├── PANSTACK.cpp │ ├── PARALLEL.cpp │ └── PLAYFIT │ │ └── Main.java ├── JULY12 │ ├── CSUMD.cpp │ ├── DREAM.cpp │ ├── GRAYSC.cpp │ └── SAD.cpp └── OCT12 │ └── DIAMOND │ └── Main.java ├── codeforces ├── 101-codeforces-beta-round-79 │ └── E.cpp ├── 104-codeforces-beta-round-80 │ └── A.rb ├── 109-codeforces-beta-round-84 │ ├── C.cpp │ └── D.cpp ├── 110-codeforces-beta-round-84 │ └── A.py ├── 111-codeforces-round-85 │ └── D.cpp ├── 112-codeforces-beta-round-85 │ └── A.py ├── 113-codeforces-beta-round-86 │ └── B.cpp ├── 114-codeforces-beta-round-86 │ └── A.py ├── 116-codeforces-beta-round-87 │ ├── A.py │ └── D │ │ └── Solution.java ├── 117-codeforces-beta-round-88 │ └── D.cpp ├── 118-codeforces-beta-round-89 │ ├── A.py │ └── B.py ├── 119-codeforces-beta-round-90 │ └── A.py ├── 121-codeforces-beta-round-91 │ └── D │ │ └── Solution.java ├── 122-codeforces-beta-round-91 │ └── A.py ├── 124-codeforces-beta-round-92 │ └── A.py ├── 125-codeforces-testing-round-2 │ └── D.cpp ├── 126-codeforces-beta-round-93 │ └── D.cpp ├── 127-codeforces-beta-round-93 │ ├── A.cpp │ └── B │ │ └── Main.java ├── 129-codeforces-beta-round-94 │ ├── A.hs │ └── D.cpp ├── 13-codeforces-beta-round-13 │ └── B.cpp ├── 131-codeforces-beta-round-95 │ ├── A.py │ ├── B.py │ └── C.hs ├── 133-codeforces-beta-round-96 │ └── A.py ├── 136-codeforces-beta-round-97 │ └── A.py ├── 139-codeforces-beta-round-99 │ └── A.py ├── 14-codeforces-beta-round-14 │ └── C.cpp ├── 140-codeforces-round-100 │ ├── E.cpp │ └── F.cpp ├── 141-codeforces-round-101 │ └── D.cpp ├── 144-codeforces-round-103 │ └── A.py ├── 146-codeforces-round-104 │ └── A.py ├── 147-codeforces-testing-round-4 │ └── B │ │ └── Main.java ├── 149-codeforces-round-106 │ ├── A.py │ └── E │ │ └── Main.java ├── 15-codeforces-beta-round-15 │ ├── A.cpp │ └── B.cpp ├── 151-codeforces-round-107 │ └── A.py ├── 152-codeforces-round-108 │ ├── A.cpp │ └── C.hs ├── 155-codeforces-round-109 │ └── A.py ├── 156-codeforces-round-110 │ └── C.cpp ├── 157-codeforces-round-110 │ ├── A.rb │ └── D.cpp ├── 158-vk-cup-qualification-round-1 │ └── B.py ├── 16-codeforces-beta-round-16 │ ├── A.hs │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── 160-codeforces-round-111 │ ├── A.py │ ├── B.hs │ └── D.cpp ├── 161-vk-cup-2012-round-1 │ ├── C.cpp │ └── D.cpp ├── 163-vk-cup-2012-round-2 │ ├── A.cpp │ └── E.cpp ├── 165-codeforces-round-112 │ └── A.py ├── 168-codeforces-round-114 │ └── A.rb ├── 176-croc-champ-2012-round-2 │ └── E.cpp ├── 177-abbyy-cup-2-easy │ └── E.rb ├── 178-abbyy-20-hard │ ├── A.cpp │ └── B.cpp ├── 18-codeforces-beta-round-18 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D │ │ └── Solution.java ├── 180-codeforces-round-116 │ └── F.rb ├── 182-codeforces-round-117 │ └── A.cpp ├── 183-croc-champ-2012-final │ ├── A │ │ └── Solution.java │ ├── B │ │ └── Main.java │ ├── C.cpp │ └── D │ │ └── Solution.java ├── 185-codeforces-round-118 │ ├── D.cpp │ └── E.cpp ├── 187-codeforces-round-119 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── 19-codeforces-beta-round-19 │ ├── A.rb │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── 190-codeforces-round-120 │ ├── D.cpp │ └── E.cpp ├── 191-codeforces-round-121 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── E.cpp ├── 193-codeforces-round-122 │ ├── C.cpp │ └── E │ │ └── Solution.java ├── 196-codeforces-round-124 │ └── C.cpp ├── 197-codeforces-round-124 │ └── C.rb ├── 20-codeforces-alpha-round-20 │ ├── A.rb │ └── B.cpp ├── 200-codeforces-round-126 │ └── B.rb ├── 204-codeforces-round-129 │ └── E.cpp ├── 208-codeforces-round-130 │ ├── A.py │ └── E.cpp ├── 209-vk-cup-2012-finals-pratice-session │ ├── A.cpp │ └── C.cpp ├── 213-codeforces-round-131 │ └── E.cpp ├── 214-codeforces-round-131 │ ├── A.hs │ └── E.cpp ├── 215-codeforces-round-132 │ ├── A.py │ └── E │ │ └── Solution.java ├── 216-codeforces-round-133 │ └── E.cpp ├── 217-codeforces-round-134 │ └── A.cpp ├── 219-codeforces-round-135 │ ├── A.cpp │ ├── B.py │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 22-codeforces-beta-round-22 │ ├── B.cpp │ └── D.cpp ├── 220-codeforces-round-136 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 221-codeforces-round-136 │ ├── A.py │ └── B.py ├── 222-codeforces-round-137 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 223-codeforces-round-138 │ ├── A.cpp │ ├── B.cpp │ └── C.cpp ├── 224-codeforces-round-138 │ ├── A.py │ └── B.cpp ├── 225-codeforces-round-139 │ ├── A.py │ ├── B.py │ ├── C.cpp │ ├── D.cpp │ └── E.py ├── 226-codeforces-round-140 │ ├── A.py │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 227-codeforces-round-140 │ ├── A.py │ └── B.py ├── 228-codeforces-round-141 │ ├── A.py │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ ├── D │ │ └── Main.java │ └── E │ │ └── Main.java ├── 229-codeforces-round-142 │ ├── D.cpp │ └── E.cpp ├── 230-codeforces-round-142 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 231-codeforces-round-143 │ ├── A.hs │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ ├── D.py │ └── E.cpp ├── 232-codeforces-round-144 │ ├── A.cpp │ ├── B.cpp │ ├── D.cpp │ └── E.cpp ├── 233-codeforces-round-144 │ ├── A.py │ ├── B.cpp │ └── E.cpp ├── 234-codeforces-round-145 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── E.cpp ├── 235-codeforces-round-146 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── E.cpp ├── 236-codeforces-round-146 │ ├── A.rb │ └── B.cpp ├── 237-codeforces-round-147 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 238-codeforces-round-148 │ ├── A.cpp │ └── C │ │ └── Main.java ├── 24-codeforces-beta-round-24 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 240-codeforces-round-145 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ └── F.cpp ├── 241-bayan-2012-2013-elimination-round │ └── A.rb ├── 242-codeforces-round-149 │ ├── A.rb │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 243-codeforces-round-150 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── 246-codeforces-round-151 │ ├── A.hs │ └── C.cpp ├── 248-codeforces-round-152 │ └── B.cpp ├── 25-codeforces-beta-round-25 │ ├── C.cpp │ └── D.cpp ├── 251-codeforces-round-153 │ └── A.cpp ├── 253-codeforces-round-154 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 254-codeforces-round-155 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 255-codeforces-round-156 │ ├── A.rb │ └── B.rb ├── 256-codeforces-round-156 │ ├── A.cpp │ ├── B.rb │ ├── C │ │ └── Main.java │ ├── D.cpp │ ├── D.rb │ └── E.cpp ├── 258-codeforces-round-157 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 259-codeforces-round-157 │ ├── A.rb │ └── B.cpp ├── 26-codeforces-beta-round-26 │ ├── C.cpp │ ├── D.hs │ └── E.cpp ├── 260-codeforces-round-158 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 265-codeforces-round-162 │ └── D │ │ └── Solution.java ├── 266-codeforces-round-163 │ ├── A.hs │ └── E │ │ └── Solution.java ├── 269-codeforces-round-165 │ └── C.cpp ├── 27-codeforces-beta-round-27 │ ├── B.cpp │ └── C.cpp ├── 271-codeforces-round-166 │ ├── A.rb │ ├── B.cpp │ ├── C.cpp │ └── E.cpp ├── 273-codeforces-round-167 │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ ├── D │ │ └── Main.java │ └── E.cpp ├── 274-codeforces-round-168 │ └── E.cpp ├── 275-codeforces-round-168 │ └── A.py ├── 276-codeforces-round-169 │ ├── A.rb │ ├── B.rb │ ├── C │ │ └── Main.java │ └── D.rb ├── 28-codeforces-beta-round-28 │ ├── A.cpp │ ├── B.cpp │ └── D.cpp ├── 282-codeforces-round-173 │ ├── A.rb │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 283-codeforces-round-174 │ ├── A │ │ └── Main.java │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ ├── D │ │ └── Main.java │ └── E.cpp ├── 285-codeforces-round-175 │ ├── D.hs │ └── E │ │ └── Solution.java ├── 286-codeforces-round-176 │ ├── A │ │ └── Solution.java │ ├── B │ │ └── Solution.java │ ├── C │ │ └── Solution.java │ ├── D.cpp │ └── E.cpp ├── 288-codeforces-round-177 │ ├── C.cpp │ └── D │ │ └── Solution.java ├── 29-codeforces-beta-round-29 │ ├── B.hs │ ├── C.cpp │ ├── D.py │ └── E.cpp ├── 293-croc-champ-2013-round-2 │ ├── D │ │ └── Solution.java │ └── E.cpp ├── 294-codeforces-round-178 │ ├── D.cpp │ └── E.cpp ├── 297-codeforces-round-180 │ └── D.cpp ├── 299-croc-champ-2013-round-2 │ └── A.rb ├── 30-codeforces-beta-round-30 │ ├── A.py │ ├── B.py │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 300-codeforces-round-181 │ ├── A.hs │ ├── C.cpp │ └── E.cpp ├── 301-codeforces-round-182 │ ├── A │ │ └── Solution.java │ ├── B │ │ └── Solution.java │ ├── C.rb │ └── D │ │ └── Solution.java ├── 304-codeforces-round-183 │ └── C.hs ├── 305-codeforces-round-184 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── 306-testing-round-6 │ ├── A.rb │ ├── B.cpp │ ├── C │ │ └── Solution.java │ └── D.cpp ├── 31-codeforces-beta-round-31 │ ├── B.py │ ├── C.cpp │ └── D.cpp ├── 313-codeforces-round-186 │ └── D │ │ └── Solution.java ├── 314-codeforces-round-187 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── 317-codeforces-round-188 │ ├── A.hs │ └── B │ │ └── Solution.java ├── 319-codeforces-round-189 │ ├── A │ │ └── Solution.java │ ├── B.cpp │ ├── C │ │ └── Solution.java │ └── D.cpp ├── 32-codeforces-beta-round-32 │ ├── A.cpp │ ├── B.hs │ ├── C.hs │ └── D.cpp ├── 325-memsql-startcup-round-1 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 33-codeforces-beta-round-33 │ ├── B.cpp │ └── C.cpp ├── 331-abbyy-cup-30-finals │ └── E.cpp ├── 338-codeforces-round-196 │ ├── A │ │ └── Solution.java │ ├── B │ │ └── Solution.java │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 34-codeforces-beta-round-34 │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 343-codeforces-round-200 │ ├── A │ │ └── Main.java │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ ├── D │ │ └── Main.java │ └── E │ │ └── Main.java ├── 348-codeforces-round-202 │ ├── A │ │ └── Main.java │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ └── D │ │ └── Main.java ├── 360-codeforces-round-210 │ ├── A │ │ └── Main.java │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ └── E │ │ └── Main.java ├── 364-codeforces-round-213 │ └── E.cpp ├── 37-codeforces-beta-round-37 │ └── C.py ├── 375-codeforces-round-221 │ └── E.cpp ├── 377-codeforces-round-222 │ └── C.cpp ├── 39-school-team-contest-1 │ ├── A.cpp │ └── H.hs ├── 396-codeforces-round-232 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 416-codeforces-round-241 │ ├── A.java │ ├── B.java │ ├── C.java │ └── E.java ├── 418-rcc-2014-warmup │ ├── A │ │ └── Main.java │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ ├── D.cpp │ └── E │ │ └── Main.java ├── 425-codeforces-round-243 │ ├── A │ │ └── Main.java │ ├── B │ │ └── Main.java │ ├── C │ │ └── Main.java │ ├── D │ │ └── Main.java │ └── E │ │ └── Main.java ├── 429-codeforces-round-245 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 434-codeforces-round-248 │ ├── A │ │ └── Main.java │ ├── B.cpp │ ├── C │ │ └── Main.java │ ├── D │ │ └── Main.java │ └── E.cpp ├── 436-zepto-code-rush-2014 │ └── E.cpp ├── 44-school-team-contest-2 │ └── F.cpp ├── 45-school-team-contest-3 │ ├── B.cpp │ └── G.cpp ├── 452-memsql-startcup-2.0-round-1 │ └── E │ │ └── Main.java ├── 461-codeforces-round-263 │ ├── A │ │ └── Main.java │ ├── B │ │ └── Main.java │ ├── C.cpp │ ├── D.cpp │ └── E │ │ └── Main.java ├── 464-codeforces-round-265 │ └── E.cpp ├── 478-codeforces-round-273 │ └── E.cpp ├── 51-codeforces-beta-round-48 │ └── D.cpp ├── 54-codeforces-beta-round-50 │ └── E.cpp ├── 542-vk-cup-2015-round-3 │ ├── A.cpp │ ├── B.cpp │ ├── C │ │ └── Main.java │ ├── D.cpp │ ├── E.cpp │ └── F │ │ └── Main.java ├── 546-codeforces-round-304 │ └── C.hs ├── 547-codeforces-round-305 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 549-looksery-cup-2015 │ ├── A.hs │ ├── B.hs │ ├── C.hs │ ├── D.hs │ ├── F.cpp │ ├── G.hs │ └── H.hs ├── 55-codeforces-beta-round-55 │ └── A.rb ├── 550-codeforces-round-306 │ ├── A.hs │ ├── B.hs │ ├── C.hs │ └── D.hs ├── 551-codeforces-round-307 │ ├── B.hs │ └── C.cs ├── 552-codeforces-round-308 │ ├── A.hs │ ├── B.hs │ ├── C.hs │ ├── D.hs │ └── E.py ├── 555-codeforces-round-310 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 559-codeforces-round-313 │ └── E.cpp ├── 568-codeforces-round-315 │ └── E.cpp ├── 573-codeforces-round-318 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── E.cpp ├── 58-codeforces-beta-round-54 │ └── C.cpp ├── 60-codeforces-beta-round-56 │ ├── C │ │ └── Solution.java │ └── E.cpp ├── 603-codeforces-round-334 │ ├── A.hs │ ├── C.hs │ └── E.cpp ├── 623-aim-tech-round │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 66-codeforces-beta-round-61 │ └── D.rb ├── 663-codeforces-round-347 │ └── E.cpp ├── 668-codeforces-round-348 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ └── F.cpp ├── 671-codeforces-round-352 │ ├── C.cpp │ └── D.cpp ├── 674-codeforces-round-351 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ └── G.cpp ├── 679-codeforces-round-356 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 68-codeforces-beta-round-62 │ └── D.cpp ├── 685-codeforces-round-359 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 687-codeforces-round-360 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 700-codeforces-round-364 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── 704-codeforces-round-366 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── 708-aim-tech-round-3 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 71-codeforces-beta-round-65 │ └── A.py ├── 713-codeforces-round-371 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 715-codeforces-round-372 │ ├── A.py │ ├── B.cpp │ ├── C.cpp │ ├── D.py │ └── E.cpp ├── 718-codeforces-round-373 │ ├── A.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 80-codeforces-beta-round-69 │ └── A.rb ├── 81-yandex-algorithm-open-2011-qualification-1 │ └── A.cpp ├── 82-yandex-algorithm-2011-qualification-2 │ └── A.py ├── 83-codeforces-beta-round-72 │ └── D.cpp ├── 833-codeforces-round-426 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 84-codeforces-beta-round-72 │ └── A.pl ├── 840-codeforces-round-429 │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── 86-yandex-algorithm-2011-round-2 │ └── A.rb ├── 87-codeforces-beta-round-73 │ └── C.cpp ├── 908-good-bye-2017 │ ├── A.py │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ └── G.cpp ├── 91-codeforces-beta-round-75 │ ├── A.rb │ └── C.cpp └── 96-codeforces-beta-round-77 │ ├── A.hs │ └── D.cpp ├── google-code-jam ├── emea-semifinal-2008 │ └── C │ │ └── C.cpp ├── google-code-jam-2015 │ └── round-3 │ │ ├── A.cpp │ │ └── B.cpp ├── qualification-round-2012 │ ├── A │ │ ├── A.cpp │ │ ├── A.py │ │ ├── A.text │ │ └── parser.py │ ├── B │ │ ├── B.py │ │ └── Main.java │ └── C │ │ ├── C.cpp │ │ └── C.py └── round-1a-2012 │ ├── A │ └── A.py │ └── B │ └── B.cpp ├── hdoj ├── p1695.cpp ├── p3530.cpp ├── p3948.cpp ├── p4389.cpp ├── p4436.cpp ├── p4524 │ └── Main.java ├── p4625.cpp ├── p4626.cpp ├── p4668.cpp ├── p4680.cpp ├── p5470.cpp ├── p5489.cpp ├── p5756.cpp └── p5759.cpp ├── multi-university-trainings └── 2011 │ ├── contest-16 │ ├── G.cpp │ ├── H.cpp │ └── I.cpp │ └── contest-3 │ └── C.cpp ├── my-contests ├── 2012-03-13-stoi-2012 │ ├── 2012年汕头市选成绩.xlsx │ ├── junior │ │ └── problems │ │ │ └── problems.tex │ └── senior │ │ ├── makers.zip │ │ ├── makers │ │ ├── A-50.cpp │ │ ├── A.cpp │ │ ├── Agen.py │ │ ├── Atest.sh │ │ ├── B-50.cpp │ │ ├── B.cpp │ │ ├── Bgen.py │ │ ├── Btest.sh │ │ ├── C-50.cpp │ │ ├── C.cpp │ │ ├── Cgen.py │ │ ├── Ctest.sh │ │ ├── D-30.cpp │ │ ├── D.cpp │ │ ├── Dgen.py │ │ ├── Dtest.sh │ │ └── gen-all.sh │ │ └── problems │ │ └── problems.tex └── 2015-06-14-sichuan-province-contest │ ├── lib │ ├── checkers │ │ ├── acmp.cpp │ │ ├── caseicmp.cpp │ │ ├── casencmp.cpp │ │ ├── casewcmp.cpp │ │ ├── dcmp.cpp │ │ ├── fcmp.cpp │ │ ├── hcmp.cpp │ │ ├── icmp.cpp │ │ ├── lcmp.cpp │ │ ├── ncmp.cpp │ │ ├── pointscmp.cpp │ │ ├── rcmp.cpp │ │ ├── rcmp4.cpp │ │ ├── rcmp6.cpp │ │ ├── rcmp9.cpp │ │ ├── rncmp.cpp │ │ ├── uncmp.cpp │ │ ├── wcmp.cpp │ │ └── yesno.cpp │ ├── docs │ │ └── read.me │ ├── generators │ │ ├── bgen.cpp │ │ ├── igen.cpp │ │ ├── iwgen.cpp │ │ ├── multigen.cpp │ │ ├── sgen.cpp │ │ └── swgen.cpp │ ├── stress.sh │ ├── testlib.h │ ├── testlib.rb │ └── validators │ │ ├── bipartite-graph-validator.cpp │ │ ├── ival.cpp │ │ ├── nval.cpp │ │ ├── sval.cpp │ │ ├── undirected-graph-validator.cpp │ │ └── undirected-tree-validator.cpp │ ├── problems │ ├── carry │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── slow.cpp │ │ │ ├── solution.cpp │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ └── validator.cpp │ ├── censor │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── 004.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── random.exe │ │ │ ├── random_ab.cpp │ │ │ ├── random_ab.exe │ │ │ ├── random_fib.cpp │ │ │ ├── random_fib.exe │ │ │ ├── slow.cpp │ │ │ ├── solution.cpp │ │ │ ├── solution.exe │ │ │ ├── stress.sh │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ ├── validator.cpp │ │ │ └── validator.exe │ ├── cover │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── slow.cpp │ │ │ ├── solution.cpp │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ └── validator.cpp │ ├── doall.sh │ ├── easy │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── 004.hand │ │ │ ├── 005.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── solution.cpp │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ └── validator.cpp │ ├── necklace │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── slow.cpp │ │ │ ├── solution.cpp │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ ├── validator │ │ │ └── validator.cpp │ ├── party │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── 004.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── slow.cpp │ │ │ ├── solution.cpp │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ └── validator.cpp │ ├── rectangle │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── figure.png │ │ │ ├── figure.sketch │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── slow.py │ │ │ ├── solution.cpp │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ └── validator.cpp │ ├── rmq │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── random_no.cpp │ │ │ ├── slow.cpp │ │ │ ├── solution.cpp │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ ├── validator.cpp │ │ │ └── wrong.cpp │ ├── travel │ │ ├── check.cpp │ │ └── src │ │ │ ├── 001.hand │ │ │ ├── 002.hand │ │ │ ├── 003.hand │ │ │ ├── 004.hand │ │ │ ├── doall.rb │ │ │ ├── doall.sh │ │ │ ├── problem.md │ │ │ ├── random.cpp │ │ │ ├── random_dis.cpp │ │ │ ├── slow.cpp │ │ │ ├── slow2.cpp │ │ │ ├── solution.cpp │ │ │ ├── stress.sh │ │ │ ├── testlib.h │ │ │ ├── testlib.rb │ │ │ ├── validator.cpp │ │ │ ├── wrong.cpp │ │ │ └── wrong2.cpp │ └── turn │ │ ├── check.cpp │ │ └── src │ │ ├── 001.hand │ │ ├── 002.hand │ │ ├── 003.hand │ │ ├── 004.hand │ │ ├── 005.hand │ │ ├── 006.hand │ │ ├── doall.rb │ │ ├── doall.sh │ │ ├── problem.md │ │ ├── random.cpp │ │ ├── solution.cpp │ │ ├── testlib.h │ │ ├── testlib.rb │ │ └── validator.cpp │ └── statement │ ├── doall.rb │ ├── doall.sh │ ├── figure.png │ └── template.tex ├── ontak └── ontak-2010 │ ├── kla.cpp │ ├── pod.cpp │ ├── szc.cpp │ ├── wie.cpp │ └── wyd.cpp ├── pe.h ├── poi ├── poi-x │ ├── aut.cpp │ ├── cia.cpp │ ├── cze.cpp │ ├── lic.cpp │ ├── mal.cpp │ ├── mas.cpp │ ├── ply.cpp │ ├── pol.cpp │ ├── prz.cpp │ ├── ska.cpp │ ├── sum.cpp │ ├── tas.cpp │ └── tro.cpp └── poi-xi │ ├── szn.cpp │ └── tur.cpp ├── poj ├── p1753.cpp ├── p2186.cpp ├── p2378.cpp ├── p2761.cpp ├── p2923.cpp ├── p2942.cpp ├── p3155.cpp └── p3468.cpp ├── project-euler ├── p103.cpp ├── p104.py ├── p105.py ├── p106.cpp ├── p107.py ├── p108.py ├── p109.py ├── p110.py ├── p111.cpp ├── p112.py ├── p113.py ├── p114.py ├── p116.py ├── p117.py ├── p118.cpp ├── p119.py ├── p120.py ├── p121.py ├── p122.cpp ├── p123.cpp ├── p124.cpp ├── p125.cpp ├── p126.cpp ├── p127.cpp ├── p128.cpp ├── p131.cpp ├── p132.py ├── p133.cpp ├── p134.cpp ├── p135.cpp ├── p136.cpp ├── p137.py ├── p138.py ├── p139.cpp ├── p140.py ├── p141.cpp ├── p142.cpp ├── p143.cpp ├── p144.cpp ├── p145.cpp ├── p146.cpp ├── p147.py ├── p148.py ├── p149.py ├── p150.cpp ├── p151.py ├── p152.py ├── p153.cpp ├── p154.cpp ├── p154.py ├── p155.cpp ├── p156.cpp ├── p157.py ├── p158.py ├── p159.cpp ├── p160.py ├── p161.cpp ├── p162.py ├── p163.cpp ├── p164.py ├── p166.cpp ├── p167.cpp ├── p168.py ├── p169.py ├── p170.cpp ├── p171.py ├── p173.py ├── p174.cpp ├── p175.py ├── p176.py ├── p177.cpp ├── p178.cpp ├── p179.cpp ├── p180.py ├── p181 │ └── Main.java ├── p182.py ├── p183.py ├── p184.cpp ├── p185.cpp ├── p186.py ├── p187.cpp ├── p188.py ├── p189.cpp ├── p190.py ├── p191.py ├── p192.py ├── p193.cpp ├── p194.cpp ├── p195.py ├── p196.cpp ├── p197.py ├── p198.cpp ├── p199.py ├── p200.cpp ├── p201.cpp ├── p202.py ├── p203.py ├── p204.py ├── p205.py ├── p206.py ├── p207.py ├── p208.py ├── p209.py ├── p210.cpp ├── p211.cpp ├── p212.cpp ├── p213.cpp ├── p214.cpp ├── p215.cpp ├── p216.cpp ├── p217.py ├── p219.py ├── p220.py ├── p225.cpp ├── p227.sage ├── p228.py ├── p229.cpp ├── p230.py ├── p231.cpp ├── p232.cpp ├── p234.cpp ├── p235.py ├── p237.cpp ├── p238.cpp ├── p239.py ├── p240.py ├── p242.py ├── p250.cpp ├── p258.cpp ├── p269 │ └── p269.cpp ├── p282.cpp ├── p296.cpp ├── p300 │ └── p300.cpp ├── p301 │ └── p301.hs ├── p303 │ └── p303.py ├── p306 │ └── p306.cpp ├── p309.cpp ├── p310 │ └── Main.java ├── p318 │ └── p318.py ├── p320 │ └── p320.cpp ├── p326 │ └── p326.py ├── p337 │ └── p337.cpp ├── p340 │ └── p340.hs ├── p342 │ └── p342.cpp ├── p345 │ └── p345.py ├── p346.py ├── p347 │ └── p347.cpp ├── p348.cpp ├── p349.py ├── p350.cpp ├── p351 │ └── p351.py ├── p357 │ └── p357.cpp ├── p360 │ └── p360.cpp ├── p364 │ └── p364.cpp ├── p365 │ └── p365.py ├── p375.cpp ├── p377 │ └── p377.rb ├── p379 │ └── p379.cpp ├── p383 │ └── p383.cpp ├── p388 │ └── p388.py ├── p400 │ └── p400.py ├── p402 │ └── p402.py ├── p407 │ └── p407.cpp ├── p409 │ └── p409.cpp ├── p411 │ └── p411.cpp ├── p412 │ └── p412.py ├── p413 │ └── p413.py ├── p416 │ ├── p416.cpp │ └── p416.hs ├── p435 │ └── p435.hs ├── p439 │ └── p439-backup.py ├── p452 │ └── p452.cpp ├── p453 │ └── p453.cpp ├── p457.cpp ├── p458.sage ├── p468.cpp ├── p486 │ ├── step1.sage │ ├── step2.cpp │ └── step3.sage ├── p487.cpp ├── p488 │ └── p488.py ├── p490.sage ├── p491 │ └── p491.cpp ├── p495.cpp ├── p499.cpp ├── p500 │ └── p500.cpp ├── p502 │ └── p502.cpp ├── p503 │ └── p503.py ├── p509 │ └── p509.py ├── p511 │ └── p511.cpp ├── p515 │ └── Main.hs ├── p516 │ └── p516.cpp ├── p518 │ └── p518.cpp ├── p523 │ └── p523.py ├── p528.py ├── p529 │ └── p529.cpp ├── p544 │ └── p544.cpp ├── p545.cpp ├── p546 │ └── p546.py ├── p548 │ └── p548.cpp ├── p550 │ └── p550.cpp ├── p551 │ └── p551.py ├── p552.cpp ├── p553 │ └── p553.cpp ├── p554 │ └── p554.py ├── p555.py ├── p559 │ └── p559.cpp ├── p569.cpp ├── p571.cpp ├── p588.py ├── p590.cpp ├── p593.cpp ├── p596.py ├── p598.py ├── p599.sage ├── p600.py ├── p601.sage ├── p602.py ├── p603.sage ├── p604.py ├── p605.py ├── p609.cpp ├── p612.cpp └── p78 │ └── solution.cpp ├── regional ├── china-final-2016 │ └── I.cpp ├── dhaka-2012 │ └── K.cpp ├── fuzhou-2010 │ └── C-wa.cpp ├── latin-america-2012 │ └── J.cpp └── neerc-2007 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── E │ └── Main.java │ └── L.cpp ├── sgu ├── p100.cpp ├── p101.cpp ├── p102.cpp ├── p103.cpp ├── p104.cpp ├── p105.cpp ├── p106.cpp ├── p107.cpp ├── p108.cpp ├── p109.cpp ├── p110.cpp ├── p111.cpp ├── p112 │ └── Solution.java ├── p113.cpp ├── p114.cpp ├── p115.cpp ├── p116.cpp ├── p117.cpp ├── p118.cpp ├── p120.cpp ├── p121.cpp ├── p122 │ └── Solution.java ├── p123 │ └── Solution.java ├── p124.cpp ├── p125.cpp ├── p126.cpp ├── p127.cpp ├── p128.cpp ├── p129.cpp ├── p130.cpp ├── p131.cpp ├── p132.cpp ├── p133 │ └── Solution.java ├── p134.cpp ├── p135.cpp ├── p136.cpp ├── p137.cpp ├── p138.cpp ├── p139.cpp ├── p140.cpp ├── p141.cpp ├── p142.cpp ├── p143.cpp ├── p144.cpp ├── p145.cpp ├── p146 │ └── Solution.java ├── p147.cpp ├── p148.cpp ├── p149.cpp ├── p150.cpp ├── p151.cpp ├── p152.cpp ├── p153.cpp ├── p154.cpp ├── p155.cpp ├── p156.cpp ├── p157.cpp ├── p158.cpp ├── p159 │ └── Solution.java ├── p160.cpp ├── p161 │ └── Solution.java ├── p162.cpp ├── p163.cpp ├── p164.cpp ├── p165.cpp ├── p166.cpp ├── p167.cpp ├── p168.cpp ├── p169.cpp ├── p170.cpp ├── p171.cpp ├── p172.cpp ├── p173.cpp ├── p174.cpp ├── p175.cpp ├── p176.cpp ├── p177.cpp ├── p178.cpp ├── p179.cpp ├── p180.cpp ├── p181.cpp ├── p182 │ └── Solution.java ├── p183.cpp ├── p184.cpp ├── p185.cpp ├── p186.cpp ├── p187.cpp ├── p188.cpp ├── p189 │ └── Solution.java ├── p190.cpp ├── p191.cpp ├── p192 │ └── Solution.java ├── p194.cpp ├── p195.cpp ├── p196.cpp ├── p197 │ └── Solution.java ├── p198.cpp ├── p199.cpp ├── p200.cpp ├── p201 │ └── Solution.java ├── p202 │ └── Solution.java ├── p203.cpp ├── p205 │ └── Solution.java ├── p206 │ └── Solution.java ├── p207 │ └── Solution.java ├── p208.cpp ├── p209.cpp ├── p210.cpp ├── p212.cpp ├── p213.cpp ├── p214.cpp ├── p216.cpp ├── p218.cpp ├── p220 ├── p221 │ └── Solution.java ├── p222 │ └── Solution.java ├── p223.cpp ├── p224.cpp ├── p226.cpp ├── p228.cpp ├── p229.cpp ├── p230 │ └── Solution.java ├── p231.cpp ├── p239 │ └── Solution.java ├── p247 │ └── Solution.java ├── p248 │ └── Solution.java ├── p249.cpp ├── p259 │ └── Solution.java ├── p262.cpp ├── p269 │ └── Solution.java ├── p270.cpp ├── p275 │ └── Solution.java ├── p277.cpp ├── p278.cpp ├── p279.cpp ├── p280.cpp ├── p281.cpp ├── p282.cpp ├── p284 │ └── Solution.java ├── p286.cpp ├── p289.cpp ├── p294 │ └── Solution.java ├── p296 │ └── Solution.java ├── p297 │ └── Solution.java ├── p298.cpp ├── p299 │ └── Solution.java ├── p304.cpp ├── p305.cpp ├── p307.cpp ├── p309.cpp ├── p310 │ └── Solution.java ├── p311.cpp ├── p317.cpp ├── p318 │ └── Solution.java ├── p319.cpp ├── p321.cpp ├── p322.cpp ├── p323.cpp ├── p325.cpp ├── p326.cpp ├── p327 │ └── Solution.java ├── p330 │ └── Solution.java ├── p337.cpp ├── p339.cpp ├── p342 │ └── Solution.java ├── p344.cpp ├── p347.cpp ├── p350 │ └── Solution.java ├── p352.cpp ├── p354.cpp ├── p355.cpp ├── p356 │ └── Solution.java ├── p357 │ └── Solution.java ├── p358 │ └── Solution.java ├── p361.cpp ├── p362 │ └── Solution.java ├── p365 │ └── Solution.java ├── p367.cpp ├── p370.cpp ├── p372.cpp ├── p374 │ └── Solution.java ├── p375.cpp ├── p379 │ └── Solution.java ├── p381.cpp ├── p384.cpp ├── p389 │ └── Solution.java ├── p390 │ └── Solution.java ├── p397.cpp ├── p398.cpp ├── p404 │ └── Solution.java ├── p405 │ └── Solution.java ├── p406 │ └── Solution.java ├── p407 │ └── Solution.java ├── p409.cpp ├── p410.cpp ├── p411.cpp ├── p415 │ └── Solution.java ├── p443 │ └── Solution.java ├── p444.cpp ├── p455.cpp ├── p458.cpp ├── p502.cpp ├── p527.cpp ├── p529.cpp └── p536.cpp ├── soj └── p3098.cpp ├── spoj ├── COT.cpp └── GEN.cpp ├── timus ├── p1132.cpp ├── p1173.cpp ├── p1469.cpp ├── p1855.cpp └── p1978.cpp ├── topcoder ├── AkariDaisukiDiv1.java ├── AntsMeet.java ├── ArtShift.java ├── BinaryPolynomialDivOne.java ├── BlackWhiteMagic.java ├── CandidateKeys.java ├── CarrotBoxes.java ├── CirclesGame.java ├── ColorfulCards.java ├── ColorfulCookie.java ├── DrivingAround.java ├── DropCoins.java ├── EllysXors.java ├── EqualizeStrings.java ├── FavouriteDigits.java ├── FleaCircus.java ├── FoxAndVacation.java ├── FoxListeningToMusic.java ├── GuessTheNumberGame.java ├── ImportantSequence.java ├── KleofasTail.java ├── MinimumLiars.java ├── MonochromePuzzle.java ├── MonstersValley.java ├── MuddyRoad.java ├── NewArenaPassword.java ├── Nisoku.java ├── OptimalQueues.java ├── OrderedNim.java ├── PatrolRoute.java ├── PerfectPermutation.java ├── PrefixFreeSuperset.java ├── RabbitNumbering.java ├── RandomColoring.java ├── RandomSort.cpp ├── ReverseResources.java ├── RowOfColors.java ├── Rumor.java ├── SPartition.java ├── STable.java ├── StrangeCountry.java ├── TagalogDictionary.java ├── TheDivisionGame.cpp ├── TheLuckyString.cpp ├── TrappingRabbit.java ├── TripleStrings.java ├── TwoLotteryGames.java ├── single-round-match │ ├── single-round-match-512 │ │ └── PickAndDelete.java │ ├── single-round-match-518 │ │ ├── ConvexSequence.java │ │ ├── LargestSubsequence.java │ │ └── Nim.java │ ├── single-round-match-519 │ │ └── VerySmoothDecompositions.cpp │ ├── single-round-match-539 │ │ └── FoxBomb.java │ ├── single-round-match-540 │ │ └── ProductQuery.cpp │ ├── single-round-match-541 │ │ └── XorLife.java │ ├── single-round-match-544 │ │ └── SplittingFoxes.java │ ├── single-round-match-545 │ │ └── SetAndSet.java │ ├── single-round-match-546 │ │ └── FleaCircus.java │ ├── single-round-match-548 │ │ └── KingdomAndCities.java │ ├── single-round-match-549 │ │ ├── CosmicBlocks.cpp │ │ └── PointyWizardHats.cpp │ ├── single-round-match-550 │ │ ├── CheckerExpansion.java │ │ ├── ConversionMachine.java │ │ └── RotatingBot.java │ ├── single-round-match-551 │ │ ├── ColorfulChocolates.java │ │ ├── ColorfulWolves.java │ │ └── SweetFruits.cpp │ ├── single-round-match-552 │ │ ├── FoxAndFlowerShopDivOne.cpp │ │ ├── FoxPaintingBalls.java │ │ └── HolyNumbers.cpp │ ├── single-round-match-553 │ │ ├── Suminator.java │ │ ├── TwoConvexShapes.cpp │ │ └── YamanoteLine.java │ ├── single-round-match-554 │ │ ├── TheBrickTowerEasyDivOne.java │ │ ├── TheBrickTowerHardDivOne.cpp │ │ └── TheBrickTowerMediumDivOne.java │ ├── single-round-match-555 │ │ ├── CuttingBitString.java │ │ ├── MapGuessing.java │ │ └── XorBoard.java │ ├── single-round-match-556 │ │ ├── LeftRightDigitsGame2.java │ │ ├── OldBridges.java │ │ └── XorTravelingSalesman.java │ ├── single-round-match-557 │ │ ├── FoxAndMountainEasy.java │ │ ├── Incubator.java │ │ └── XorAndSum.java │ ├── single-round-match-558 │ │ ├── Ear.java │ │ ├── Stamp.java │ │ └── SurroundingGame.java │ ├── single-round-match-559 │ │ ├── CircusTents.java │ │ ├── HatRack.java │ │ └── HyperKnight.java │ ├── single-round-match-560 │ │ ├── BoundedOptimization.java │ │ ├── DrawingPointsDivOne.java │ │ └── TomekPhone.java │ ├── single-round-match-561 │ │ ├── CirclesGame.java │ │ ├── ICPCBalloons.java │ │ └── Orienteering.java │ ├── single-round-match-562 │ │ ├── CheckerFreeness.java │ │ ├── InducedSubgraphs.java │ │ └── PastingPaintingDivOne.java │ ├── single-round-match-563 │ │ ├── CoinsGame.java │ │ ├── FoxAndHandle.java │ │ └── SpellCards.java │ ├── single-round-match-564 │ │ ├── AlternateColors2.java │ │ ├── DefectiveAddition.java │ │ └── KnightCircuit2.java │ ├── single-round-match-565 │ │ └── UnknownTree.java │ ├── single-round-match-566 │ │ ├── FencingPenguins.java │ │ ├── PenguinEmperor.java │ │ └── PenguinSledding.java │ ├── single-round-match-567 │ │ ├── Mountains.java │ │ ├── StringGame.java │ │ └── TheSquareRootDilemma.java │ ├── single-round-match-568 │ │ ├── BallsSeparating.java │ │ ├── DisjointSemicircles.java │ │ └── EqualSums.java │ ├── single-round-match-569 │ │ ├── MegaFactorial.java │ │ ├── TheDevice.java │ │ └── TheJediTest.java │ ├── single-round-match-570 │ │ ├── CentaurCompany.java │ │ ├── CurvyonRails.java │ │ └── RobotHerb.java │ ├── single-round-match-571 │ │ ├── CandyOnDisk.java │ │ ├── FoxAndMp3.java │ │ └── MagicMolecule.java │ ├── single-round-match-572 │ │ ├── NextAndPrev.java │ │ └── SkiResorts.java │ ├── single-round-match-573 │ │ ├── SkiResorts.java │ │ ├── TeamContest.java │ │ └── WolfPack.java │ ├── single-round-match-574 │ │ ├── PolygonTraversal.java │ │ ├── TheNumberGame.java │ │ └── Tunnels.java │ ├── single-round-match-575 │ │ ├── TheNumberGameDivOne.java │ │ ├── TheSwapsDivOne.java │ │ └── TheTilesDivOne.java │ ├── single-round-match-576 │ │ ├── ArcadeManao.java │ │ ├── CharacterBoard.cpp │ │ └── TheExperiment.java │ ├── single-round-match-577 │ │ ├── BoardPainting.java │ │ ├── EllysChessboard.java │ │ └── EllysRoomAssignmentsDiv1.java │ ├── single-round-match-578 │ │ ├── DeerInZooDivOne.java │ │ ├── GooseInZooDivOne.java │ │ └── WolfInZooDivOne.cpp │ ├── single-round-match-579 │ │ ├── RockPaperScissors.java │ │ ├── TravellingPurchasingMan.java │ │ └── UndoHistory.java │ ├── single-round-match-580 │ │ ├── EelAndRabbit.java │ │ ├── ShoutterDiv1.java │ │ └── WallGameDiv1.java │ ├── single-round-match-581 │ │ ├── SurveillanceSystem.java │ │ ├── TreeUnion.java │ │ └── YetAnotherBoardGame.java │ ├── single-round-match-582 │ │ ├── ColorfulBuilding.java │ │ ├── SemiPerfectPower.java │ │ └── SpaceWarDiv1.java │ ├── single-round-match-583 │ │ ├── RandomPaintingOnABoard.java │ │ ├── TravelOnMars.java │ │ └── TurnOnLamps.java │ ├── single-round-match-584 │ │ ├── Egalitarianism.java │ │ ├── Excavations.java │ │ └── FoxTheLinguist.java │ ├── single-round-match-585 │ │ ├── EnclosingTriangle.java │ │ ├── LISNumber.java │ │ └── TrafficCongestion.java │ ├── single-round-match-586 │ │ ├── History.java │ │ ├── PiecewiseLinearFunction.java │ │ └── StringWeight.java │ ├── single-round-match-587 │ │ ├── JumpFurther.java │ │ ├── ThreeColorability.java │ │ └── TriangleXor.java │ ├── single-round-match-588 │ │ ├── GameInDarknessDiv1.java │ │ └── KeyDungeonDiv1.java │ ├── single-round-match-589 │ │ ├── FlippingBitsDiv1.java │ │ ├── GearsDiv1.java │ │ └── GooseTattarrattatDiv1.java │ ├── single-round-match-590 │ │ ├── FoxAndChess.java │ │ ├── FoxAndCity.java │ │ └── XorCards.java │ ├── single-round-match-591 │ │ ├── PyramidSequences.java │ │ ├── StringPath.java │ │ └── TheTree.java │ ├── single-round-match-592 │ │ ├── LittleElephantAndBalls.java │ │ ├── LittleElephantAndPermutationDiv1.cpp │ │ └── SplittingFoxes2.java │ ├── single-round-match-593 │ │ ├── HexagonalBoard.java │ │ ├── MayTheBestPetWin.java │ │ └── WolfDelaymasterHard.java │ ├── single-round-match-594 │ │ ├── AstronomicalRecords.java │ │ ├── FoxAndAvatar.java │ │ └── FoxAndGo3.java │ ├── single-round-match-595 │ │ ├── Constellation.java │ │ ├── LittleElephantAndIntervalsDiv1.java │ │ └── LittleElephantAndRGB.java │ ├── single-round-match-596 │ │ ├── BitwiseAnd.java │ │ ├── IncrementAndDoubling.java │ │ └── SparseFactorial.java │ ├── single-round-match-597 │ │ ├── ConvexPolygonGame.java │ │ ├── LittleElephantAndBoard.java │ │ ├── LittleElephantAndString.java │ │ └── LittleElephantAndSubset.cpp │ ├── single-round-match-598 │ │ ├── FoxAndFencing.java │ │ └── TPS.java │ ├── single-round-match-599 │ │ ├── FindPolygons.java │ │ └── SimilarNames.java │ ├── single-round-match-600 │ │ ├── LotsOfLines.java │ │ ├── ORSolitaire.java │ │ └── PalindromeMatrix.cpp │ ├── single-round-match-601 │ │ ├── WinterAndPresents.java │ │ ├── WinterAndShopping.cpp │ │ └── WinterAndSnowmen.java │ ├── single-round-match-602 │ │ ├── PilingRectsDiv1.cpp │ │ └── TypoCoderDiv1.java │ ├── single-round-match-603 │ │ ├── MaxMinTreeGame.java │ │ ├── PairsOfStrings.java │ │ └── SumOfArrays.cpp │ ├── single-round-match-604 │ │ ├── FamilyCrest.cpp │ │ ├── FoxConnection.java │ │ └── PowerOfThree.java │ ├── single-round-match-605 │ │ ├── AlienAndHamburgers.java │ │ ├── AlienAndPermutation.cpp │ │ └── AlienAndSetDiv1.java │ ├── single-round-match-606 │ │ ├── EllysNumberGuessing.java │ │ ├── EllysPairing.java │ │ └── Subcube.java │ ├── single-round-match-607 │ │ ├── CombinationLockDiv1.java │ │ └── PalindromicSubstringsDiv1.java │ ├── single-round-match-608 │ │ ├── BigO.java │ │ ├── MysticAndCandies.java │ │ └── OneDimensionalRobot.java │ ├── single-round-match-609 │ │ ├── LotteryTree.java │ │ ├── MagicalStringDiv1.java │ │ └── PackingBallsDiv1.java │ ├── single-round-match-610 │ │ ├── AlbertoTheAviator.java │ │ ├── MiningGoldHard.java │ │ └── TheMatrix.java │ ├── single-round-match-611 │ │ ├── Egalitarianism2.java │ │ ├── ElephantDrinking.java │ │ └── LCMSet.java │ ├── single-round-match-612 │ │ ├── EmoticonsDiv1.java │ │ ├── LeftAndRightHandedDiv1.java │ │ └── SpecialCells.java │ ├── single-round-match-613 │ │ ├── RandomGCD.java │ │ ├── TaroCheckers.java │ │ └── TaroFriends.java │ ├── single-round-match-614 │ │ ├── CycleColoring.java │ │ ├── MinimumSquare.java │ │ └── TorusSailing.java │ ├── single-round-match-615 │ │ ├── AlternativePiles.java │ │ ├── AmebaDiv1.java │ │ └── LongLongTripDiv1.java │ ├── single-round-match-616 │ │ ├── ColorfulCoins.java │ │ ├── ThreeLLogo.cpp │ │ └── WakingUp.java │ ├── single-round-match-617 │ │ ├── FarStrings.java │ │ ├── MyLongCake.java │ │ └── PieOrDolphin.java │ ├── single-round-match-618 │ │ ├── Family.java │ │ ├── LongWordsDiv1.java │ │ └── MovingRooksDiv1.java │ ├── single-round-match-619 │ │ ├── GoodCompanyDivOne.java │ │ ├── SimilarSequencesAnother.java │ │ └── SplitStoneGame.java │ ├── single-round-match-620 │ │ ├── CandidatesSelection.cpp │ │ ├── PairGame.cpp │ │ └── PerfectSquare.java │ ├── single-round-match-621 │ │ ├── RadioRange.java │ │ ├── StringsNightmareAgain.java │ │ └── TreesAnalysis.java │ ├── single-round-match-622 │ │ ├── BuildingRoutes.java │ │ ├── Ethernet.java │ │ └── FibonacciXor.cpp │ ├── single-round-match-623 │ │ ├── CatchTheBeat.java │ │ └── UniformBoard.java │ ├── single-round-match-624 │ │ ├── BuildingHeights.java │ │ ├── DrivingPlans.cpp │ │ └── TreeColoring.cpp │ ├── single-round-match-630 │ │ ├── Egalitarianism3.java │ │ └── SuffixArrayDiv1.java │ ├── single-round-match-655 │ │ ├── BichromePainting.java │ │ ├── BichromeSky.cpp │ │ └── Nine.java │ ├── single-round-match-657 │ │ ├── PolynomialGCD.cpp │ │ └── RookGraph.cpp │ ├── single-round-match-658 │ │ └── OddEvenTree.java │ ├── single-round-match-659 │ │ ├── ApplesAndOrangesEasy.java │ │ ├── CampLunch.cpp │ │ └── PublicTransitHard.cpp │ ├── single-round-match-666 │ │ ├── CountBinarySequences.cpp │ │ ├── SumOverPermutations.java │ │ └── WalkOverATree │ │ │ └── WalkOverATree.go │ ├── single-round-match-675 │ │ └── BichromeSky2.cpp │ ├── single-round-match-691 │ │ ├── Moneymanager.cpp │ │ ├── Sunnygraphs.cpp │ │ └── Undivisors.cpp │ ├── single-round-match-692 │ │ ├── HardProof.cpp │ │ ├── LinenCenter.cpp │ │ └── TreeSums.cpp │ ├── single-round-match-693 │ │ ├── BiconnectedDiv1.cpp │ │ ├── BipartiteConstruction.cpp │ │ └── TriangleFree.cpp │ ├── single-round-match-696 │ │ ├── Clicounting.cpp │ │ ├── Gperm.cpp │ │ └── Substreeng.cpp │ └── single-round-match-697 │ │ ├── ConnectedStates.cpp │ │ ├── DivisibleSetDiv1.cpp │ │ └── XorOrderDiv1.cpp └── topcoder-open │ ├── topcoder-open-2011 │ └── semifinal-1 │ │ └── MatrixPower.java │ ├── topcoder-open-2012 │ ├── round-1A │ │ ├── EllysFractions.java │ │ ├── EllysJuice.java │ │ └── EllysLights.java │ ├── round-1B │ │ ├── FoxAndDoraemon.java │ │ ├── FoxAndKgram.java │ │ └── FoxAndPhotography.java │ ├── round-1C │ │ ├── PasswordXGrid.java │ │ ├── PasswordXGuessing.java │ │ └── PasswordXPalindrome.java │ ├── round-2A │ │ ├── CucumberWatering.java │ │ ├── EvenPaths.java │ │ └── SwitchesAndLamps.java │ ├── round-2B │ │ ├── BlurredDartboard.java │ │ ├── HeavyBooks.java │ │ └── SequenceTransmission.java │ ├── round-2C │ │ ├── FlattenOut.java │ │ ├── GreedyTravelingSalesman.java │ │ └── ThreePoints.java │ ├── round-3A │ │ ├── ChromaticNumber.java │ │ ├── CowsMooing.java │ │ └── FoxAndCake.cpp │ ├── round-3B │ │ ├── CrossingTheRiver.java │ │ ├── ElevenMultiples.java │ │ └── PQHulls.java │ ├── semifinal-1 │ │ ├── FloodFill3D.java │ │ ├── StRings.cpp │ │ └── YetAnotherNim.java │ ├── semifinal-2 │ │ ├── BallRemoval.java │ │ ├── IndependentOfOR.java │ │ └── TheAnimalProgrammingCompetitions.java │ └── wildcard │ │ ├── KnightOfIntegerland.java │ │ ├── StringEquations.java │ │ └── StringSequences.java │ ├── topcoder-open-2013 │ ├── round-1A │ │ ├── DirectionBoard.java │ │ ├── HouseBuilding.java │ │ └── TheFrog.java │ ├── round-1B │ │ ├── EllysFigurines.java │ │ ├── EllysPairs.java │ │ └── EllysReversals.java │ ├── round-1C │ │ ├── TheArray.java │ │ ├── TheKnights.java │ │ └── TheOlympiadInInformatics.java │ ├── round-2A │ │ ├── TheLargestString.cpp │ │ ├── TheMagicMatrix.cpp │ │ └── ThePowers.java │ ├── round-2B │ │ ├── FruitTrees.java │ │ ├── LitPanels.java │ │ └── ScotlandYard.java │ ├── round-2C │ │ ├── DancingFoxes.java │ │ ├── TheMountain.java │ │ └── WellTimedSearch.java │ └── round-3A │ │ ├── Block3Checkers.java │ │ ├── TrickyInequality.cpp │ │ └── YetAnotherANDProblem.java │ ├── topcoder-open-2014 │ ├── round-1A │ │ ├── EllysLamps.java │ │ ├── EllysScrabble.java │ │ └── EllysSortingTrimmer.java │ ├── round-1B │ │ ├── EagleInZoo.java │ │ ├── SpamChecker.java │ │ └── WolvesAndSheep.java │ ├── round-1C │ │ ├── FizzBuzzTurbo.java │ │ ├── RedPaint.java │ │ └── Unique.cpp │ ├── round-2A │ │ ├── NarrowPassage.java │ │ ├── SixteenBricks.java │ │ └── TreePuzzle.java │ ├── round-2B │ │ ├── AlwaysDefined.cpp │ │ ├── SumAndProductPuzzle.java │ │ └── SwitchingGame.java │ ├── round-2C │ │ ├── CliqueGraph.java │ │ ├── InverseRMQ.cpp │ │ └── SubstringReversal.java │ └── round-3A │ │ └── BrightLamps.java │ ├── topcoder-open-2016 │ ├── round-2A │ │ ├── CliqueCuts.cpp │ │ ├── LCMGCD.java │ │ └── VendingMachines.cpp │ ├── round-2B │ │ ├── FoxAndGemstone.java │ │ ├── ParenthesesTree.java │ │ └── TriangleTriples.java │ ├── round-2C │ │ ├── BearBall.cpp │ │ ├── BearCircleGame.cpp │ │ └── BearGridRect.cpp │ └── round-3A │ │ ├── FoxAndGo4.cpp │ │ ├── ParenthesesAndPermutation.cpp │ │ └── TopologicalOrdering.cpp │ └── topcoder-open-2017 │ ├── CoprimeMatrix.cpp │ └── HiddenRabbits.cpp ├── training ├── 2012-05-01-x-open-cup │ └── A │ │ └── Main.java ├── 2012-06-13-waterloo-local-contest │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── H.cpp │ ├── I.cpp │ └── J.cpp ├── 2012-06-18-mid-central-usa-2006-and-2007 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ ├── K.cpp │ └── L.cpp ├── 2012-06-19-arab-and-north-africa-2007 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ └── H.cpp ├── 2012-06-24-zoj-monthly-june-2012 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── H.cpp │ ├── J.cpp │ └── K.cpp ├── 2012-06-29-zoj-monthly-june-2009 │ ├── A.cpp │ ├── B.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ └── I.cpp ├── 2012-07-06-ural-su-and-orel-stu-contest-summer-2006 │ ├── A.cpp │ └── J.cpp └── 2012-07-09-usu-personal-contest-2007 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ └── I.cpp ├── uoj └── p192.cpp └── zoj ├── p2112.cpp ├── p2676.cpp ├── p3686.cpp ├── p3812.cpp └── p3940.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.dsYM 3 | *.swp 4 | 5 | *.aux 6 | *.log 7 | *.pdf 8 | 9 | *.class 10 | *.in 11 | *.out 12 | 13 | *.zip 14 | 15 | /summary/ 16 | -------------------------------------------------------------------------------- /algorithm-engagements/algorithm-engagements-2012/obr.cpp: -------------------------------------------------------------------------------- 1 | // Algorithm engagements 2012 2 | // Round 0 -- Picture 3 | #include 4 | 5 | const int N = 1000; 6 | 7 | int n, m; 8 | char map[N][N + 1]; 9 | 10 | int main() { 11 | scanf("%d%d", &n, &m); 12 | for (int i = 0; i < n; ++ i) { 13 | scanf("%s", map[i]); 14 | } 15 | for (int i = 0; i < m; ++ i) { 16 | for (int j = 0; j < n; ++ j) { 17 | putchar(map[n - 1 - j][i]); 18 | } 19 | puts(""); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /codechef/APRIL12/DOUBLE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int test_count; 7 | scanf("%d", &test_count); 8 | while (test_count > 0) { 9 | test_count --; 10 | int n; 11 | scanf("%d", &n); 12 | printf("%d\n", n & ~1); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /codeforces/104-codeforces-beta-round-80/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #80 2 | # Problem A -- Blackjack 3 | cards = [0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 15, 4] 4 | n = gets.to_i - 10 5 | puts (0 <= n and n <= 11) ? cards[n] : 0 6 | -------------------------------------------------------------------------------- /codeforces/110-codeforces-beta-round-84/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #84 2 | # Problem A -- Nearly Lucky Number 3 | def is_lucky(n): 4 | if n == 0: 5 | return False 6 | while n > 0: 7 | if (n % 10) not in [4, 7]: 8 | return False 9 | n /= 10 10 | return True 11 | 12 | print ("NO", "YES")[is_lucky(len(filter(is_lucky, map(int, raw_input()))))] 13 | -------------------------------------------------------------------------------- /codeforces/112-codeforces-beta-round-85/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #85 2 | # Problem A -- Petya and Strings 3 | a = raw_input().lower() 4 | b = raw_input().lower() 5 | if a < b: 6 | print -1 7 | elif a > b: 8 | print 1 9 | else: 10 | print 0 11 | -------------------------------------------------------------------------------- /codeforces/114-codeforces-beta-round-86/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #86 2 | # Problem A -- Cifera 3 | k = input() 4 | l = input() 5 | result = -1 6 | while l % k == 0: 7 | l /= k 8 | result += 1 9 | if l == 1: 10 | print "YES" 11 | print result 12 | else: 13 | print "NO" 14 | -------------------------------------------------------------------------------- /codeforces/116-codeforces-beta-round-87/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #87 2 | # Problem A -- Tram 3 | capacity, current = 0, 0 4 | for i in xrange(input()): 5 | a, b = map(int, raw_input().split()) 6 | current += b - a 7 | capacity = max(capacity, current) 8 | print capacity 9 | -------------------------------------------------------------------------------- /codeforces/118-codeforces-beta-round-89/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #89 2 | # Problem A -- String Task 3 | print '.' + '.'.join(filter(lambda c: c not in ['a', 'o', 'y', 'e', 'u', 'i'], raw_input().lower())) 4 | -------------------------------------------------------------------------------- /codeforces/118-codeforces-beta-round-89/B.py: -------------------------------------------------------------------------------- 1 | # Codefores Beta Round #89 2 | # Problem B -- Present from Lena 3 | gen = lambda n: range(0, n) + range(n, -1, -1) 4 | n = input() 5 | for i in gen(n): 6 | print " " * ((n - i) << 1) + " ".join(map(str, gen(i))) 7 | -------------------------------------------------------------------------------- /codeforces/119-codeforces-beta-round-90/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #90 2 | # Problem A -- Epic Game 3 | from fractions import gcd 4 | a, b, n = map(int, raw_input().split()) 5 | t = 1 6 | while n > 0: 7 | n -= gcd(a, n) 8 | t ^= 1 9 | a, b = b, a 10 | print t 11 | -------------------------------------------------------------------------------- /codeforces/122-codeforces-beta-round-91/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #91 2 | # Problem A -- Lucky Division 3 | from sys import exit 4 | def lucky(n): 5 | while n > 0: 6 | if (n % 10) not in [4, 7]: 7 | return False 8 | n /= 10 9 | return True 10 | 11 | n = input() 12 | for i in xrange(1, n + 1): 13 | if n % i == 0 and lucky(i): 14 | print "YES" 15 | exit(0) 16 | print "NO" 17 | -------------------------------------------------------------------------------- /codeforces/124-codeforces-beta-round-92/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #92 2 | # Problem A -- The number of positions 3 | n, a, b = map(int, raw_input().split()) 4 | print min(n - a, b + 1) 5 | -------------------------------------------------------------------------------- /codeforces/127-codeforces-beta-round-93/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Beta Round #93 2 | // Problem A -- Wasted Time 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | const int N = 100; 9 | 10 | int n, k, x[N], y[N]; 11 | 12 | int sqr(int x) { 13 | return x * x; 14 | } 15 | 16 | int main() { 17 | scanf("%d%d", &n, &k); 18 | for (int i = 0; i < n; ++ i) { 19 | scanf("%d%d", x + i, y + i); 20 | } 21 | double result = 0.0; 22 | for (int i = 0; i + 1 < n; ++ i) { 23 | result += sqrt(sqr(x[i + 1] - x[i]) + sqr(y[i + 1] - y[i])); 24 | } 25 | printf("%.10f\n", result / 50 * k); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /codeforces/129-codeforces-beta-round-94/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Beta Round #94 2 | -- Problem A -- Cookies 3 | main = interact work 4 | 5 | work :: String -> String 6 | work input = show $ solve . parse $ input 7 | 8 | parse :: String -> [Int] 9 | parse = map (\i -> read i :: Int) . words . last . lines 10 | 11 | solve :: [Int] -> Int 12 | solve a = length . filter (\x -> x `mod` 2 == (sum a) `mod` 2) $ a 13 | -------------------------------------------------------------------------------- /codeforces/131-codeforces-beta-round-95/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #95 2 | # Problem A -- cAPS lOCK 3 | all_upper = lambda word: all(map(lambda c: c.isupper(), word)) 4 | word = raw_input() 5 | if all_upper(word) or word[0].islower() and all_upper(word[1:]): 6 | print "".join(map(lambda c: c.upper() if c.islower() else c.lower(), word)) 7 | else: 8 | print word 9 | -------------------------------------------------------------------------------- /codeforces/131-codeforces-beta-round-95/B.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #95 2 | # Problem B -- Opposites Attract 3 | n = input() 4 | a = map(int, raw_input().split()) 5 | result = 0 6 | for i in xrange(1, 11): 7 | result += a.count(i) * a.count(-i) 8 | result += a.count(0) * (a.count(0) - 1) / 2 9 | print result 10 | -------------------------------------------------------------------------------- /codeforces/131-codeforces-beta-round-95/C.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Beta Round #95 2 | -- Problem C -- The World is a Theatre 3 | main :: IO() 4 | main = interact work 5 | 6 | work :: String -> String 7 | work input = show $ solve n m t 8 | where [n, m, t] = map read . words $ input 9 | 10 | factorial :: Integer -> Integer 11 | factorial n = product [1..n] 12 | 13 | binom :: Integer -> Integer -> Integer 14 | binom n k 15 | | n >= k = factorial n `div` factorial k `div` factorial (n - k) 16 | | otherwise = 0 17 | 18 | solve :: Integer -> Integer -> Integer -> Integer 19 | solve n m t = sum [binom n i * binom m (t - i) | i <- [4..t - 1]] 20 | -------------------------------------------------------------------------------- /codeforces/133-codeforces-beta-round-96/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #96 2 | # Problem A -- HQ9+ 3 | print "YES" if any(map(lambda c: c in ['H', 'Q', '9'], raw_input())) else "NO" 4 | -------------------------------------------------------------------------------- /codeforces/136-codeforces-beta-round-97/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #97 2 | # Problem A -- Presents 3 | n = input() 4 | answer = [0] * n 5 | for k, v in enumerate(map(int, raw_input().split())): 6 | answer[v - 1] = k + 1 7 | print " ".join(map(str, answer)) 8 | -------------------------------------------------------------------------------- /codeforces/139-codeforces-beta-round-99/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #99 2 | # Problem A -- Petr and Book 3 | from sys import exit 4 | n = input() 5 | a = list(enumerate(map(int, raw_input().split()))) 6 | while n > 0: 7 | if a[0][1] >= n: 8 | print a[0][0] + 1 9 | exit(0) 10 | n -= a[0][1] 11 | a = a[1:] + [a[0]] 12 | -------------------------------------------------------------------------------- /codeforces/144-codeforces-round-103/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #103 2 | # Problem A -- Arrival of the General 3 | n = input() 4 | a = map(int, raw_input().split()) 5 | result = 0 6 | i = 0 7 | while a[i] != max(a): 8 | i += 1 9 | while i > 0: 10 | result += 1 11 | a[i], a[i - 1] = a[i - 1], a[i] 12 | i -= 1 13 | i = -1 14 | while a[i] != min(a): 15 | i -= 1 16 | while i < -1: 17 | result += 1 18 | a[i], a[i + 1] = a[i + 1], a[i] 19 | i += 1 20 | print result 21 | -------------------------------------------------------------------------------- /codeforces/146-codeforces-round-104/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #104 2 | # Problem A -- Lucky Ticket 3 | n = input() 4 | s = map(int, list(raw_input())) 5 | lucky = lambda x : x in [4, 7] 6 | print "YES" if all(map(lucky, s)) and sum(s[:n >> 1]) == sum(s[n >> 1:]) else "NO" 7 | -------------------------------------------------------------------------------- /codeforces/149-codeforces-round-106/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #106 2 | # Problem A -- Business trip 3 | k, result = input(), 0 4 | a = sorted(map(int, raw_input().split())) 5 | try: 6 | while k > 0: 7 | result += 1 8 | k -= a.pop() 9 | print result 10 | except: 11 | print -1 12 | -------------------------------------------------------------------------------- /codeforces/151-codeforces-round-107/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #107 2 | # Problem A -- Soft Drinking 3 | n, k, l, c, d, p, nl, np = map(int, raw_input().split()) 4 | print min((k * l / nl, c * d, p / np)) / n 5 | -------------------------------------------------------------------------------- /codeforces/152-codeforces-round-108/C.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #108 2 | -- Problem C -- Pocket Book 3 | import Data.List 4 | 5 | main :: IO() 6 | main = interact work 7 | 8 | work :: String -> String 9 | work = show . solve . tail . lines 10 | 11 | solve :: [String] -> Integer 12 | solve strings = (product . map (fromIntegral . length . group . sort) . transpose $ strings) `mod` 1000000007 13 | -------------------------------------------------------------------------------- /codeforces/155-codeforces-round-109/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #109 2 | # Problem A -- I_love_%username% 3 | n = input() 4 | a = map(int, raw_input().split()) 5 | result = 0 6 | for i in xrange(1, n): 7 | if a[i] > max(a[:i]) or a[i] < min(a[:i]): 8 | result += 1 9 | print result 10 | -------------------------------------------------------------------------------- /codeforces/157-codeforces-round-110/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #110 2 | # Problem A -- Game Outcome 3 | a = [] 4 | n = gets.to_i 5 | n.times { a << gets.split.map(&:to_i) } 6 | rows = (columns = [0] * n).dup 7 | (0...n).each do |i| 8 | (0...n).each do |j| 9 | rows[i] += a[i][j] 10 | columns[j] += a[i][j] 11 | end 12 | end 13 | puts rows.product(columns).select {|x, y| x < y }.size 14 | -------------------------------------------------------------------------------- /codeforces/158-vk-cup-qualification-round-1/B.py: -------------------------------------------------------------------------------- 1 | # VK Cup 2012 Qualification Round 1 2 | # Problem B -- Taxi 3 | n = input() 4 | sizes = [0] * 5 5 | for size in map(int, raw_input().split()): 6 | sizes[size] += 1 7 | taxis, spaces = 0, 0 8 | taxis += sizes[4] 9 | taxis += sizes[3] 10 | spaces += sizes[3] 11 | taxis += (sizes[2] + 1) / 2 12 | spaces += (sizes[2] % 2) * 2 13 | taxis += (max(sizes[1] - spaces, 0) + 3) / 4 14 | print taxis 15 | -------------------------------------------------------------------------------- /codeforces/16-codeforces-beta-round-16/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Beta Round #16 2 | -- Problem A -- Flag 3 | import Data.List 4 | 5 | main :: IO() 6 | main = showBool . check . tail . lines =<< getContents 7 | 8 | showBool :: Bool -> IO() 9 | showBool True = putStrLn "YES" 10 | showBool False = putStrLn "NO" 11 | 12 | check :: [String] -> Bool 13 | check flag = (checkRow flag) && (checkColumn flag) 14 | 15 | checkRow :: [String] -> Bool 16 | checkRow = all (\x -> (length . group) x == 1) 17 | 18 | checkColumn :: [String] -> Bool 19 | checkColumn flag = all id . zipWith (/=) rows $ (tail rows) 20 | where rows = map head flag 21 | -------------------------------------------------------------------------------- /codeforces/16-codeforces-beta-round-16/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Beta Round #16 2 | // Problem B -- Burglar and Matches 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef long long LL; 10 | 11 | const int M = 20; 12 | 13 | LL n; 14 | int m; 15 | std::pair boxes[M]; 16 | 17 | int main() { 18 | std::cin >> n >> m; 19 | for (int i = 0; i < m; ++ i) { 20 | std::cin >> boxes[i].second >> boxes[i].first; 21 | } 22 | std::sort(boxes, boxes + m, std::greater >()); 23 | LL answer = 0; 24 | for (int i = 0; i < m && n; ++ i) { 25 | LL take = std::min(n, (LL)boxes[i].second); 26 | n -= take; 27 | answer += take * boxes[i].first; 28 | } 29 | std::cout << answer << std::endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /codeforces/16-codeforces-beta-round-16/C.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Beta Round #16 2 | // Problem C -- Monitor 3 | #include 4 | #include 5 | #include 6 | 7 | int a, b, x, y; 8 | 9 | int main() { 10 | scanf("%d%d%d%d", &a, &b, &x, &y); 11 | int g = std::__gcd(x, y); 12 | x /= g; 13 | y /= g; 14 | int k = std::min(a / x, b / y); 15 | printf("%d %d\n", x * k, y * k); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /codeforces/160-codeforces-round-111/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #111 2 | # Problem A -- Twins 3 | n = input() 4 | a = sorted(map(int, raw_input().split()), reverse = True) 5 | s = sum(a) 6 | i, c = 0, 0 7 | while c <= s - c: 8 | c += a[i] 9 | i += 1 10 | print i 11 | -------------------------------------------------------------------------------- /codeforces/160-codeforces-round-111/B.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #111 2 | -- Problem B -- Unlucky Ticket 3 | import Data.List 4 | 5 | main = interact work 6 | 7 | work :: String -> String 8 | work input = if check first second || check second first then "YES" else "NO" 9 | where n = read . head . lines $ input :: Int 10 | ticket = last . lines $ input 11 | first = take n ticket 12 | second = drop n ticket 13 | 14 | check :: String -> String -> Bool 15 | check a b = and $ zipWith (<) (sort a) (sort b) 16 | -------------------------------------------------------------------------------- /codeforces/165-codeforces-round-112/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #112 2 | # Problem A -- Supercentral Point 3 | n = input() 4 | points = [map(int, raw_input().split()) for i in xrange(n)] 5 | result = 0 6 | for p in points: 7 | comprators = [lambda q: p[0] == q[0] and p[1] < q[1], 8 | lambda q: p[0] == q[0] and p[1] > q[1], 9 | lambda q: p[1] == q[1] and p[0] < q[0], 10 | lambda q: p[1] == q[1] and p[0] > q[0]] 11 | if all([filter(c, points) for c in comprators]): 12 | result += 1 13 | print result 14 | -------------------------------------------------------------------------------- /codeforces/168-codeforces-round-114/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #114 2 | # Problem A -- Wizards and Demonstration 3 | n, x, y = gets.split.map(&:to_i) 4 | puts [(y * n + 99) / 100 - x, 0].max 5 | -------------------------------------------------------------------------------- /codeforces/178-abbyy-20-hard/A.cpp: -------------------------------------------------------------------------------- 1 | // ABBYY Cup 2.0 Hard 2 | // Problem A -- Educational Game 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | const int N = 100000; 9 | 10 | int n, a[N], next[N]; 11 | 12 | int main() { 13 | scanf("%d", &n); 14 | for (int i = 0; i < n; ++ i) { 15 | scanf("%d", a + i); 16 | } 17 | for (int i = 0; i < n; ++ i) { 18 | int j = 0; 19 | while (i + (1 << j + 1) < n) { 20 | j ++; 21 | } 22 | next[i] = i + (1 << j); 23 | } 24 | long long answer = 0; 25 | for (int i = 0; i < n - 1; ++ i) { 26 | answer += a[i]; 27 | a[next[i]] += a[i]; 28 | std::cout << answer << std::endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /codeforces/18-codeforces-beta-round-18/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Beta Round #18 2 | // Problem B -- Platforms 3 | #include 4 | #include 5 | #include 6 | 7 | int n, d, m, l; 8 | 9 | int main() { 10 | scanf("%d%d%d%d", &n, &d, &m, &l); 11 | long long answer = 1000000000000000000LL; 12 | for (int i = 0; i < m; ++ i) { 13 | if ((long long)i * d % m > l) { 14 | answer = std::min(answer, (long long)i * d); 15 | } 16 | } 17 | answer = std::min(answer, (((long long)(n - 1) * m + l) / d + 1) * d); 18 | std::cout << answer << std::endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /codeforces/18-codeforces-beta-round-18/C.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Beta Round #18 2 | // Problem C -- Stripe 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | int main() { 9 | int n; 10 | scanf("%d", &n); 11 | int sum = 0; 12 | std::map map; 13 | for (int i = 0; i < n; ++ i) { 14 | int a; 15 | scanf("%d", &a); 16 | sum += a; 17 | if (i != n - 1) { 18 | map[sum] ++; 19 | } 20 | } 21 | if (sum % 2 == 0) { 22 | printf("%d\n", map[sum / 2]); 23 | } else { 24 | puts("0"); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /codeforces/180-codeforces-round-116/F.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #116 2 | # Problem F -- Mathematical Analysis Rocks! 3 | n = gets.to_i 4 | a, b = [0, 1].map { gets.split.map(&:to_i) } 5 | p = [] 6 | n.times do |i| 7 | p[a[i] - 1] = b[i] 8 | end 9 | puts p.join(' ') 10 | -------------------------------------------------------------------------------- /codeforces/187-codeforces-round-119/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #119 2 | // Problem A -- Permutations 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 222222; 8 | 9 | int n, a[N], p[N]; 10 | 11 | int main() { 12 | scanf("%d", &n); 13 | for (int i = 0; i < n; ++ i) { 14 | scanf("%d", a + i); 15 | } 16 | for (int i = 0; i < n; ++ i) { 17 | int b_i; 18 | scanf("%d", &b_i); 19 | p[b_i] = i; 20 | } 21 | int result = 0; 22 | int last_index = -1; 23 | for (int i = 0; i < n; ++ i) { 24 | if (p[a[i]] > last_index) { 25 | result ++; 26 | last_index = p[a[i]]; 27 | } else { 28 | break; 29 | } 30 | } 31 | printf("%d\n", n - result); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /codeforces/190-codeforces-round-120/D.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #120 2 | // Problem D -- Non-Secret Cypher 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | const int N = 400000; 10 | 11 | int n, m, a[N]; 12 | map frequency; 13 | 14 | int main() { 15 | scanf("%d%d", &n, &m); 16 | for (int i = 0; i < n; ++ i) { 17 | scanf("%d", a + i); 18 | } 19 | long long result = (long long)n * (n + 1) / 2; 20 | int j = n; 21 | for (int i = n - 1; i >= 0; -- i) { 22 | frequency[a[i]] ++; 23 | while (frequency[a[i]] >= m) { 24 | frequency[a[-- j]] --; 25 | } 26 | result -= j - i; 27 | } 28 | cout << result << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /codeforces/197-codeforces-round-124/C.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #124 2 | # Problem C -- Lexicographically Maximum Subsequence 3 | answer = [] 4 | gets.each_char do |c| 5 | answer.pop while not answer.empty? and answer[-1] < c 6 | answer << c 7 | end 8 | puts answer.join("") 9 | -------------------------------------------------------------------------------- /codeforces/20-codeforces-alpha-round-20/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Alpha Round #20 2 | # Problem A -- BerOS file system 3 | path = gets.chop.split(/\/+/) * '/' 4 | puts path == '' ? '/' : path 5 | -------------------------------------------------------------------------------- /codeforces/200-codeforces-round-126/B.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #126 2 | # Problem B -- Drinks 3 | n = gets.to_i 4 | puts gets.split.map(&:to_i).inject { |s, i| s + i } / n.to_f 5 | -------------------------------------------------------------------------------- /codeforces/208-codeforces-round-130/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #130 2 | # Problem A -- Dubstep 3 | print " ".join(raw_input().replace("WUB", " ").split()) 4 | -------------------------------------------------------------------------------- /codeforces/209-vk-cup-2012-finals-pratice-session/A.cpp: -------------------------------------------------------------------------------- 1 | // VK Cup 2012 Finals, Practice Session 2 | // Problem A -- Multicolored Marbles 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int MOD = 1000000000 + 7; 8 | 9 | int n, sum[2]; 10 | 11 | int main() { 12 | scanf("%d", &n); 13 | int answer = 0; 14 | memset(sum, 0, sizeof(sum)); 15 | for (int i = n - 1; i >= 0; -- i) { 16 | int j = i & 1; 17 | int ways = (sum[j ^ 1] + 1) % MOD; 18 | (sum[j] += ways) %= MOD; 19 | (answer += ways) %= MOD; 20 | } 21 | printf("%d\n", answer); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /codeforces/214-codeforces-round-131/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #131 2 | -- Problem A -- System of Equations 3 | main = interact work 4 | 5 | work :: String -> String 6 | work input = show $ solve n m 7 | where numbers = map (\i -> read i :: Int) . words $ input 8 | n = numbers !! 0 9 | m = numbers !! 1 10 | 11 | square :: Int -> Int 12 | square x = x * x 13 | 14 | solve :: Int -> Int -> Int 15 | solve n m = length . filter (check n m) $ [0..n] 16 | 17 | check :: Int -> Int -> Int -> Bool 18 | check n m x = y >= 0 && x + square y == m 19 | where y = n - square x 20 | -------------------------------------------------------------------------------- /codeforces/215-codeforces-round-132/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #132 2 | # Problem A -- Bicycle Chain 3 | n = input() 4 | a = map(int, raw_input().split()) 5 | m = input() 6 | b = map(int, raw_input().split()) 7 | maximum = 0 8 | for i in a: 9 | for j in b: 10 | if j % i == 0: 11 | maximum = max(maximum, j / i) 12 | result = 0 13 | for i in a: 14 | for j in b: 15 | if j % i == 0 and j / i == maximum: 16 | result += 1 17 | print result 18 | -------------------------------------------------------------------------------- /codeforces/219-codeforces-round-135/B.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #135 2 | # Problem B -- Special Offer! Super Price 999 Bourles! 3 | def find(p, l): 4 | if l > 0 and int('9' * l) > p: 5 | return 0 6 | low = 0 7 | high = 10 ** 18 8 | while low < high: 9 | middle = (low + high + 1) >> 1 10 | if int(str(middle) + '9' * l) <= p: 11 | low = middle 12 | else: 13 | high = middle - 1 14 | return int(str(low) + '9' * l) 15 | 16 | p, d = map(int, raw_input().split()) 17 | i = 18 18 | while p - find(p, i) > d: 19 | i -= 1 20 | print find(p, i) 21 | -------------------------------------------------------------------------------- /codeforces/219-codeforces-round-135/E.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Beta Round #94 2 | // Problem E -- Games with Rectangle 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 1000; 8 | const int MOD = 1000000000 + 7; 9 | 10 | int n, m, k; 11 | int binom[N + 1][(N << 1) + 1]; 12 | 13 | int main() { 14 | scanf("%d%d%d", &n, &m, &k); 15 | memset(binom, 0, sizeof(binom)); 16 | for (int i = 0; i <= N; ++ i) { 17 | binom[i][0] = 1; 18 | for (int j = 1; j <= i; ++ j) { 19 | binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % MOD; 20 | } 21 | } 22 | printf("%d\n", (int)((long long)binom[n - 1][k << 1] * binom[m - 1][k << 1] % MOD)); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codeforces/220-codeforces-round-136/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #136 2 | // Problem A -- Little Elephant and Problem 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | const int N = 100000; 9 | 10 | int n, a[N], b[N]; 11 | 12 | int main() { 13 | scanf("%d", &n); 14 | for (int i = 0; i < n; ++ i) { 15 | scanf("%d", a + i); 16 | b[i] = a[i]; 17 | } 18 | sort(b, b + n); 19 | int counter = 0; 20 | for (int i = 0; i < n; ++ i) { 21 | counter += a[i] != b[i]; 22 | } 23 | puts(counter <= 2? "YES": "NO"); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /codeforces/221-codeforces-round-136/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #136 2 | # Problem A -- Little Elephant and Function 3 | 4 | n = input() 5 | position = [i for i in xrange(n)] 6 | for i in xrange(1, n): 7 | position[i], position[i - 1] = position[i - 1], position[i] 8 | result = [0] * n 9 | for i in xrange(n): 10 | result[position[i]] = i + 1 11 | print " ".join(map(str, result)) 12 | -------------------------------------------------------------------------------- /codeforces/221-codeforces-round-136/B.py: -------------------------------------------------------------------------------- 1 | def get_divisors(n): 2 | divisors = [] 3 | i = 1 4 | while i * i <= n: 5 | if n % i == 0: 6 | divisors.append(i) 7 | if i * i != n: 8 | divisors.append(n / i) 9 | i += 1 10 | return divisors 11 | 12 | n = input() 13 | digits = set(map(int, str(n))) 14 | result = 0 15 | for d in get_divisors(n): 16 | found = False 17 | for i in map(int, str(d)): 18 | found |= i in digits 19 | if found: 20 | result += 1 21 | print result 22 | -------------------------------------------------------------------------------- /codeforces/222-codeforces-round-137/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #137 2 | // Problem A -- Shooshuns and Sequence 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | const int N = 100000; 9 | 10 | int n, k, a[N]; 11 | 12 | int main() { 13 | scanf("%d%d", &n, &k); 14 | k --; 15 | for (int i = 0; i < n; ++ i) { 16 | scanf("%d", a + i); 17 | } 18 | int finalValue = *min_element(a + k, a + n); 19 | if (*max_element(a + k, a + n) != finalValue) { 20 | puts("-1"); 21 | } else { 22 | int i = k - 1; 23 | while (i >= 0 && a[i] == finalValue) { 24 | i --; 25 | } 26 | printf("%d\n", i + 1); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /codeforces/222-codeforces-round-137/D.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #137 2 | // Problem D -- Olympiad 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | const int N = 100000; 9 | 10 | int n, limit, a[N], b[N]; 11 | 12 | int main() { 13 | scanf("%d%d", &n, &limit); 14 | for (int i = 0; i < n; ++ i) { 15 | scanf("%d", a + i); 16 | } 17 | sort(a, a + n); 18 | for (int i = 0; i < n; ++ i) { 19 | scanf("%d", b + i); 20 | } 21 | sort(b, b + n); 22 | int result = 0; 23 | for (int i = n - 1, j = 0; i >= 0; -- i) { 24 | while (j < n && a[i] + b[j] < limit) { 25 | j ++; 26 | } 27 | if (j < n) { 28 | j ++; 29 | result ++; 30 | } 31 | } 32 | printf("1 %d\n", result); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /codeforces/224-codeforces-round-138/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #138 2 | # Problem A -- Parallelepiped 3 | a, b, c = map(int, raw_input().split()) 4 | for i in xrange(1, a + 1): 5 | if a % i == 0 and b % i == 0 and a / i * b / i == c: 6 | print 4 * (i + a / i + b / i) 7 | -------------------------------------------------------------------------------- /codeforces/224-codeforces-round-138/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #138 2 | // Problem B -- Array 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | const int N = 100000; 9 | 10 | int n, m, a[N]; 11 | 12 | int main() { 13 | scanf("%d%d", &n, &m); 14 | for (int i = 0; i < n; ++ i) { 15 | scanf("%d", a + i); 16 | } 17 | map hash; 18 | for (int i = 0; i < n; ++ i) { 19 | hash[a[i]] ++; 20 | if (hash[a[i]] == 1) { 21 | m --; 22 | } 23 | if (m == 0) { 24 | int j = 0; 25 | while (hash[a[j]] > 1) { 26 | hash[a[j ++]] --; 27 | } 28 | printf("%d %d\n", j + 1, i + 1); 29 | return 0; 30 | } 31 | } 32 | puts("-1 -1"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /codeforces/225-codeforces-round-139/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #139 2 | # Problem A -- Dice Tower 3 | from sys import exit 4 | n = input() 5 | top = input() 6 | for i in xrange(n): 7 | top = 7 - top 8 | a, b = map(int, raw_input().split()) 9 | sides = [a, b, 7 - a, 7 - b] 10 | choices = [i for i in xrange(1, 7) if i not in sides and i != top] 11 | if len(choices) > 1: 12 | print "NO" 13 | exit(0) 14 | top = choices[0] 15 | print "YES" 16 | -------------------------------------------------------------------------------- /codeforces/225-codeforces-round-139/B.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #139 2 | # Problem B -- Well-known Numbers 3 | s, k = map(int, raw_input().split()) 4 | sums, sequence = 1, [1] 5 | while sums <= s: 6 | sequence.append(sums) 7 | sums += sums 8 | if len(sequence) > k: 9 | sums -= sequence[-(k + 1)] 10 | result = [] 11 | for i in reversed(sequence): 12 | if i <= s: 13 | s -= i 14 | result.append(i) 15 | if len(result) == 1: 16 | result.append(0) 17 | print len(result) 18 | print " ".join(map(str, result)) 19 | -------------------------------------------------------------------------------- /codeforces/225-codeforces-round-139/E.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #139 2 | # Problem E -- Unsolvable 3 | primes = [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 4 | 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 5 | 11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 6 | 216091, 756839, 859433, 1257787, 1398269, 2976221, 3021377, 7 | 6972593, 13466917, 20996011] 8 | n = input() 9 | print pow(2, primes[n - 1] - 1, 10 ** 9 + 7) - 1 10 | -------------------------------------------------------------------------------- /codeforces/226-codeforces-round-140/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #140 2 | # Problem A -- Flying Saucer Segments 3 | n, m = map(int, raw_input().split()) 4 | print (pow(3, n, m) + m - 1) % m 5 | -------------------------------------------------------------------------------- /codeforces/227-codeforces-round-140/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #140 2 | # Problem A -- Where do I Turn? 3 | def det(a, b): 4 | return a[0] * b[1] - a[1] * b[0] 5 | 6 | def subtract(a, b): 7 | return (a[0] - b[0], a[1] - b[1]) 8 | 9 | read = lambda: tuple(map(int, raw_input().split())) 10 | a = read() 11 | b = read() 12 | c = read() 13 | d = det(subtract(b, a), subtract(c, b)) 14 | if d < 0: 15 | print "RIGHT" 16 | elif d == 0: 17 | print "TOWARDS" 18 | else: 19 | print "LEFT" 20 | -------------------------------------------------------------------------------- /codeforces/227-codeforces-round-140/B.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #140 2 | # Problem B -- Effective Approach 3 | n = input() 4 | positions = [0] * n 5 | for i, p in enumerate(map(int, raw_input().split())): 6 | positions[p - 1] = i 7 | m = input() 8 | a, b = 0, 0 9 | for k in map(int, raw_input().split()): 10 | a += 1 + positions[k - 1] 11 | b += n - positions[k - 1] 12 | print a, b 13 | -------------------------------------------------------------------------------- /codeforces/228-codeforces-round-141/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #141 2 | # Problem A -- Is your horseshoe on the other hoof? 3 | print 4 - len(set(map(int, raw_input().split()))) 4 | -------------------------------------------------------------------------------- /codeforces/230-codeforces-round-142/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #142 2 | // Problem A -- Dragons 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | const int N = 1000; 10 | 11 | int s, n; 12 | pair dragons[N]; 13 | 14 | int main() { 15 | scanf("%d%d", &s, &n); 16 | for (int i = 0; i < n; ++ i) { 17 | scanf("%d%d", &dragons[i].first, &dragons[i].second); 18 | } 19 | sort(dragons, dragons + n); 20 | for (int i = 0; i < n; ++ i) { 21 | if (s <= dragons[i].first) { 22 | puts("NO"); 23 | return 0; 24 | } 25 | s += dragons[i].second; 26 | } 27 | puts("YES"); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /codeforces/230-codeforces-round-142/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #142 2 | // Problem B -- T-primes 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | set tprimes; 10 | 11 | bool isPrime(int n) { 12 | for (int i = 2; i * i <= n; ++ i) { 13 | if (n % i == 0) { 14 | return false; 15 | } 16 | } 17 | return true; 18 | } 19 | 20 | int main() { 21 | tprimes.insert(4); 22 | for (int i = 3; i <= 1000000; i += 2) { 23 | if (isPrime(i)) { 24 | tprimes.insert((long long)i * i); 25 | } 26 | } 27 | int n; 28 | scanf("%d", &n); 29 | while (n --) { 30 | long long x; 31 | cin >> x; 32 | puts(tprimes.find(x) == tprimes.end()? "NO": "YES"); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /codeforces/230-codeforces-round-142/E.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #142 2 | // Problem E -- Triangles 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | const int N = 1000000; 9 | 10 | int n, m, degree[N]; 11 | 12 | int main() { 13 | scanf("%d%d", &n, &m); 14 | for (int i = 0; i < n; ++ i) { 15 | degree[i] = 0; 16 | } 17 | for (int i = 0; i < m; ++ i) { 18 | int a, b; 19 | scanf("%d%d", &a, &b); 20 | degree[-- a] ++; 21 | degree[-- b] ++; 22 | } 23 | long long result = 0; 24 | for (int i = 0; i < n; ++ i) { 25 | result += (long long)degree[i] * (n - 1 - degree[i]); 26 | } 27 | cout << (long long)n * (n - 1) * (n - 2) / 6 - result / 2 << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /codeforces/231-codeforces-round-143/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #143 2 | -- Problem A -- Team 3 | main = interact work 4 | 5 | work :: String -> String 6 | work input = show . length . filter (\l -> sum l >= 2) . map parse . tail . lines $ input 7 | 8 | parse :: String -> [Int] 9 | parse line = map (\i -> read i :: Int) . words $ line 10 | -------------------------------------------------------------------------------- /codeforces/231-codeforces-round-143/D.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #143 2 | # Problem D -- Magic Box 3 | read_ints = lambda: map(int, raw_input().split()) 4 | x, y, z = read_ints() 5 | a, b, c = read_ints() 6 | y_min, y_max, z_min, z_max, x_min, x_max = read_ints() 7 | result = 0 8 | if x < 0: 9 | result += x_min 10 | if x > a: 11 | result += x_max 12 | if y < 0: 13 | result += y_min 14 | if y > b: 15 | result += y_max 16 | if z < 0: 17 | result += z_min 18 | if z > c: 19 | result += z_max 20 | print result 21 | -------------------------------------------------------------------------------- /codeforces/233-codeforces-round-144/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #144 2 | # Problem A -- Perfect Permutation 3 | n = input() 4 | if n % 2 == 0: 5 | print " ".join(map(str, [(i ^ 1) + 1 for i in xrange(n)])) 6 | else: 7 | print -1 8 | -------------------------------------------------------------------------------- /codeforces/234-codeforces-round-145/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #145 2 | // Problem A -- Lefthanders and Righthanders 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 100; 8 | 9 | int n; 10 | char type[N + 2]; 11 | 12 | int main() { 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | scanf("%d%s", &n, type + 1); 16 | for (int i = 1; i <= n >> 1; ++ i) { 17 | if (type[i] == 'R' && type[i + (n >> 1)] == 'L') { 18 | printf("%d %d\n", i + (n >> 1), i); 19 | } else { 20 | printf("%d %d\n", i, i + (n >> 1)); 21 | } 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codeforces/234-codeforces-round-145/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #155 2 | // Problem B -- Reading 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | const int N = 1000; 11 | 12 | int n, k, level[N]; 13 | pair order[N]; 14 | 15 | int main() { 16 | freopen("input.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | scanf("%d%d", &n, &k); 19 | for (int i = 0; i < n; ++ i) { 20 | scanf("%d", level + i); 21 | order[i] = make_pair(level[i], i); 22 | } 23 | sort(order, order + n, greater > ()); 24 | printf("%d\n", order[k - 1].first); 25 | for (int i = 0; i < k; ++ i) { 26 | printf("%d%c", order[i].second + 1, i == k - 1 ? '\n' : ' '); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /codeforces/235-codeforces-round-146/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #146 2 | // Problem A -- LCM Challenge 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | 10 | LL gcd(LL a, LL b) { 11 | return b ? gcd(b, a % b) : a; 12 | } 13 | 14 | LL lcm(LL a, LL b) { 15 | return a / gcd(a, b) * b; 16 | } 17 | 18 | int main() { 19 | int n; 20 | cin >> n; 21 | LL result = 0; 22 | for (LL i = max(n - 50, 1); i <= n; ++ i) { 23 | for (LL j = max(n - 50, 1); j <= n; ++ j) { 24 | for (LL k = max(n - 50, 1); k <= n; ++ k) { 25 | result = max(result, lcm(lcm(i, j), k)); 26 | } 27 | } 28 | } 29 | cout << result << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /codeforces/235-codeforces-round-146/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #146 2 | // Problem B -- Let's Play Osu! 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int n; 9 | scanf("%d", &n); 10 | double result = 0; 11 | double sum = 0; 12 | for (int i = 0; i < n; ++ i) { 13 | double p; 14 | scanf("%lf", &p); 15 | sum = (1 + sum) * p; 16 | result += 2 * sum; 17 | result -= p; 18 | } 19 | printf("%.8f\n", result); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codeforces/236-codeforces-round-146/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #146 2 | # Problem A -- Boy or Girl 3 | puts gets.split(//).uniq.size.odd? ? "CHAT WITH HER!" : "IGNORE HIM!" 4 | -------------------------------------------------------------------------------- /codeforces/237-codeforces-round-147/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #147 2 | // Problem A -- Free Cash 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int counter[60 * 24]; 9 | 10 | int main() { 11 | memset(counter, 0, sizeof(counter)); 12 | int n; 13 | scanf("%d", &n); 14 | for (int i = 0; i < n; ++ i) { 15 | int h, m; 16 | scanf("%d%d", &h, &m); 17 | counter[h * 60 + m] ++; 18 | } 19 | printf("%d\n", *max_element(counter, counter + 60 * 24)); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codeforces/238-codeforces-round-148/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #148 2 | // Problem A -- Not Wool Sequences 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int MOD = 1000000000 + 9; 8 | 9 | int n, m; 10 | 11 | int pow(int a, int n) { 12 | int ret = 1; 13 | while (n) { 14 | if (n & 1) { 15 | ret = (long long)ret * a % MOD; 16 | } 17 | a = (long long)a * a % MOD; 18 | n >>= 1; 19 | } 20 | return ret; 21 | } 22 | 23 | int main() { 24 | scanf("%d%d", &n, &m); 25 | int left = (pow(2, m) + MOD - 1) % MOD; 26 | int answer = 1; 27 | for (int i = 0; i < n; ++ i) { 28 | answer = (long long)answer * left % MOD; 29 | (left += MOD - 1) %= MOD; 30 | } 31 | printf("%d\n", answer); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /codeforces/24-codeforces-beta-round-24/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cin; 6 | 7 | const int N = 100000; 8 | 9 | int n, x_0, y_0, x[N], y[N]; 10 | long long m; 11 | 12 | int solve(int a_0, int* a) { 13 | m %= 2 * n; 14 | for (int i = 0; i < m; ++ i) { 15 | a_0 = 2 * a[i % n] - a_0; 16 | } 17 | return a_0; 18 | } 19 | 20 | int main() { 21 | cin >> n >> m >> x_0 >> y_0; 22 | for (int i = 0; i < n; ++ i) { 23 | cin >> x[i] >> y[i]; 24 | } 25 | printf("%d %d\n", solve(x_0, x), solve(y_0, y)); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /codeforces/241-bayan-2012-2013-elimination-round/A.rb: -------------------------------------------------------------------------------- 1 | # Bayan 2012-2013 Elimination Round 2 | # Problem A -- Old Peykan 3 | read = lambda { gets.split.map(&:to_i) } 4 | m, k = read.call 5 | d = read.call 6 | s = read.call 7 | answer = now = left = delta = 0 8 | while now < m 9 | delta = [delta, s[now]].max 10 | left += s[now] 11 | while left < d[now] 12 | answer += k 13 | left += delta 14 | end 15 | answer += d[now] 16 | left -= d[now] 17 | now += 1 18 | end 19 | puts answer 20 | -------------------------------------------------------------------------------- /codeforces/242-codeforces-round-149/A.rb: -------------------------------------------------------------------------------- 1 | x, y, a, b = gets.split.map(&:to_i) 2 | outcomes = [] 3 | a.upto(x) do |i| 4 | b.upto(y) do |j| 5 | outcomes << [i, j] if i > j 6 | end 7 | end 8 | puts outcomes.size 9 | outcomes.sort.each do |outcome| 10 | puts outcome.join(" ") 11 | end 12 | -------------------------------------------------------------------------------- /codeforces/242-codeforces-round-149/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #149 2 | // Problem B -- Big Segment 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 100000; 8 | 9 | int n, l[N], r[N]; 10 | 11 | 12 | int main() { 13 | scanf("%d", &n); 14 | for (int i = 0; i < n; ++ i) { 15 | scanf("%d%d", l + i, r + i); 16 | } 17 | int x = 0; 18 | for (int i = 1; i < n; ++ i) { 19 | if (l[i] < l[x] || l[i] == l[x] && r[i] > r[x]) { 20 | x = i; 21 | } 22 | } 23 | for (int i = 0; i < n; ++ i) { 24 | if (r[i] > r[x]) { 25 | puts("-1"); 26 | return 0; 27 | } 28 | } 29 | printf("%d\n", x + 1); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /codeforces/246-codeforces-round-151/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #151 2 | -- Problem A -- Buggy Sorting 3 | main :: IO() 4 | main = prettify . solve . (\x -> read x :: Int) =<< getLine 5 | 6 | solve :: Int -> Maybe [Int] 7 | solve n 8 | | n <= 2 = Nothing 9 | | otherwise = Just (reverse [1..n]) 10 | 11 | prettify :: Maybe [Int] -> IO() 12 | prettify Nothing = putStrLn "-1" 13 | prettify (Just list) = putStrLn $ unwords . map show $ list 14 | -------------------------------------------------------------------------------- /codeforces/251-codeforces-round-153/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #153 2 | // Problem A -- Points on Line 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | const int N = 100000; 9 | 10 | int n, d, a[N]; 11 | 12 | int main() { 13 | scanf("%d%d", &n, &d); 14 | for (int i = 0; i < n; ++ i) { 15 | scanf("%d", a + i); 16 | } 17 | long long ways = 0; 18 | for (int i = 0; i < n; ++ i) { 19 | int size = std::upper_bound(a, a + n, a[i] + d) - a - i - 1; 20 | if (size >= 2) { 21 | ways += (long long)size * (size - 1) / 2; 22 | } 23 | } 24 | std::cout << ways << std::endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /codeforces/253-codeforces-round-154/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int count[2]; 5 | 6 | int main() { 7 | freopen("input.txt", "r", stdin); 8 | freopen("output.txt", "w", stdout); 9 | scanf("%d%d", count, count + 1); 10 | int now = count[0] < count[1]; 11 | for (int i = count[0] + count[1]; i; -- i) { 12 | if (!count[now]) { 13 | now ^= 1; 14 | } 15 | putchar(now ? 'G' : 'B'); 16 | count[now] --; 17 | now ^= 1; 18 | } 19 | puts(""); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /codeforces/253-codeforces-round-154/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #154 2 | // Problem B -- Physics Practical 3 | #include 4 | #include 5 | #include 6 | 7 | const int N = 100000; 8 | 9 | int n, a[N]; 10 | 11 | int main() { 12 | freopen("input.txt", "r", stdin); 13 | freopen("output.txt", "w", stdout); 14 | scanf("%d", &n); 15 | for (int i = 0; i < n; ++ i) { 16 | scanf("%d", a + i); 17 | } 18 | std::sort(a, a + n); 19 | int answer = n; 20 | for (int i = 0; i < n; ++ i) { 21 | answer = std::min(answer, (int)(i + n - (std::upper_bound(a, a + n, 2 * a[i]) - a))); 22 | } 23 | printf("%d\n", answer); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /codeforces/255-codeforces-round-156/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #156 2 | # Problem A -- Greg's Workout 3 | n = gets.to_i 4 | count = [0] * 3 5 | gets.split.map(&:to_i).each_with_index do |a, i| 6 | count[i % 3] += a 7 | end 8 | i = 0 9 | i += 1 until count[i] == count.max 10 | puts %w(chest biceps back)[i] 11 | -------------------------------------------------------------------------------- /codeforces/255-codeforces-round-156/B.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #156 2 | # Problem B -- Code Parsing 3 | count = [0, 0] 4 | gets.strip.each_char do |c| 5 | count[c == 'x' ? 0 : 1] += 1 6 | end 7 | if count[0] > count[1] 8 | puts "x" * (count[0] - count[1]) 9 | else 10 | puts "y" * (count[1] - count[0]) 11 | end 12 | -------------------------------------------------------------------------------- /codeforces/256-codeforces-round-156/B.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #156 2 | # Problem B -- Mr. Bender and Square 3 | def square(x) 4 | x * x 5 | end 6 | 7 | def triangle(x) 8 | x * (x + 1) / 2 9 | end 10 | 11 | def solve(n, x, y, t) 12 | count = t * t + (t + 1) * (t + 1) 13 | 4.times do 14 | count -= square(t - x + 1) if x <= t 15 | count += triangle(t - x - y + 1) if x + y <= t 16 | x, y = n + 1 - y, x 17 | end 18 | count 19 | end 20 | 21 | n, x, y, c = gets.split.map(&:to_i) 22 | low, high = 0, (n - 1) * 2 23 | while low < high 24 | middle = (low + high) / 2 25 | if solve(n, x, y, middle) >= c 26 | high = middle 27 | else 28 | low = middle + 1 29 | end 30 | end 31 | p high 32 | -------------------------------------------------------------------------------- /codeforces/258-codeforces-round-157/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #157 2 | // Problem A -- Little Elephant and Bits 3 | #include 4 | #include 5 | 6 | const int N = 100000; 7 | 8 | char s[N + 1]; 9 | 10 | int main() { 11 | scanf("%s", s); 12 | int n = strlen(s); 13 | for (int i = 0; i < n; ++ i) { 14 | if (s[i] == '0') { 15 | for (int j = 0; j < n; ++ j) { 16 | if (i != j) { 17 | putchar(s[j]); 18 | } 19 | } 20 | puts(""); 21 | return 0; 22 | } 23 | } 24 | printf("%s\n", s + 1); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /codeforces/259-codeforces-round-157/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #157 2 | # Problem A -- Little Elephant and Chess 3 | 8.times do 4 | row = gets 5 | 7.times do |j| 6 | if row[j] == row[j + 1] 7 | puts "NO" 8 | exit(0) 9 | end 10 | end 11 | end 12 | puts "YES" 13 | -------------------------------------------------------------------------------- /codeforces/26-codeforces-beta-round-26/D.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Beta Round #26 2 | -- Problem D -- Tickets 3 | main :: IO() 4 | main = interact work 5 | 6 | work :: String -> String 7 | work input = show $ solve n m k 8 | where [n, m, k] = map (\i -> read i :: Int) . words $ input 9 | 10 | solve :: Int -> Int -> Int -> Double 11 | solve n m k = max 0 (1 - product [fromIntegral (m - i) / fromIntegral (n + i + 1) | i <- [0..k]]) 12 | -------------------------------------------------------------------------------- /codeforces/260-codeforces-round-158/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #158 2 | // Problem A -- Adding Digits 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int a, b, n; 10 | 11 | int main() { 12 | scanf("%d%d%d", &a, &b, &n); 13 | std::stringstream sout; 14 | sout << a; 15 | while (n --) { 16 | int d = 9; 17 | while (d >= 0 && (a * 10 + d) % b != 0) { 18 | d --; 19 | } 20 | if (d == -1) { 21 | std::cout << -1 << std::endl; 22 | return 0; 23 | } 24 | sout << d; 25 | a = (a * 10 + d) % b; 26 | } 27 | std::string s; 28 | sout >> s; 29 | std::cout << s << std::endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /codeforces/266-codeforces-round-163/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #163 2 | -- Problem A -- Stones on the Table 3 | main :: IO() 4 | main = 5 | getLine >> 6 | getLine >>= \s -> 7 | print $ length . filter id $ zipWith (==) s (tail s) 8 | -------------------------------------------------------------------------------- /codeforces/271-codeforces-round-166/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #166 2 | # Problem A -- Beautiful Year 3 | def check(n) 4 | n.to_s.split(//).uniq.size == 4 5 | end 6 | 7 | n = gets.to_i 8 | begin 9 | n += 1 10 | end until check(n) 11 | puts n 12 | -------------------------------------------------------------------------------- /codeforces/271-codeforces-round-166/C.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #166 2 | // Problem C -- Secret 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int k, n; 9 | 10 | int main() { 11 | scanf("%d%d", &n, &k); 12 | if (n < 3 * k) { 13 | puts("-1"); 14 | return 0; 15 | } 16 | std::vector answer; 17 | for (int i = 1; i <= k; ++ i) { 18 | answer.push_back(i); 19 | answer.push_back(i); 20 | } 21 | for (int i = 1; i <= k; ++ i) { 22 | answer.push_back(i); 23 | } 24 | for (int i = 3 * k; i < n; ++ i) { 25 | answer.push_back(rand() % k + 1); 26 | } 27 | for (int i = 0; i < n; ++ i) { 28 | printf("%d ", answer[i]); 29 | } 30 | puts(""); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /codeforces/275-codeforces-round-168/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Round #168 2 | # Problem A -- Lights Out 3 | def count(a, i, j): 4 | total = 1 5 | for x, y in ((-1, 0), (0, -1), (0, 0), (0, 1), (1, 0)): 6 | if 0 <= i + x < 3 and 0 <= j + y < 3: 7 | total += a[i + x][j + y] 8 | return str(total % 2) 9 | a = [map(int, raw_input().split()) for _ in xrange(3)] 10 | for i in xrange(3): 11 | print ''.join([count(a, i, j) for j in xrange(3)]) 12 | -------------------------------------------------------------------------------- /codeforces/276-codeforces-round-169/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #169 2 | # Problem A -- Lunch Rush 3 | n, k = gets.split.map(&:to_i) 4 | puts([*1..n].map do 5 | f, t = gets.split.map(&:to_i) 6 | if t <= k 7 | f 8 | else 9 | f - (t - k) 10 | end 11 | end.max) 12 | -------------------------------------------------------------------------------- /codeforces/276-codeforces-round-169/B.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #169 2 | # Problem B -- Little Girl and Game 3 | count = [0] * 26 4 | gets.chomp.each_char do |c| 5 | count[c.ord - 'a'.ord] += 1 6 | end 7 | odds = count.count { |x| x % 2 == 1 } 8 | puts (odds <= 1 or odds % 2 == 1) ? "First" : "Second" 9 | -------------------------------------------------------------------------------- /codeforces/276-codeforces-round-169/D.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #169 2 | # Problem D -- Little Girl and Maximum XOR 3 | l, r = gets.split.map(&:to_i) 4 | if l == r 5 | puts 0 6 | else 7 | 59.downto(0) do |i| 8 | if l >= (1 << i) 9 | l -= 1 << i 10 | r -= 1 << i 11 | elsif r >= (1 << i) 12 | puts (1 << i + 1) - 1 13 | exit 0 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /codeforces/282-codeforces-round-173/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #173 2 | # Problem A -- Bit++ 3 | puts([*1..gets.to_i].inject(0) do |x, i| 4 | x + (gets.index('+') ? 1 : -1) 5 | end) 6 | -------------------------------------------------------------------------------- /codeforces/282-codeforces-round-173/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #173 2 | // Problem B -- Painting Eggs 3 | #include 4 | #include 5 | 6 | int n; 7 | 8 | int main() { 9 | scanf("%d", &n); 10 | int sum = 0; 11 | for (int i = 0, a, b; i < n; ++ i) { 12 | scanf("%d%d", &a, &b); 13 | if (sum + a <= 500) { 14 | sum += a; 15 | putchar('A'); 16 | } else { 17 | sum -= b; 18 | putchar('G'); 19 | } 20 | } 21 | puts(""); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /codeforces/282-codeforces-round-173/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int N = 1000000; 5 | 6 | char a[N + 1], b[N + 1]; 7 | 8 | int main() { 9 | scanf("%s%s", a, b); 10 | if (strlen(a) != strlen(b)) { 11 | puts("NO"); 12 | } else { 13 | int n = strlen(a); 14 | int i = 0; 15 | while (a[i] == '0') { 16 | i ++; 17 | } 18 | int j = 0; 19 | while (b[j] == '0') { 20 | j ++; 21 | } 22 | puts((i < n) ^ (j < n) ? "NO": "YES"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /codeforces/285-codeforces-round-175/D.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #175 2 | -- Problem D - Permutation Sum 3 | answer = [1, 3, 15, 133, 2025, 37851, 1030367, 36362925] 4 | 5 | solve :: Int -> Integer 6 | solve n = if n `mod` 2 == 0 7 | then 0 8 | else answer !! (n `div` 2) * product [1..fromIntegral n] `mod` (1000000000 + 7) 9 | 10 | main = interact work where 11 | work = show . solve . (\x -> read x :: Int) 12 | -------------------------------------------------------------------------------- /codeforces/29-codeforces-beta-round-29/B.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Beta Round #29 2 | -- Problem B -- Traffic Lights 3 | main :: IO() 4 | main = interact work 5 | 6 | work :: String -> String 7 | work input = show (solve l d v g r) 8 | where [l, d, v, g, r] = map (\i -> read i :: Int) . words $ input 9 | 10 | solve :: Int -> Int -> Int -> Int -> Int -> Double 11 | solve l d v g r = total + penalty 12 | where total = fromIntegral l / fromIntegral v 13 | penalty = if left < v * g 14 | then 0 15 | else fromIntegral (g + r) - fromIntegral left / fromIntegral v 16 | left = d `mod` (v * (g + r)) 17 | -------------------------------------------------------------------------------- /codeforces/299-croc-champ-2013-round-2/A.rb: -------------------------------------------------------------------------------- 1 | # Croc Champ 2013 - Round 2 2 | # Problem A -- Ksusha and Array 3 | n = gets.to_i 4 | as = gets.split.map(&:to_i) 5 | g = as.inject(0) { |g, a| g.gcd(a) } 6 | as.each do |a| 7 | if g % a == 0 8 | puts a 9 | exit 0 10 | end 11 | end 12 | puts -1 13 | -------------------------------------------------------------------------------- /codeforces/30-codeforces-beta-round-30/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #30 2 | # Problem A -- Accounting 3 | no = "No solution" 4 | 5 | a, b, n = map(int, raw_input().split()) 6 | if a == 0: 7 | if b == 0: 8 | print 0 9 | else: 10 | print no 11 | else: 12 | if b % a == 0: 13 | c = b / a 14 | if c < 0 and n % 2 == 0: 15 | print no 16 | else: 17 | x = (int)(round(pow(abs(c), 1.0 / n) * (1, -1)[c < 0])) 18 | if a * pow(x, n) == b: 19 | print x 20 | else: 21 | print no 22 | else: 23 | print no 24 | -------------------------------------------------------------------------------- /codeforces/300-codeforces-round-181/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #181 2 | -- Problem A -- Array 3 | main :: IO() 4 | main = interact work 5 | 6 | work :: String -> String 7 | work input = unlines . map showList . solve $ numbers 8 | where numbers = map parseInt . words . last . lines $ input 9 | parseInt str = read str :: Int 10 | showList lst = (show . length $ lst) ++ " " ++ (unwords . map show $ lst) 11 | 12 | solve :: [Int] -> [[Int]] 13 | solve numbers = if null ps 14 | then [[head ns], tail . take 3 $ ns, drop 3 ns ++ zs] 15 | else [[head ns], [head ps], tail ns ++ tail ps ++ zs] 16 | where ps = filter (\x -> x > 0) numbers 17 | ns = filter (\x -> x < 0) numbers 18 | zs = filter (\x -> x == 0) numbers 19 | -------------------------------------------------------------------------------- /codeforces/301-codeforces-round-182/C.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Round #182 2 | # Problem C -- Yaroslav and Algorithm 3 | 9.times { |d| puts "#{d}??<>#{d + 1}" } 4 | puts "9??>>??0" 5 | puts "??<>1" 6 | 10.times { |d| puts "?#{d}>>#{d}?" } 7 | puts "?>>??" 8 | 10.times { |d| puts "#{d}>>?#{d}" } 9 | -------------------------------------------------------------------------------- /codeforces/304-codeforces-round-183/C.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #183 2 | -- Problem C -- Lucky Permutation Triple 3 | main :: IO() 4 | main = interact work 5 | 6 | work :: String -> String 7 | work input = unlines . map unwords . map (map show) . solve $ n 8 | where n = read input :: Int 9 | 10 | solve :: Int -> [[Int]] 11 | solve n = if n `mod` 2 == 0 12 | then [[-1]] 13 | else [a, a, [i * 2 `mod` n | i <- a]] 14 | where a = [0..n - 1] 15 | -------------------------------------------------------------------------------- /codeforces/305-codeforces-round-184/B.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #184 2 | // Problem B -- Continued Fractions 3 | #include 4 | #include 5 | #include 6 | 7 | typedef long long LL; 8 | 9 | const int N = 90; 10 | 11 | int n; 12 | LL p, q, a[N]; 13 | 14 | bool check(LL p, LL q, int i) { 15 | if (i < n) { 16 | if (q == 0) { 17 | return false; 18 | } 19 | if (p / q < a[i]) { 20 | return false; 21 | } 22 | return check(q, p - a[i] * q, i + 1); 23 | } 24 | return q == 0; 25 | } 26 | 27 | int main() { 28 | std::cin >> p >> q >> n; 29 | for (int i = 0; i < n; ++ i) { 30 | std::cin >> a[i]; 31 | } 32 | puts(check(p, q, 0) ? "YES" : "NO"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /codeforces/305-codeforces-round-184/C.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Round #184 2 | // Problem C -- Ivan and Powers of Two 3 | #include 4 | #include 5 | #include 6 | 7 | int n; 8 | 9 | int main() { 10 | scanf("%d", &n); 11 | int now = 0; 12 | int count = 0; 13 | int answer = 0; 14 | for (int i = 0; i < n; ++ i) { 15 | int a; 16 | scanf("%d", &a); 17 | while (now < a && count) { 18 | now ++; 19 | answer -= count & 1; 20 | count >>= 1; 21 | } 22 | now = a; 23 | count ++; 24 | } 25 | while (count) { 26 | now ++; 27 | answer -= count & 1; 28 | count >>= 1; 29 | } 30 | answer += now; 31 | printf("%d\n", answer); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /codeforces/306-testing-round-6/A.rb: -------------------------------------------------------------------------------- 1 | # Testing Round #6 2 | # Problem A -- Candies 3 | n, m = gets.split.map(&:to_i) 4 | puts [*0...m].map { |i| n / m + (i < n % m ? 1 : 0) }.join(' ') 5 | -------------------------------------------------------------------------------- /codeforces/31-codeforces-beta-round-31/B.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #31 2 | # Problem B -- Sysadmin Bob 3 | tokens = raw_input().split('@') 4 | if len(tokens) >= 2 and len(tokens[0]) >= 1 and len(tokens[-1]) >= 1 and (len(tokens) <= 2 or min(map(len, tokens[1:-1])) >= 2): 5 | address = [] 6 | for i in reversed(xrange(len(tokens) - 1)): 7 | address.append((tokens[i][-1] if i > 0 else tokens[0]) + '@' + tokens[i + 1]) 8 | tokens[i] = tokens[i][:-1] 9 | print ",".join(reversed(address)) 10 | else: 11 | print "No solution" 12 | -------------------------------------------------------------------------------- /codeforces/317-codeforces-round-188/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Round #188 2 | -- Problem A -- Ants 3 | main :: IO() 4 | main = interact mainFunction 5 | 6 | mainFunction :: String -> String 7 | mainFunction input = show $ solve x y m 8 | where [x, y, m] = map parseInt . words $ input 9 | 10 | parseInt :: String -> Integer 11 | parseInt = read 12 | 13 | solve :: Integer -> Integer -> Integer -> Integer 14 | solve x y m = if x >= m || y >= m 15 | then 0 16 | else if x <= 0 && y <= 0 17 | then -1 18 | else if x > y 19 | then solve y x m 20 | else let s = (y - x + y) `div` y 21 | in solve (x + s * y) y m + s 22 | -------------------------------------------------------------------------------- /codeforces/32-codeforces-beta-round-32/A.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Beta Round #32 2 | // Problem A -- Reconnaissance 3 | #include 4 | #include 5 | #include 6 | 7 | const int N = 1000; 8 | 9 | int n, d, a[N]; 10 | 11 | int main() { 12 | scanf("%d%d", &n, &d); 13 | int answer = 0; 14 | for (int i = 0; i < n; ++ i) { 15 | scanf("%d", a + i); 16 | for (int j = 0; j < i; ++ j) { 17 | if (std::abs(a[i] - a[j]) <= d) { 18 | answer ++; 19 | } 20 | } 21 | } 22 | printf("%d\n", answer * 2); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /codeforces/32-codeforces-beta-round-32/B.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Beta Round #32 2 | -- Problem B -- Borze 3 | main :: IO() 4 | main = putStrLn . parse =<< getLine 5 | 6 | parse :: String -> String 7 | parse "" = "" 8 | parse "." = "0" 9 | parse (a:b:s) = if a == '.' 10 | then '0' : parse (b:s) 11 | else (if b == '.' 12 | then '1' 13 | else '2') : parse s 14 | -------------------------------------------------------------------------------- /codeforces/32-codeforces-beta-round-32/C.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Beta Round #32 2 | -- Problem C -- Flea 3 | main :: IO() 4 | main = interact work 5 | 6 | work :: String -> String 7 | work input = let [n, m, s] = map (\i -> read i :: Integer) . words $ input 8 | in show $ solve n m s 9 | 10 | solve :: Integer -> Integer -> Integer -> Integer 11 | solve n m s = solve1D n s * solve1D m s 12 | 13 | solve1D :: Integer -> Integer -> Integer 14 | solve1D n s = let r = n `mod` s 15 | in if r == 0 16 | then n 17 | else (n `div` s + 1) * r 18 | -------------------------------------------------------------------------------- /codeforces/39-school-team-contest-1/H.hs: -------------------------------------------------------------------------------- 1 | -- School Team Contest #1 2 | -- Problem H -- Multiplication Table 3 | main :: IO() 4 | main = interact work 5 | 6 | work :: String -> String 7 | work input = unlines . map (unwords . map (showBase n)) . solve $ n 8 | where n = read input :: Int 9 | 10 | showBase :: Int -> Int -> String 11 | showBase b n 12 | | n == 0 = "" 13 | | otherwise = showBase b (n `div` b) ++ show (n `mod` b) 14 | 15 | solve :: Int -> [[Int]] 16 | solve n = [[i * j | j <- numbers] | i <- numbers] 17 | where numbers = [1..n - 1] 18 | -------------------------------------------------------------------------------- /codeforces/549-looksery-cup-2015/A.hs: -------------------------------------------------------------------------------- 1 | import Data.List (sort) 2 | 3 | type Face = ((Char, Char), (Char, Char)) 4 | 5 | parse :: String -> [String] 6 | parse = tail . lines 7 | 8 | isFace :: Face -> Bool 9 | isFace ((a, b), (c, d)) = (sort [a, b, c, d]) == "acef" 10 | 11 | mkFaces :: [String] -> [Face] 12 | mkFaces = concat . pairWith zip . map (pairWith (,)) 13 | where pairWith f a = zipWith f a (tail a) 14 | 15 | solve :: [String] -> Int 16 | solve = length . filter isFace . mkFaces 17 | 18 | main :: IO () 19 | main = print . solve . parse =<< getContents 20 | -------------------------------------------------------------------------------- /codeforces/549-looksery-cup-2015/D.hs: -------------------------------------------------------------------------------- 1 | parse :: String -> [[Int]] 2 | parse = map (map weight) . tail . lines 3 | where weight 'B' = 1 4 | weight 'W' = -1 5 | 6 | diff :: [[Int]] -> [[Int]] 7 | diff = d (zipWith (-)) . map (d (-)) 8 | where d f l = (zipWith f l (tail l)) ++ [last l] 9 | 10 | solve :: [[Int]] -> Int 11 | solve = length . filter (/= 0) . concat . diff 12 | 13 | main :: IO () 14 | main = print . solve . parse =<< getContents 15 | -------------------------------------------------------------------------------- /codeforces/55-codeforces-beta-round-55/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #55 2 | # Problem A -- Word 3 | s = gets 4 | n = s.length - 1 5 | puts s.split(//).select { |c| c =~ /[[:upper:]]/ }.size > n / 2 ? s.upcase : s.downcase 6 | -------------------------------------------------------------------------------- /codeforces/550-codeforces-round-306/A.hs: -------------------------------------------------------------------------------- 1 | import Data.List (isPrefixOf) 2 | 3 | check :: String -> Bool 4 | check s = recur ["AB", "BA"] s || recur ["BA", "AB"] s 5 | where recur [] _ = True 6 | recur _ [] = False 7 | recur (p : ps) s 8 | | p `isPrefixOf` s = recur ps (drop (length p) s) 9 | | otherwise = recur (p : ps) (tail s) 10 | 11 | format :: Bool -> String 12 | format False = "NO" 13 | format True = "YES" 14 | 15 | main :: IO () 16 | main = putStrLn . format . check =<< getLine 17 | -------------------------------------------------------------------------------- /codeforces/550-codeforces-round-306/B.hs: -------------------------------------------------------------------------------- 1 | subsets :: [a] -> [[a]] 2 | subsets [] = [[]] 3 | subsets (a : as) = ss ++ map (a :) ss 4 | where ss = subsets as 5 | 6 | valid l r x a = l <= s && s <= r && d >= x 7 | where s = sum a 8 | d = (maximum a) - (minimum a) 9 | 10 | solve :: [Int] -> Int 11 | solve (_ : l : r : x : c) = length . filter (valid l r x) . subsets $ c 12 | 13 | main :: IO () 14 | main = print . solve . (map read . words) =<< getContents 15 | -------------------------------------------------------------------------------- /codeforces/550-codeforces-round-306/C.hs: -------------------------------------------------------------------------------- 1 | import Data.Char (ord, chr) 2 | import Data.List (find) 3 | 4 | parse :: String -> [Int] 5 | parse = map ((Prelude.- 48) . ord) 6 | 7 | isSubSeqOf :: (Eq a) => [a] -> [a] -> Bool 8 | isSubSeqOf [] _ = True 9 | isSubSeqOf _ [] = False 10 | isSubSeqOf (p : ps) (s : ss) 11 | | p == s = ps `isSubSeqOf` ss 12 | | otherwise = (p : ps) `isSubSeqOf` ss 13 | 14 | solve :: [Int] -> Maybe Int 15 | solve n = find ((`isSubSeqOf` n) . parse . show) [0,8..999] 16 | 17 | format Nothing = "NO" 18 | format (Just a) = "YES\n" ++ (show a) 19 | 20 | main :: IO () 21 | main = putStrLn . format . solve . parse =<< getLine 22 | -------------------------------------------------------------------------------- /codeforces/552-codeforces-round-308/A.hs: -------------------------------------------------------------------------------- 1 | main :: IO () 2 | main = print . solve . parse =<< getContents 3 | 4 | parse = map parseLine . tail . lines 5 | where parseLine l = let [x1, y1, x2, y2] = map read . words $ l 6 | in (x1, y1, x2, y2) 7 | 8 | solve = sum . map area 9 | where area (x1, y1, x2, y2) = (x2 - x1 + 1) * (y2 - y1 + 1) 10 | -------------------------------------------------------------------------------- /codeforces/552-codeforces-round-308/B.hs: -------------------------------------------------------------------------------- 1 | main :: IO () 2 | main = print . solve . read =<< getContents 3 | 4 | digits :: Integer -> Integer 5 | digits = fromIntegral . length . show 6 | 7 | solve :: Integer -> Integer 8 | solve n = n * l - sum [10 ^ i - 1 | i <- [1..l - 1]] 9 | where l = digits n 10 | -------------------------------------------------------------------------------- /codeforces/552-codeforces-round-308/C.hs: -------------------------------------------------------------------------------- 1 | bool :: a -> a -> Bool -> a 2 | bool f _ False = f 3 | bool _ t True = t 4 | 5 | type Input = (Integer, Integer) 6 | 7 | main :: IO () 8 | main = putStrLn . bool "NO" "YES" . solve . parse =<< getContents 9 | 10 | parse :: String -> (Integer, Integer) 11 | parse contents = (a, b) where [a, b] = map read . words $ contents 12 | 13 | solve :: Input -> Bool 14 | solve (w, m) 15 | | w == 2 = True 16 | | m == 0 = True 17 | | r == 0 = solve (w, m `div` w) 18 | | r == 1 = solve (w, (m - 1) `div` w) 19 | | r == w - 1 = solve (w, (m + 1) `div` w) 20 | | otherwise = False 21 | where r = m `mod` w 22 | -------------------------------------------------------------------------------- /codeforces/552-codeforces-round-308/E.py: -------------------------------------------------------------------------------- 1 | e = raw_input() 2 | 3 | n, result = len(e), eval(e) 4 | for i in xrange(-1, n): 5 | if i == -1 or e[i] == '*': 6 | for j in xrange(i + 1, n + 1): 7 | if j == n or e[j] == '*': 8 | result = max(result, eval(e[:i + 1] + '(' + e[i + 1:j] + ')' + e[j:])) 9 | print(result) 10 | -------------------------------------------------------------------------------- /codeforces/555-codeforces-round-310/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n, k; 7 | scanf("%d%d", &n, &k); 8 | int l = 0; 9 | for (int i = 0; i < k; ++ i) { 10 | int m; 11 | scanf("%d", &m); 12 | int c = 0; 13 | while (m --) { 14 | int a; 15 | scanf("%d", &a); 16 | if (a == c + 1) { 17 | l = std::max(l, ++ c); 18 | } else { 19 | c = 0; 20 | } 21 | } 22 | } 23 | printf("%d\n", (n - l << 1) - k + 1); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /codeforces/573-codeforces-round-318/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int N = 100000; 4 | 5 | int n, a[N]; 6 | 7 | int factor(int n) { 8 | while (n % 2 == 0) { 9 | n /= 2; 10 | } 11 | while (n % 3 == 0) { 12 | n /= 3; 13 | } 14 | return n; 15 | } 16 | 17 | int main() { 18 | scanf("%d", &n); 19 | for (int i = 0; i < n; ++ i) { 20 | scanf("%d", a + i); 21 | } 22 | bool valid = true; 23 | for (int i = 0; i < n; ++ i) { 24 | valid &= factor(a[0]) == factor(a[i]); 25 | } 26 | puts(valid ? "Yes" : "No"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /codeforces/573-codeforces-round-318/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int N = 100000; 5 | 6 | int n, a[N]; 7 | 8 | int main() { 9 | scanf("%d", &n); 10 | for (int i = 0; i < n; ++ i) { 11 | scanf("%d", a + i); 12 | } 13 | a[0] = a[n - 1] = 1; 14 | for (int i = 1; i < n; ++ i) { 15 | a[i] = std::min(a[i], a[i - 1] + 1); 16 | } 17 | for (int i = n - 2; i >= 0; -- i) { 18 | a[i] = std::min(a[i], a[i + 1] + 1); 19 | } 20 | printf("%d\n", *std::max_element(a, a + n)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /codeforces/58-codeforces-beta-round-54/C.cpp: -------------------------------------------------------------------------------- 1 | // Codeforces Beta Round #54 2 | // Problem C -- Trees 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i) 9 | 10 | int n; 11 | std::map count; 12 | 13 | int main() { 14 | scanf("%d", &n); 15 | for (int i = 0; i < n; ++ i) { 16 | int a; 17 | scanf("%d", &a); 18 | int ret = a - std::min(i, n - 1 - i); 19 | if (ret > 0) { 20 | count[ret] ++; 21 | } 22 | } 23 | int answer = 0; 24 | foreach (iter, count) { 25 | answer = std::max(answer, iter->second); 26 | } 27 | printf("%d\n", n - answer); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /codeforces/603-codeforces-round-334/A.hs: -------------------------------------------------------------------------------- 1 | import Data.Char (ord) 2 | 3 | parse :: String -> String 4 | parse = (!! 1) . lines 5 | 6 | solve :: String -> Int 7 | solve s = 1 + ones + min zeros 2 8 | where a = map (\c -> ord c - 48) s 9 | zeros = length . filter id $ zipWith (==) a (tail a) 10 | ones = length a - 1 - zeros 11 | 12 | main :: IO () 13 | main = print . solve . parse =<< getContents 14 | -------------------------------------------------------------------------------- /codeforces/66-codeforces-beta-round-61/D.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #61 2 | # Problem D -- Petya and His Friends 3 | def is_prime(n) 4 | i = 2 5 | while i * i <= n 6 | return false if n % i == 0 7 | i += 1 8 | end 9 | true 10 | end 11 | 12 | n = gets.to_i 13 | 14 | if n == 2 15 | puts -1 16 | else 17 | i = 2 18 | primes = [] 19 | while primes.size < n 20 | primes << i if is_prime(i) 21 | i += 1 22 | end 23 | 24 | product = primes.inject(1) { |p, i| p * i } 25 | puts primes.map { |p| product / p } 26 | end 27 | -------------------------------------------------------------------------------- /codeforces/674-codeforces-round-351/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | int n; 11 | scanf("%d", &n); 12 | std::vector c(n); 13 | for (int i = 0; i < n; ++ i) { 14 | scanf("%d", &c[i]); 15 | c[i] --; 16 | } 17 | std::vector result(n); 18 | for (int i = 0; i < n; ++ i) { 19 | std::vector count(n); 20 | std::pair best(0, 0); 21 | for (int j = i; j < n; ++ j) { 22 | count[c[j]] ++; 23 | best = std::max(best, {count[c[j]], -c[j]}); 24 | result[-best.second] ++; 25 | } 26 | } 27 | for (int i = 0; i < n; ++ i) { 28 | printf("%d%c", result[i], " \n"[i == n - 1]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /codeforces/679-codeforces-round-356/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int PRIMES[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 4, 9, 25, 49}; 5 | 6 | int main() 7 | { 8 | int count = 0; 9 | for (auto&& p : PRIMES) { 10 | 11 | std::cout << p << std::endl; 12 | std::string result; 13 | std::cin >> result; 14 | count += result == "yes"; 15 | } 16 | std::cout << (count >= 2 ? "composite" : "prime") << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /codeforces/700-codeforces-round-364/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n, k; 6 | double l, v1, v2; 7 | scanf("%d%lf%lf%lf%d", &n, &l, &v1, &v2, &k); 8 | (n += k - 1) /= k; 9 | auto v = 1. / (n / (v2 - v1) + (n - 1.) / (v2 + v1)); 10 | printf("%.9f\n", l / (v + v1)); 11 | } 12 | -------------------------------------------------------------------------------- /codeforces/708-aim-tech-round-3/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::string a; 7 | std::cin >> a; 8 | int n = a.size(); 9 | int i = 0; 10 | while (i < n && a[i] == 'a') { 11 | i ++; 12 | } 13 | if (i == n) { 14 | // aaa..a 15 | a[n - 1] = 'z'; 16 | } else { 17 | int j = i; 18 | while (j < n && a[j] != 'a') { 19 | a[j] --; 20 | j ++; 21 | } 22 | } 23 | std::cout << a << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /codeforces/71-codeforces-beta-round-65/A.py: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #65 2 | # Problem A -- Way Too Long Words 3 | for _ in xrange(input()): 4 | word = raw_input() 5 | if len(word) > 10: 6 | print word[0] + str(len(word) - 2) + word[-1] 7 | else: 8 | print word 9 | -------------------------------------------------------------------------------- /codeforces/715-codeforces-round-372/A.py: -------------------------------------------------------------------------------- 1 | n = input() 2 | print 2 3 | for i in xrange(2, n + 1): 4 | print i * (i + 1) * (i + 1) - i + 1 5 | -------------------------------------------------------------------------------- /codeforces/80-codeforces-beta-round-69/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Beta Round #69 2 | # Problem A -- Panoramix's Prediction 3 | def is_prime(n) 4 | 2.upto Math.sqrt(n).to_i do |i| 5 | return false if n % i == 0 6 | end 7 | true 8 | end 9 | 10 | n, m = gets.split.map(&:to_i) 11 | n += 1 12 | n += 1 until is_prime(n) 13 | puts n == m ? "YES" : "NO" 14 | -------------------------------------------------------------------------------- /codeforces/81-yandex-algorithm-open-2011-qualification-1/A.cpp: -------------------------------------------------------------------------------- 1 | // Yandex Algorithm Open 2011 Qualification 1 2 | // Problem A -- Plug-in 3 | #include 4 | #include 5 | 6 | const int N = 200000 + 1; 7 | 8 | char string[N]; 9 | 10 | int main() { 11 | scanf("%s", string); 12 | int size = 0; 13 | for (int i = 0; string[i]; ++ i) { 14 | if (size && string[size - 1] == string[i]) { 15 | size --; 16 | } else { 17 | string[size ++] = string[i]; 18 | } 19 | } 20 | string[size] = '\0'; 21 | puts(string); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /codeforces/82-yandex-algorithm-2011-qualification-2/A.py: -------------------------------------------------------------------------------- 1 | # Yandex.Algorithm 2011 Qualification 2 2 | # Problem A -- Double Cola 3 | names = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"] 4 | n = input() 5 | while n > 5: 6 | n = ((n - 5) + 1) >> 1 7 | print names[n - 1] 8 | -------------------------------------------------------------------------------- /codeforces/833-codeforces-round-426/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool solve(long long a, long long b) 5 | { 6 | long long r = pow(a * b, 1. / 3.) + 1; 7 | while (r * r * r > a * b) { 8 | r --; 9 | } 10 | return r * r * r == a * b && a % r == 0 && b % r == 0; 11 | } 12 | 13 | int main() 14 | { 15 | int T; 16 | scanf("%d", &T); 17 | while (T --) { 18 | int a, b; 19 | scanf("%d%d", &a, &b); 20 | puts(solve(a, b) ? "Yes" : "No"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /codeforces/84-codeforces-beta-round-72/A.pl: -------------------------------------------------------------------------------- 1 | # Codeforces Beat Round #72 2 | # Problem A -- Chips 3 | $n = <>; 4 | $result = ($n / 2 * 3); 5 | print "$result\n"; 6 | -------------------------------------------------------------------------------- /codeforces/86-yandex-algorithm-2011-round-2/A.rb: -------------------------------------------------------------------------------- 1 | # Yandex Algorithm 2011 Round 2 2 | # Problem A -- Reflection 3 | l, r = gets.split.map(&:to_i) 4 | a, b, answer = 1, 9, 0 5 | while a <= r 6 | c, d = [l, a].max, [r, b].min 7 | if c <= d 8 | answer = [(b - c) * c, (b - d) * d, answer].max 9 | if c <= b / 2 and b / 2 <= d 10 | answer = [(b - b / 2) * (b / 2), answer].max 11 | end 12 | end 13 | a *= 10 14 | b *= 10 15 | b += 9 16 | end 17 | puts answer 18 | -------------------------------------------------------------------------------- /codeforces/908-good-bye-2017/A.py: -------------------------------------------------------------------------------- 1 | count = 0 2 | for c in input(): 3 | if c in 'aeiou13579': 4 | count += 1 5 | print(count) 6 | -------------------------------------------------------------------------------- /codeforces/908-good-bye-2017/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int N = 1000; 6 | 7 | int n, r, x[N]; 8 | double y[N]; 9 | 10 | int main() 11 | { 12 | scanf("%d%d", &n, &r); 13 | for (int i = 0; i < n; ++ i) { 14 | scanf("%d", x + i); 15 | } 16 | for (int i = 0; i < n; ++ i) { 17 | double& yy = y[i] = r; 18 | for (int j = 0; j < i; ++ j) { 19 | int d = std::abs(x[i] - x[j]); 20 | if (d <= r + r) { 21 | yy = std::max(yy, y[j] + sqrt(4 * r * r - d * d)); 22 | } 23 | } 24 | } 25 | for (int i = 0; i < n; ++ i) { 26 | printf("%.8f\n", y[i]); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /codeforces/91-codeforces-beta-round-75/A.rb: -------------------------------------------------------------------------------- 1 | # Codeforces Beat Round #75 2 | # Problem A -- Chips 3 | n, m = gets.split.map(&:to_i) 4 | i = 1 5 | while i <= m do 6 | m -= i 7 | i = 1 + i % n 8 | end 9 | puts m 10 | -------------------------------------------------------------------------------- /codeforces/96-codeforces-beta-round-77/A.hs: -------------------------------------------------------------------------------- 1 | -- Codeforces Beta Round #77 2 | -- Problem A -- Football 3 | import Data.List 4 | main = interact work 5 | 6 | work :: String -> String 7 | work input = if (maximum . map length . group) input >= 7 then "YES" else "NO" 8 | -------------------------------------------------------------------------------- /google-code-jam/qualification-round-2012/A/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char mapping[26] = {24, 7, 4, 18, 14, 2, 21, 23, 3, 20, 8, 6, 11, 1, 10, 17, 25, 19, 13, 22, 9, 15, 5, 12, 0, 16}; 7 | 8 | int main() { 9 | int test_count; 10 | scanf("%d", &test_count); 11 | char buffer[111]; 12 | fgets(buffer, 111, stdin); 13 | for (int test = 1; test <= test_count; ++ test) { 14 | char string[111]; 15 | fgets(string, 111, stdin); 16 | int length = strlen(string); 17 | for (int i = 0; i < length; ++ i) { 18 | if (isalpha(string[i])) { 19 | string[i] = mapping[string[i] - 'a'] + 'a'; 20 | } 21 | } 22 | printf("Case #%d: %s", test, string); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /google-code-jam/qualification-round-2012/A/A.py: -------------------------------------------------------------------------------- 1 | # Qualification Round 2012 2 | # Problem A -- Speaking in Tongues 3 | mapping = [24, 7, 4, 18, 14, 2, 21, 23, 3, 20, 8, 6, 11, 1, 10, 17, 25, 19, 13, 22, 9, 15, 5, 12, 0, 16] 4 | 5 | def translate(text): 6 | return ''.join(map(lambda c: chr(ord('a') + mapping[ord(c) - ord('a')]) if c != ' ' else ' ', text)) 7 | 8 | test_count = input() 9 | for test in xrange(test_count): 10 | print "Case #%d: %s" %(test + 1, translate(raw_input())) 11 | 12 | -------------------------------------------------------------------------------- /google-code-jam/qualification-round-2012/A/A.text: -------------------------------------------------------------------------------- 1 | a zoo ejp mysljylc kd kxveddknmc re jsicpdrysi rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd de kr kd eoya kw aej tysr re ujdr lkgc jv 2 | y qee our language is impossible to understand there are twenty six factorial possibilities so it is okay if you want to just give up 3 | -------------------------------------------------------------------------------- /google-code-jam/qualification-round-2012/A/parser.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | a = raw_input() 3 | b = raw_input() 4 | mapping = [-1] * 26 5 | for x, y in zip(a, b): 6 | if x.isalpha() and y.isalpha(): 7 | mapping[ord(x) - ord('a')] = ord(y) - ord('a') 8 | mapping[ord('q') - ord('a')] = ord('z') - ord('a') 9 | print mapping 10 | #for k, v in enumerate(mapping): 11 | # print chr(ord('a') + k), chr(ord('a') + v) 12 | -------------------------------------------------------------------------------- /google-code-jam/qualification-round-2012/C/C.py: -------------------------------------------------------------------------------- 1 | # Qualification Round 2012 2 | # Problem C -- Recycled Numbers 3 | test_count = input() 4 | for test in xrange(test_count): 5 | a, b = map(int, raw_input().split()) 6 | times = 1 7 | while times * 10 <= b: 8 | times *= 10 9 | result = 0 10 | for x in xrange(a, b + 1): 11 | numbers = [] 12 | i = x 13 | while True: 14 | r = i % 10 15 | i = (i - r) / 10 + r * times 16 | if a <= i <= b: 17 | numbers.append(i) 18 | if i == x: 19 | break 20 | if min(numbers) == x: 21 | n = len(set(numbers)) 22 | result += n * (n - 1) / 2 23 | print "Case #%d: %d" %(test + 1, result) 24 | -------------------------------------------------------------------------------- /google-code-jam/round-1a-2012/A/A.py: -------------------------------------------------------------------------------- 1 | # Round 1A 2012 2 | # Problem A -- Password Problem 3 | def solve(): 4 | m, n = map(int, raw_input().split()) 5 | p = map(float, raw_input().split()) 6 | ps = [1.0] 7 | for i in xrange(m): 8 | ps.append(ps[-1] * p[i]) 9 | result = n + 2 # give up 10 | for d in xrange(m + 1): 11 | expected = (d + n - (m - d) + 1) \ 12 | + (1 - ps[m - d]) * (n + 1) 13 | result = min(result, expected) 14 | return result 15 | 16 | test_count = input() 17 | for test in xrange(test_count): 18 | print "Case #%d: %.6f" %(test + 1, solve()) 19 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/2012年汕头市选成绩.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2012-03-13-stoi-2012/2012年汕头市选成绩.xlsx -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2012-03-13-stoi-2012/senior/makers.zip -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/A-50.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int N = 111111; 6 | const int MOD = 1000000007; 7 | 8 | int n, a[N]; 9 | 10 | int multiply(int a, int b) { 11 | return (long long)a * b % MOD; 12 | } 13 | 14 | int main() { 15 | scanf("%d", &n); 16 | for (int i = 1; i <= n; ++ i) { 17 | scanf("%d", a + i); 18 | } 19 | for (int i = 1; i <= n; ++ i) { 20 | int result = 1; 21 | for (int j = 1; j <= n; ++ j) { 22 | if (i != j) { 23 | result = multiply(result, a[j]); 24 | } 25 | } 26 | printf("%d%c", result, i == n? '\n': ' '); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/Agen.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from sys import argv 3 | from random import randint 4 | 5 | rand = lambda n: randint(n - n / 3, n) 6 | 7 | n = rand(int(argv[1])) 8 | print n 9 | print " ".join(map(str, (rand(10 ** 9) for i in xrange(n)))) 10 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/Atest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # test_count max_n 3 | make A 4 | make A-50 5 | for count in `seq $1`; do 6 | # echo Test \#$count 7 | ./Agen.py $2 | tee A.in > /dev/null 8 | cat A.in | ./A | tee A.out > /dev/null 9 | cat A.in | ./A-50 | tee A-50.out > /dev/null 10 | if ! diff A.out A-50.out; then 11 | break 12 | fi 13 | done 14 | echo "All test(s) OK" 15 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/Bgen.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from sys import argv 3 | from random import randint 4 | 5 | rand = lambda n: randint(n - n / 3, n) 6 | 7 | limit = int(argv[1]) 8 | 9 | n = rand(limit) 10 | m = rand(limit) 11 | print n, m, rand(n), randint(1, n), randint(1, n) 12 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/Btest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # test_count max_n 3 | make B 4 | make B-50 5 | for count in `seq $1`; do 6 | # echo Test \#$count 7 | ./Bgen.py $2 | tee B.in > /dev/null 8 | cat B.in | ./B | tee B.out > /dev/null 9 | cat B.in | ./B-50 | tee B-50.out > /dev/null 10 | if ! diff B.out B-50.out; then 11 | break 12 | fi 13 | done 14 | echo "All test(s) OK" 15 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/Cgen.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from sys import argv 3 | from random import randint, choice 4 | 5 | rand = lambda n: randint(n - n / 3, n) 6 | 7 | limit = int(argv[1]) 8 | result = int(argv[2]) 9 | 10 | n = rand(limit) 11 | if result == 0: 12 | print "".join([choice(['a', 'b', 'c'])] * n) 13 | else: 14 | print "".join([choice(['a', 'b', 'c']) for i in xrange(n)]) 15 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/Ctest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # test_count max_n 3 | make C 4 | make C-50 5 | for count in `seq $1`; do 6 | echo Test \#$count 7 | ./Cgen.py $2 1 | tee C.in > /dev/null 8 | cat C.in | ./C | tee C.out > /dev/null 9 | cat C.in | ./C-50 | tee C-50.out > /dev/null 10 | if ! diff C.out C-50.out; then 11 | break 12 | fi 13 | done 14 | echo "All test(s) OK" 15 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/D-30.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int N = 111111; 7 | 8 | int n, m, a[N]; 9 | 10 | int main() { 11 | scanf("%d%d", &n, &m); 12 | for (int i = 1; i <= n; ++ i) { 13 | scanf("%d", a + i); 14 | } 15 | while (m --) { 16 | int l, r, b; 17 | scanf("%d%d%d", &l, &r, &b); 18 | int result = 0; 19 | for (int j = l; j <= r; ++ j) { 20 | result = max(result, a[j] ^ b); 21 | } 22 | printf("%d\n", result); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/Dgen.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from sys import argv 3 | from random import randint 4 | 5 | rand = lambda n: randint(n - n / 3, n) 6 | 7 | limit = int(argv[1]) 8 | value = 10 ** 4 9 | 10 | n = m = limit 11 | print n, m 12 | print " ".join(map(str, (randint(0, value) for i in xrange(n)))) 13 | for i in xrange(m): 14 | a = randint(1, n - n / 4) 15 | b = randint(a + n / 4, n) 16 | print a, b, randint(0, value) 17 | -------------------------------------------------------------------------------- /my-contests/2012-03-13-stoi-2012/senior/makers/Dtest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # test_count max_n 3 | make D 4 | make D-30 5 | for count in `seq $1`; do 6 | echo Test \#$count 7 | ./Dgen.py $2 | tee D.in > /dev/null 8 | cat D.in | ./D | tee D.out > /dev/null 9 | cat D.in | ./D-30 | tee D-30.out > /dev/null 10 | if ! diff D.out D-30.out; then 11 | break 12 | fi 13 | done 14 | echo "All test(s) OK" 15 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/checkers/acmp.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | #include 3 | #include 4 | 5 | const double EPS = 1.5E-6; 6 | 7 | int main(int argc, char * argv[]) 8 | { 9 | setName("compare two doubles, maximal absolute error = %.10lf", EPS); 10 | registerTestlibCmd(argc, argv); 11 | 12 | double ja = ans.readDouble(); 13 | double pa = ouf.readDouble(); 14 | 15 | if (fabs(ja - pa) > EPS + 1E-15) 16 | quitf(_wa, "expected %.10lf, found %.10lf", ja, pa); 17 | 18 | quitf(_ok, "answer is %.10lf", ja); 19 | } 20 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/checkers/dcmp.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | #include 3 | #include 4 | 5 | const double EPS = 1E-6; 6 | 7 | int main(int argc, char * argv[]) 8 | { 9 | setName("compare two doubles, maximal absolute or relative error = %.10lf", EPS); 10 | registerTestlibCmd(argc, argv); 11 | 12 | double ja = ans.readDouble(); 13 | double pa = ouf.readDouble(); 14 | 15 | if (!doubleCompare(ja, pa, EPS)) 16 | quitf(_wa, "expected %.10lf, found %.10lf", ja, pa); 17 | 18 | quitf(_ok, "answer is %.10lf", ja); 19 | } 20 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/checkers/icmp.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | #include 3 | 4 | int main(int argc, char * argv[]) 5 | { 6 | setName("compare two signed int%d's", 8 * sizeof(int)); 7 | registerTestlibCmd(argc, argv); 8 | 9 | int ja = ans.readInt(); 10 | int pa = ouf.readInt(); 11 | 12 | if (ja != pa) 13 | quitf(_wa, "expected %d, found %d", ja, pa); 14 | 15 | quitf(_ok, "answer is %d", ja); 16 | } 17 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/checkers/pointscmp.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | using namespace std; 4 | 5 | int main(int argc, char * argv[]) 6 | { 7 | setName("example of scored checker"); 8 | registerTestlibCmd(argc, argv); 9 | 10 | double ja = ans.readDouble(); 11 | double pa = ouf.readDouble(); 12 | 13 | quitp(fabs(ja - pa), "ja=%.4lf pa=%.4lf", ja, pa); 14 | } 15 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/checkers/rcmp.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | #include 3 | #include 4 | 5 | const double EPS = 1.5E-6; 6 | 7 | int main(int argc, char * argv[]) 8 | { 9 | setName("compare two doubles, maximal absolute error = %.10lf", EPS); 10 | registerTestlibCmd(argc, argv); 11 | 12 | double ja = ans.readDouble(); 13 | double pa = ouf.readDouble(); 14 | 15 | if (fabs(ja - pa) > EPS + 1E-15) 16 | quitf(_wa, "expected %.10lf, found %.10lf", ja, pa); 17 | 18 | quitf(_ok, "answer is %.10lf", ja); 19 | } 20 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/checkers/rncmp.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | const double EPS = 1.5E-5; 7 | 8 | int main(int argc, char * argv[]) 9 | { 10 | setName("compare two sequences of doubles, maximal absolute error = %.10lf", EPS); 11 | registerTestlibCmd(argc, argv); 12 | 13 | int n = 0; 14 | while (!ans.seekEof()) 15 | { 16 | n++; 17 | double j = ans.readDouble(); 18 | double p = ouf.readDouble(); 19 | if (fabs(j - p) > EPS + 1E-15) 20 | quitf(_wa, "%d%s numbers differ - expected: '%.10lf', found: '%.10lf'", n, englishEnding(n).c_str(), j, p); 21 | } 22 | 23 | quitf(_ok, "%d numbers", n); 24 | } 25 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/docs/read.me: -------------------------------------------------------------------------------- 1 | TESTLIB 2 | 3 | == What is it? == 4 | Testlib is simple library which helps you to write 5 | * checkers 6 | * validators 7 | * generators 8 | for programming competitions problems. 9 | You can find latest release of the library on http://code.google.com/p/testlib/ 10 | Problem development management system Polygon completely supports testlib. 11 | 12 | == How to use? == 13 | Easest way is to read c++ sources in the checkers/, validators/ and generators/ folders. 14 | Also some classes and methods in testlib have documentation. 15 | 16 | Thanks for using testlib, 17 | Mike Mirzayanov 18 | 19 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/generators/bgen.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Outputs random 100-digits binary string mostly containing 0's. 3 | * In average it contrains only 10% of 1's. 4 | * 5 | * To generate different values, call "bgen.exe" with different parameters. 6 | * 7 | * It is typical behaviour of testlib generator to setup randseed by command line. 8 | */ 9 | 10 | #include "testlib.h" 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main(int argc, char* argv[]) 16 | { 17 | registerGen(argc, argv, 1); 18 | 19 | cout << rnd.next("[0000000001]{100}") << endl; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/generators/igen.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Outputs random number between 1 and 10^6, inclusive. 3 | * To generate different values, call "igen.exe" with different parameters. 4 | * For example, "igen.exe 1" returns 504077, but "igen.exe 3" returns 808747. 5 | * 6 | * It is typical behaviour of testlib generator to setup randseed by command line. 7 | */ 8 | 9 | #include "testlib.h" 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main(int argc, char* argv[]) 15 | { 16 | registerGen(argc, argv, 1); 17 | 18 | cout << rnd.next(1, 1000000) << endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/generators/sgen.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Outputs random token. 3 | * 4 | * Token contains latin letters and digits and have length 5 | * between 1 and 1000 characters, inclusive. 6 | * 7 | * To generate different values, call "sgen.exe" with different parameters. 8 | * 9 | * It is typical behaviour of testlib generator to setup randseed by command line. 10 | */ 11 | 12 | #include "testlib.h" 13 | #include 14 | 15 | using namespace std; 16 | 17 | int main(int argc, char* argv[]) 18 | { 19 | registerGen(argc, argv, 1); 20 | 21 | cout << rnd.next("[a-zA-Z0-9]{1,1000}") << endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/stress.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | count=0 3 | while true; do 4 | count=$(($count + 1)) 5 | ./random $count $@ > input 6 | ./solution < input > output.1 7 | ./slow < input > output.2 8 | cat output.1 9 | if diff output.1 output.2; then 10 | echo Passed \#$count 11 | else 12 | echo Failed \#$count 13 | break 14 | fi 15 | done 16 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/validators/ival.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Validates that input contains the only integer between 1 and 100, inclusive. 3 | * Also validates that file ends with EOLN and EOF. 4 | */ 5 | 6 | #include "testlib.h" 7 | 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | registerValidation(); 13 | 14 | inf.readInt(1, 100, "n"); 15 | inf.readEoln(); 16 | inf.readEof(); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/validators/nval.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Validates that the first line contains the integer between 1 and 10^5, inclusive. 3 | * The second line should contains space-separated sequence of integers between -10^15 and 10^15, inclusive. 4 | * Also validates that file ends with EOLN and EOF. 5 | */ 6 | 7 | #include "testlib.h" 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | registerValidation(); 14 | 15 | int n = inf.readInt(1, 100000, "n"); 16 | inf.readEoln(); 17 | 18 | for (int i = 0; i < n; i++) 19 | { 20 | inf.readLong(-1000000000000000LL, 1000000000000000LL, format("a[%d]", i + 1)); 21 | 22 | if (i + 1 < n) 23 | inf.readSpace(); 24 | } 25 | inf.readEoln(); 26 | 27 | inf.readEof(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/lib/validators/sval.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Validates that the input contains the only token token. 3 | * This token can contain only lowercase latin letters a-z. The length should be between 1 and 100, inclusive. 4 | * Also validates that file ends with EOLN and EOF. 5 | */ 6 | 7 | #include "testlib.h" 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | registerValidation(); 14 | 15 | inf.readToken("[a-z]{1,100}", "s"); 16 | inf.readEoln(); 17 | inf.readEof(); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/carry/src/001.hand: -------------------------------------------------------------------------------- 1 | 2 2 | 5 5 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/carry/src/002.hand: -------------------------------------------------------------------------------- 1 | 10 2 | 0 1 2 3 4 5 6 7 8 9 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/carry/src/003.hand: -------------------------------------------------------------------------------- 1 | 2 2 | 0 0 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/carry/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'validator.cpp' 8 | make 'solution.cpp' 9 | 10 | sh 'mkdir -p ../tests' 11 | 12 | test '001.hand' 13 | test '002.hand' 14 | test '003.hand' 15 | [100, 1000, 100000].each do |n| 16 | [1000, 1000000, 1000000000].each do |m| 17 | test :random, '?', n, m 18 | end 19 | end 20 | 21 | sh 'rm -rf *.exe' 22 | end 23 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/carry/src/random.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | registerGen(argc, argv, 1); 6 | int n = std::atoi(argv[2]); 7 | int m = std::atoi(argv[3]); 8 | printf("%d\n", n); 9 | for (int i = 0; i < n; ++ i) { 10 | printf("%d%c", rnd.next(0, m), " \n"[i == n - 1]); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/carry/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | int main() 4 | { 5 | registerValidation(); 6 | int n = inf.readInt(2, 100000); 7 | inf.readEoln(); 8 | for (int i = 0; i < n; ++ i) { 9 | inf.readInt(0, 1000000000); 10 | if (i + 1 < n) { 11 | inf.readSpace(); 12 | } else { 13 | inf.readEoln(); 14 | } 15 | } 16 | inf.readEof(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/001.hand: -------------------------------------------------------------------------------- 1 | abc 2 | aaabcbc 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/002.hand: -------------------------------------------------------------------------------- 1 | b 2 | bbb 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/003.hand: -------------------------------------------------------------------------------- 1 | abc 2 | ab 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/004.hand: -------------------------------------------------------------------------------- 1 | a 2 | a 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'random_ab.cpp' 8 | make 'random_fib.cpp' 9 | make 'validator.cpp' 10 | make 'solution.cpp' 11 | 12 | sh 'mkdir -p ../tests' 13 | 14 | test '001.hand' 15 | test '002.hand' 16 | test '003.hand' 17 | test '004.hand' 18 | test :random, '?', 1000, 5000000 19 | test :random_ab, '?', 1000, 5000000 20 | test :random_fib, '?', 1000, 5000000 21 | test :random, '?', 100000, 5000000 22 | test :random_ab, '?', 100000, 5000000 23 | test :random_fib, '?', 100000, 5000000 24 | 25 | sh 'rm -rf *.exe' 26 | end 27 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/problem.md: -------------------------------------------------------------------------------- 1 | # Censor 2 | 3 | frog is now a editor to censor so-called sensitive words (敏感词). 4 | 5 | She has a long text $p$. 6 | Her job is relatively simple -- 7 | just to find the first occurence of sensitive word $w$ and remove it. 8 | 9 | frog repeats over and over again. 10 | Help her do the tedious work. 11 | 12 | ## Input 13 | 14 | The input consists of multiple tests. For each test: 15 | 16 | The first line contains $1$ string $w$. 17 | The second line contains $1$ string $p$. 18 | 19 | ($1 \leq \textrm{length of}\ w, p \leq 5 \cdot 10^6$, 20 | $w, p$ consists of only lowercase letter) 21 | 22 | ## Output 23 | 24 | For each test, write $1$ string which denotes the censored text. 25 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/random.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/random.exe -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/random_ab.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | #include 4 | 5 | int main(int argc, char* argv[]) 6 | { 7 | registerGen(argc, argv, 1); 8 | int n = std::atoi(argv[2]); 9 | int m = std::atoi(argv[3]); 10 | for (int i = 0; i < n; ++ i) { 11 | putchar('a'); 12 | } 13 | puts(""); 14 | for (int i = 0; i < m; ++ i) { 15 | putchar("ba"[std::min(rnd.next(0, m / n), 1)]); 16 | } 17 | puts(""); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/random_ab.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/random_ab.exe -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/random_fib.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | #include 4 | #include 5 | 6 | std::string cut(const std::string &source, int n) 7 | { 8 | int p = rnd.next(0, (int)source.length() - n); 9 | return source.substr(p, n); 10 | } 11 | 12 | int main(int argc, char* argv[]) 13 | { 14 | registerGen(argc, argv, 1); 15 | int n = std::atoi(argv[2]); 16 | int m = std::atoi(argv[3]); 17 | std::string fib = [&]() { 18 | std::string p = "a"; 19 | std::string q = "b"; 20 | while ((int)q.size() < n + m) { 21 | p += q; 22 | std::swap(p, q); 23 | } 24 | return q; 25 | }(); 26 | puts(cut(fib, n).c_str()); 27 | puts(cut(fib, m).c_str()); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/random_fib.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/random_fib.exe -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/slow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int N = 5000000; 5 | 6 | char p[N + 1], t[N + 1]; 7 | 8 | int main() 9 | { 10 | while (scanf("%s%s", p, t) == 2) { 11 | int n = strlen(p); 12 | while (true) { 13 | char* it = strstr(t, p); 14 | if (it == NULL) { 15 | break; 16 | } 17 | for (it += n; *it; ++ it) { 18 | *(it - n) = *it; 19 | } 20 | *(it - n) = '\0'; 21 | } 22 | puts(t); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/solution.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/solution.exe -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/stress.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | count=0 3 | while true; do 4 | count=$(($count + 1)) 5 | ./random $count $@ > input 6 | ./solution < input > output.1 7 | ./slow < input > output.2 8 | cat output.1 9 | if diff output.1 output.2; then 10 | echo Passed \#$count 11 | else 12 | echo Failed \#$count 13 | break 14 | fi 15 | done 16 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | int main() 4 | { 5 | registerValidation(); 6 | inf.readToken("[a-z]{1,5000000}"); 7 | inf.readEoln(); 8 | inf.readToken("[a-z]{1,5000000}"); 9 | inf.readEoln(); 10 | inf.readEof(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/validator.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/problems/censor/src/validator.exe -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/cover/src/001.hand: -------------------------------------------------------------------------------- 1 | 3 2 2 | 1 2 3 | 1 3 4 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/cover/src/002.hand: -------------------------------------------------------------------------------- 1 | 6 5 2 | 1 2 3 | 1 3 4 | 1 4 5 | 2 5 6 | 2 6 7 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/cover/src/003.hand: -------------------------------------------------------------------------------- 1 | 2 1 2 | 1 2 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/cover/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'validator.cpp' 8 | make 'solution.cpp' 9 | 10 | sh 'mkdir -p ../tests' 11 | 12 | test '001.hand' 13 | test '002.hand' 14 | test '003.hand' 15 | [1,3,10,12,14,21,23,30,32,34,36,38,40,43,45,51,58,60,62,69,71,73,75,77,84,88,92,97,101,104,105,106,108,112,116,119,121,123,125,127,128,129,131,132,134,136,144,149,151,153].each do |seed| 16 | test :random, seed, 500, 30, 1000 17 | end 18 | 19 | sh 'rm -rf *.exe' 20 | end 21 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/cover/src/problem.md: -------------------------------------------------------------------------------- 1 | # Vertex Cover 2 | 3 | 4 | frog has a graph with $n$ vertices $v(1), v(2), \dots, v(n)$ 5 | and $m$ edges $(v(a_1), v(b_1)), (v(a_2), v(b_2)), \dots, (v(a_m), v(b_m))$. 6 | 7 | She would like to color some vertices 8 | so that each edge has at least one colored vertex. 9 | 10 | Find the minimum number of colored vertices. 11 | 12 | ## Input 13 | 14 | The input consists of multiple tests. For each test: 15 | 16 | The first line contains $2$ integers $n, m$ 17 | ($2 \leq n \leq 500, 1 \leq m \leq \frac{n(n - 1)}{2}$). 18 | Each of the following $m$ lines contains $2$ integers $a_i, b_i$ 19 | ($1 \leq a_i, b_i \leq n, a_i \neq b_i, \min\{a_i, b_i\} \leq 30$) 20 | 21 | ## Output 22 | 23 | For each test, write $1$ integer which denotes the minimum number of colored vertices. 24 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/cover/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | registerValidation(); 8 | int n = inf.readInt(2, 500); 9 | inf.readSpace(); 10 | int m = inf.readInt(1, n * (n - 1) / 2); 11 | inf.readEoln(); 12 | for (int i = 0; i < m; ++ i) { 13 | int a = inf.readInt(1, n); 14 | inf.readSpace(); 15 | int b = inf.readInt(1, n); 16 | inf.readEoln(); 17 | ensure(std::min(a, b) <= 30); 18 | } 19 | inf.readEof(); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/doall.sh: -------------------------------------------------------------------------------- 1 | for p in carry censor cover easy necklace party rectangle rmq travel turn; do 2 | cd $p/src 3 | sh doall.sh 4 | cd ../../ 5 | done 6 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/001.hand: -------------------------------------------------------------------------------- 1 | 2 2 | 1 4 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/002.hand: -------------------------------------------------------------------------------- 1 | 2 2 | 2 3 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/003.hand: -------------------------------------------------------------------------------- 1 | 1 2 | 0 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/004.hand: -------------------------------------------------------------------------------- 1 | 1 2 | 1000000000 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'validator.cpp' 8 | make 'solution.cpp' 9 | 10 | sh 'mkdir -p ../tests' 11 | 12 | test '001.hand' 13 | test '002.hand' 14 | test '003.hand' 15 | test '004.hand' 16 | test '005.hand' 17 | 10.times { test :random, '?', 100000, 1000000000 } 18 | 19 | sh 'rm -rf *.exe' 20 | end 21 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/problem.md: -------------------------------------------------------------------------------- 1 | # Easy Math 2 | 3 | Given $n$ integers $a_1, a_2, \dots, a_n$, 4 | check if the sum of their square root $\sqrt{a_1} + \sqrt{a_2} + \dots + \sqrt{a_n}$ is a integer. 5 | 6 | ## Input 7 | 8 | The input consists of multiple tests. For each test: 9 | 10 | The first line contains $1$ integer $n$ ($1 \leq n \leq 10^5$). 11 | The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10^9$). 12 | 13 | ## Output 14 | 15 | For each test, write "`Yes`" if the sum is a integer, or "`No`" otherwise. 16 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/random.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | #include 4 | 5 | int main(int argc, char* argv[]) 6 | { 7 | registerGen(argc, argv, 1); 8 | int n = std::atoi(argv[2]); 9 | int m = std::atoi(argv[3]); 10 | std::vector a; 11 | for (int i = 0; i * i <= m; ++ i) { 12 | a.push_back(i * i); 13 | } 14 | printf("%d\n", n); 15 | int t = rnd.next(0, 1); 16 | for (int i = 0; i < n; ++ i) { 17 | int x = t ? a[rnd.next(0, (int)a.size() - 1)] : rnd.next(0, m); 18 | printf("%d%c", x, " \n"[i == n - 1]); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::map sqrt; 7 | for (int i = 0; i * i <= 1000000000; ++ i) { 8 | sqrt[i * i] = i; 9 | } 10 | int n; 11 | while (scanf("%d", &n) == 1) { 12 | bool valid = true; 13 | for (int i = 0; i < n; ++ i) { 14 | int a; 15 | scanf("%d", &a); 16 | valid &= sqrt.count(a); 17 | } 18 | puts(valid ? "Yes" : "No"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/easy/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | int main() 4 | { 5 | registerValidation(); 6 | int n = inf.readInt(1, 100000); 7 | inf.readEoln(); 8 | for (int i = 0; i < n; ++ i) { 9 | inf.readInt(0, 1000000000); 10 | if (i + 1 < n) { 11 | inf.readSpace(); 12 | } else { 13 | inf.readEoln(); 14 | } 15 | } 16 | inf.readEof(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/necklace/src/001.hand: -------------------------------------------------------------------------------- 1 | 6 2 | 10000 3 2 4 2 3 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/necklace/src/002.hand: -------------------------------------------------------------------------------- 1 | 2 2 | 10000 10000 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/necklace/src/003.hand: -------------------------------------------------------------------------------- 1 | 1 2 | 10000 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/necklace/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'validator.cpp' 8 | make 'solution.cpp' 9 | 10 | sh 'mkdir -p ../tests' 11 | 12 | test '001.hand' 13 | test '002.hand' 14 | test '003.hand' 15 | [100, 1000, 100000].each do |n| 16 | [10, 100, 10000].each do |m| 17 | [1, 10].each do |k| 18 | test :random, '?', n, m, k 19 | end 20 | end 21 | end 22 | 23 | sh 'rm -rf *.exe' 24 | end 25 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/necklace/src/random.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | const int MAX_VALUE = 10000; 4 | 5 | int main(int argc, char* argv[]) 6 | { 7 | registerGen(argc, argv, 1); 8 | int n = std::atoi(argv[2]); 9 | int m = std::atoi(argv[3]); 10 | int k = std::atoi(argv[4]); 11 | std::vector a; 12 | for (int i = 0; i < n; ++ i) { 13 | a.push_back(rnd.next(0, m - 1)); 14 | } 15 | for (int i = 0; i < k; ++ i) { 16 | a[rnd.next(0, n - 1)] = MAX_VALUE; 17 | } 18 | printf("%d\n", n); 19 | for (int i = 0; i < n; ++ i) { 20 | printf("%d%c", a[i], " \n"[i == n - 1]); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/necklace/src/validator: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/problems/necklace/src/validator -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/necklace/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | const int MAX_VALUE = 10000; 4 | 5 | int main() 6 | { 7 | registerValidation(); 8 | int n = inf.readInt(1, 100000); 9 | inf.readEoln(); 10 | int m = 0; 11 | for (int i = 0; i < n; ++ i) { 12 | int v = inf.readInt(0, MAX_VALUE); 13 | if (i + 1 < n) { 14 | inf.readSpace(); 15 | } else { 16 | inf.readEoln(); 17 | } 18 | m += (v == MAX_VALUE); 19 | } 20 | ensure(1 <= m && m <= 10); 21 | inf.readEof(); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/party/src/001.hand: -------------------------------------------------------------------------------- 1 | 2 1 2 | 1 1 3 | 3 3 4 | 1 2 5 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/party/src/002.hand: -------------------------------------------------------------------------------- 1 | 2 1 2 | 1 1 3 | 2 2 4 | 1 2 5 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/party/src/003.hand: -------------------------------------------------------------------------------- 1 | 3 2 2 | 2 1 2 3 | 1 3 2 4 | 1 2 5 | 2 3 6 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/party/src/004.hand: -------------------------------------------------------------------------------- 1 | 1 0 2 | 1 3 | 1 4 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/party/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'validator.cpp' 8 | make 'solution.cpp' 9 | 10 | sh 'mkdir -p ../tests' 11 | 12 | test '001.hand' 13 | test '002.hand' 14 | test '003.hand' 15 | test '003.hand' 16 | [[10, 20], [100, 200], [100, 1000], [1000, 2000], [1000, 10000]].each do |n, m| 17 | [2, 10, 1000000].each do |k| 18 | test :random, '?', n, m, k 19 | end 20 | end 21 | 22 | sh 'rm -rf *.exe' 23 | end 24 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/001.hand: -------------------------------------------------------------------------------- 1 | 2 2 6 2 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/002.hand: -------------------------------------------------------------------------------- 1 | 1 1 0 2 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/003.hand: -------------------------------------------------------------------------------- 1 | 50000 50000 1000000000 2 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'validator.cpp' 8 | make 'solution.cpp' 9 | 10 | sh 'mkdir -p ../tests' 11 | 12 | test '001.hand' 13 | test '002.hand' 14 | test '003.hand' 15 | [10, 100, 1000, 50000].each do |n| 16 | 5.times { test :random, '?', n } 17 | end 18 | 19 | sh 'rm -rf *.exe' 20 | end 21 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/figure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/figure.png -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/figure.sketch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/figure.sketch -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/problem.md: -------------------------------------------------------------------------------- 1 | # Rectangle 2 | 3 | frog has a piece of paper divided into $n$ rows and $m$ columns. 4 | Today, she would like to draw a rectangle whose perimeter is not greater than $k$. 5 | 6 | ![There are $8$ (out of $9$) ways when $n = m = 2, k = 6$](figure.png) 7 | 8 | Find the number of ways of drawing. 9 | 10 | ## Input 11 | 12 | The input consists of multiple tests. For each test: 13 | 14 | The first line contains $3$ integer $n, m, k$ 15 | ($1 \leq n, m \leq 5 \cdot 10^4, 0 \leq k \leq 10^9$). 16 | 17 | ## Output 18 | 19 | For each test, write $1$ integer which denotes the number of ways of drawing. 20 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/random.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | registerGen(argc, argv, 1); 6 | int N = std::atoi(argv[2]); 7 | int n = rnd.next(1, N); 8 | int m = rnd.next(1, N); 9 | int k = rnd.next(0, n + m << 2); 10 | printf("%d %d %d\n", n, m, k); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/slow.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def solve(n, m, k): 4 | result = 0 5 | for i in xrange(1, n + 1): 6 | for j in xrange(1, m + 1): 7 | if 2 * (i + j) <= k: 8 | result += (n - i + 1) * (m - j + 1) 9 | return result 10 | 11 | for line in sys.stdin.readlines(): 12 | print(solve(*map(int, line.rstrip().split()))) 13 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n, m, l; 7 | while (scanf("%d%d%d", &n, &m, &l) == 3) { 8 | long long result = 0; 9 | for (int i = 1; i <= n; ++ i) { 10 | int max = std::min(l / 2 - i, m); 11 | if (max >= 1) { 12 | result += (long long)(2 * m - max + 1) * max / 2 * (n + 1 - i); 13 | } 14 | } 15 | std::cout << result << std::endl; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rectangle/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | int main() 4 | { 5 | registerValidation(); 6 | inf.readInt(1, 50000); 7 | inf.readSpace(); 8 | inf.readInt(1, 50000); 9 | inf.readSpace(); 10 | inf.readInt(0, 1000000000); 11 | inf.readEoln(); 12 | inf.readEof(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rmq/src/001.hand: -------------------------------------------------------------------------------- 1 | 5 1 1 2 | 1 5 1 3 | 1 5 5 4 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rmq/src/002.hand: -------------------------------------------------------------------------------- 1 | 3 1 1 2 | 1 2 2 3 | 1 2 2 4 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rmq/src/003.hand: -------------------------------------------------------------------------------- 1 | 1 0 0 2 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rmq/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'random_no.cpp' 8 | make 'validator.cpp' 9 | make 'solution.cpp' 10 | 11 | sh 'mkdir -p ../tests' 12 | 13 | test '001.hand' 14 | test '002.hand' 15 | test '003.hand' 16 | 10.times { test :random, '?', 50, 50 } 17 | 10.times { test :random_no, '?', 50, 50 } 18 | 19 | sh 'rm -rf *.exe' 20 | end 21 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/rmq/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | int main() 4 | { 5 | registerValidation(); 6 | int n = inf.readInt(1, 50); 7 | inf.readSpace(); 8 | int m[2]; 9 | m[0] = inf.readInt(0, 50); 10 | inf.readSpace(); 11 | m[1] = inf.readInt(0, 50); 12 | inf.readEoln(); 13 | ensure(m[0] + m[1] <= 50); 14 | for (int i = 0; i < 2; ++ i) { 15 | for (int j = 1; j <= m[i]; ++ j) { 16 | int l = inf.readInt(1, n); 17 | inf.readSpace(); 18 | int r = inf.readInt(1, n); 19 | inf.readSpace(); 20 | inf.readInt(1, n); 21 | inf.readEoln(); 22 | ensure(l <= r); 23 | } 24 | } 25 | inf.readEof(); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/travel/src/001.hand: -------------------------------------------------------------------------------- 1 | 3 2 1 3 2 | 1 2 3 | 2 3 4 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/travel/src/002.hand: -------------------------------------------------------------------------------- 1 | 3 2 2 3 2 | 1 2 3 | 2 3 4 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/travel/src/003.hand: -------------------------------------------------------------------------------- 1 | 2 0 1 1 2 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/travel/src/004.hand: -------------------------------------------------------------------------------- 1 | 2 0 1 1000000000 2 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/travel/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'random_dis.cpp' 8 | make 'validator.cpp' 9 | make 'solution.cpp' 10 | 11 | sh 'mkdir -p ../tests' 12 | 13 | test '001.hand' 14 | test '002.hand' 15 | test '003.hand' 16 | test '004.hand' 17 | [0, 1].each do |t| 18 | 10.times { test :random, '?', 100, 2000, 1000000000, t } 19 | end 20 | [0, 1].each do |t| 21 | test :random, '?', 100000, 500000, 1000000000, t 22 | end 23 | test :random_dis, '?', 100000, 500000, 1000000000 24 | 25 | sh 'rm -rf *.exe' 26 | end 27 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/travel/src/stress.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | count=0 3 | while true; do 4 | count=$(($count + 1)) 5 | ./random $count $@ > input 6 | ./solution < input > output.1 7 | ./slow < input > output.2 8 | cat output.1 9 | if diff output.1 output.2; then 10 | echo Passed \#$count 11 | else 12 | echo Failed \#$count 13 | break 14 | fi 15 | done 16 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/travel/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | int main() 4 | { 5 | registerValidation(); 6 | int n = inf.readInt(2, 100000); 7 | inf.readSpace(); 8 | int m = inf.readInt(0, 500000); 9 | inf.readSpace(); 10 | inf.readInt(1, 1000000000); 11 | inf.readSpace(); 12 | inf.readInt(1, 1000000000); 13 | inf.readEoln(); 14 | for (int i = 0; i < m; ++ i) { 15 | int a = inf.readInt(1, n); 16 | inf.readSpace(); 17 | int b = inf.readInt(1, n); 18 | inf.readEoln(); 19 | ensure(a != b); 20 | } 21 | inf.readEof(); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/001.hand: -------------------------------------------------------------------------------- 1 | 2 2 | 1 0 3 | 0 -1 4 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/002.hand: -------------------------------------------------------------------------------- 1 | 1 2 | 0 1 3 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/003.hand: -------------------------------------------------------------------------------- 1 | 4 2 | 1 0 3 | 0 1 4 | 0 -1 5 | -1 0 6 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/004.hand: -------------------------------------------------------------------------------- 1 | 4 2 | 1000000000 0 3 | 999999999 -1000000000 4 | -1 -999999999 5 | 0 1 6 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/005.hand: -------------------------------------------------------------------------------- 1 | 6 2 | 2 0 3 | 3 -1 4 | 4 1 5 | 0 2 6 | -1 -1 7 | 1 -2 8 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/006.hand: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/doall.rb: -------------------------------------------------------------------------------- 1 | require_relative 'testlib' 2 | 3 | doall do 4 | sh 'CXXFLAGS="-O2 -std=c++11 -Wall -Wno-unused-result -Wno-parentheses -Wno-varargs -Wno-attributes"' 5 | 6 | make 'random.cpp' 7 | make 'validator.cpp' 8 | make 'solution.cpp' 9 | 10 | sh 'mkdir -p ../tests' 11 | 12 | test '001.hand' 13 | test '002.hand' 14 | test '003.hand' 15 | test '004.hand' 16 | test '005.hand' 17 | test '006.hand' 18 | 10.times { test :random, '?', 100, 10 } 19 | 10.times { test :random, '?', 1000, 100 } 20 | 21 | sh 'rm -rf *.exe' 22 | end 23 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/random.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | #include 4 | #include 5 | 6 | int main(int argc, char* argv[]) 7 | { 8 | registerGen(argc, argv, 1); 9 | int n = std::atoi(argv[2]); 10 | int m = std::atoi(argv[3]); 11 | std::vector > blocks; 12 | for (int i = -m; i < m; ++ i) { 13 | for (int j = -m; j < m; ++ j) { 14 | if ((i + j) % 2) { 15 | blocks.push_back({i, j}); 16 | } 17 | } 18 | } 19 | shuffle(blocks.begin(), blocks.end()); 20 | blocks.resize(n); 21 | printf("%d\n", (int)blocks.size()); 22 | for (const auto &it : blocks) { 23 | printf("%d %d\n", it.first, it.second); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/problems/turn/src/validator.cpp: -------------------------------------------------------------------------------- 1 | #include "testlib.h" 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | registerValidation(); 9 | int n = inf.readInt(0, 1000); 10 | inf.readEoln(); 11 | std::set > blocks; 12 | for (int i = 0; i < n; ++ i) { 13 | int x = inf.readInt(-1000000000, 1000000000); 14 | inf.readSpace(); 15 | int y = inf.readInt(-1000000000, 1000000000); 16 | inf.readEoln(); 17 | ensure(x || y); 18 | blocks.insert({x, y}); 19 | } 20 | ensure((int)blocks.size() == n); 21 | inf.readEof(); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /my-contests/2015-06-14-sichuan-province-contest/statement/figure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ftiasch/acm-icpc/a036bdbba60f8b363ee2d6cb9814e82e5536e02e/my-contests/2015-06-14-sichuan-province-contest/statement/figure.png -------------------------------------------------------------------------------- /poi/poi-x/cia.cpp: -------------------------------------------------------------------------------- 1 | // POI X Stage I -- Sequences without Stammers(cia) 2 | #include 3 | using namespace std; 4 | 5 | const int N = 10000001; 6 | 7 | int n; 8 | bool cnt[N]; 9 | 10 | int main() { 11 | scanf("%d", &n); 12 | if (n <= 3) { 13 | printf("%d\n", n == 1? 1: 2); 14 | for (int i = 0; i < n; ++ i) { 15 | putchar('a' + (i & 1)); 16 | } 17 | } else { 18 | puts("3"); 19 | cnt[0] = 0; 20 | for (int i = 1; i <= n; ++ i) { 21 | cnt[i] = cnt[i >> 1] ^ (i & 1); 22 | } 23 | for (int i = 1; i <= n; ++ i) { 24 | if (cnt[i - 1] == cnt[i]) { 25 | putchar('c'); 26 | } else { 27 | putchar(cnt[i]? 'a': 'b'); 28 | } 29 | } 30 | } 31 | puts(""); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /project-euler/p104.py: -------------------------------------------------------------------------------- 1 | def pandigital(s): 2 | return set(map(int, s)) == {i for i in xrange(1, 10)} 3 | 4 | i = 2 5 | pa = pb = sa = sb = 1 6 | while True: 7 | i += 1 8 | pa, pb = pa + pb, pa 9 | while pa > 10 ** 50: 10 | pa /= 10 11 | pb /= 10 12 | sa, sb = (sa + sb) % 1000000000, sa 13 | if pandigital(str(pa)[:9]) and pandigital(str(sa)): 14 | print i 15 | break 16 | -------------------------------------------------------------------------------- /project-euler/p105.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | result = 0 4 | for line in open('p105_sets.txt').readlines(): 5 | a = sorted(map(int, line.split(','))) 6 | n = len(a) 7 | valid = True 8 | for i in xrange(2, n + 1): 9 | if sum(a[:i]) < sum(a[-(i - 1):]): 10 | valid = False 11 | sums = [0] 12 | for x in a: 13 | sums += map(lambda y: x + y, sums) 14 | if len(set(sums)) != 2 ** n: 15 | valid = False 16 | if valid: 17 | result += sum(a) 18 | print result 19 | -------------------------------------------------------------------------------- /project-euler/p107.py: -------------------------------------------------------------------------------- 1 | from urllib.request import urlopen 2 | 3 | def find(u): 4 | if parent[u] != u: 5 | parent[u] = find(parent[u]) 6 | return parent[u] 7 | 8 | lines = urlopen('https://projecteuler.net/project/resources/p107_network.txt').readlines() 9 | n = len(lines) 10 | edges = [] 11 | for i, line in enumerate(lines): 12 | tokens = line.decode().rstrip().split(',') 13 | for j in range(i): 14 | if tokens[j] != '-': 15 | edges.append((int(tokens[j]), i, j)) 16 | result, parent = 0, [i for i in range(n)] 17 | for c, a, b in sorted(edges): 18 | if find(a) == find(b): 19 | result += c 20 | else: 21 | parent[find(a)] = find(b) 22 | print(result) 23 | -------------------------------------------------------------------------------- /project-euler/p108.py: -------------------------------------------------------------------------------- 1 | # nx + ny = xy 2 | # (x - n) (y - n) = n^2 3 | def count(n): 4 | result, p = 1, 2 5 | while p * p <= n: 6 | if n % p == 0: 7 | e = 0 8 | while n % p == 0: 9 | e += 1 10 | n /= p 11 | result *= e + e + 1 12 | p += 1 13 | if n > 1: 14 | result *= 3 15 | return (result + 1) / 2 16 | 17 | n = 1 18 | while count(n) <= 1000: 19 | n += 1 20 | print(n) 21 | -------------------------------------------------------------------------------- /project-euler/p109.py: -------------------------------------------------------------------------------- 1 | from operator import itemgetter 2 | from itertools import product 3 | 4 | scores = [] 5 | for i in range(3): 6 | for j in range(20): 7 | scores.append((i + 1, (i + 1) * (j + 1))) 8 | scores.append((3, 25)) 9 | scores.append((2, 50)) 10 | 11 | results = set() 12 | for length in range(3): 13 | for permutation in product(scores, repeat=length + 1): 14 | if permutation[-1][0] != 2: 15 | continue 16 | if sum(map(itemgetter(1), permutation)) >= 100: 17 | continue 18 | if length + 1 == 3 and permutation[0] > permutation[1]: 19 | continue 20 | results.add(permutation) 21 | print(len(results)) 22 | -------------------------------------------------------------------------------- /project-euler/p112.py: -------------------------------------------------------------------------------- 1 | def check(n): 2 | digits = list(str(n)) 3 | return sorted(digits) == digits or sorted(digits) == list(reversed(digits)) 4 | 5 | n, m = 1, 0 6 | while m * 100 != n * 99: 7 | n += 1 8 | if not check(n): 9 | m += 1 10 | print(n) 11 | -------------------------------------------------------------------------------- /project-euler/p113.py: -------------------------------------------------------------------------------- 1 | def count(n): 2 | result = 1 3 | for i in xrange(n): 4 | result *= 10 + i 5 | result /= i + 1 6 | return result 7 | 8 | n = 100 9 | asc = count(n) 10 | dec = 0 11 | for length in xrange(n): 12 | dec += count(length + 1) 13 | print(asc + dec - 10 * n - 1) 14 | -------------------------------------------------------------------------------- /project-euler/p114.py: -------------------------------------------------------------------------------- 1 | ways = [1] 2 | n = 0 3 | while ways[-1] <= 1000000: 4 | n += 1 5 | new_ways = ways[n - 1] 6 | for i in range(50, n + 1): 7 | new_ways += ways[max(n - i - 1, 0)] 8 | ways.append(new_ways) 9 | print(n) 10 | -------------------------------------------------------------------------------- /project-euler/p116.py: -------------------------------------------------------------------------------- 1 | n, result = 50, -3 2 | for length in (2, 3, 4): 3 | ways = [0] * (n + 1) 4 | ways[0] = 1 5 | for i in range(1, n + 1): 6 | ways[i] = ways[i - 1] 7 | if i >= length: 8 | ways[i] += ways[i - length] 9 | result += ways[n] 10 | print(result) 11 | -------------------------------------------------------------------------------- /project-euler/p117.py: -------------------------------------------------------------------------------- 1 | n = 50 2 | ways = [0] * (n + 1) 3 | ways[0] = 1 4 | for i in range(1, n + 1): 5 | ways[i] = ways[i - 1] 6 | for length in (2, 3, 4): 7 | if i >= length: 8 | ways[i] += ways[i - length] 9 | print(ways[n]) 10 | -------------------------------------------------------------------------------- /project-euler/p119.py: -------------------------------------------------------------------------------- 1 | LENGTH = 50 2 | results = set() 3 | for dsum in range(2, 9 * LENGTH + 1): 4 | e = 1 5 | while dsum ** e < 10 ** LENGTH: 6 | n = dsum ** e 7 | if n >= 10 and sum(map(int, str(n))) == dsum: 8 | results.add(n) 9 | e += 1 10 | print(sorted(list(results))[30 - 1]) 11 | 12 | -------------------------------------------------------------------------------- /project-euler/p120.py: -------------------------------------------------------------------------------- 1 | # a + 1 2 | # a^2 + 2a + 1 = 2a + 1 3 | # 2a^2 + 2a = 3a + 1 4 | # ... 5 | 6 | # a - 1 7 | # a^2 - 2a + 1 = -2a + 1 8 | # (-2a + 1) (a - 1) = 3a - 1 9 | # (3a - 1)(a - 1) = -4a + 1 10 | 11 | def solve(a): 12 | result = 0 13 | for n in range(1, 2 * a): 14 | r = n * a + 1 15 | if n % 2 == 0: 16 | r -= n * a - 1 17 | else: 18 | r += n * a - 1 19 | r %= a * a 20 | if r < 0: 21 | r += a * a 22 | result = max(result, r) 23 | return result 24 | 25 | result = 0 26 | for a in range(3, 1001): 27 | result += solve(a) 28 | print(result) 29 | -------------------------------------------------------------------------------- /project-euler/p121.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | def solve(n): 4 | prob = [Fraction(1)] + [Fraction(0)] * n 5 | for i in range(n): 6 | blue_prob = Fraction(1, 2 + i) 7 | for j in range(i + 1, 0, -1): 8 | prob[j] = prob[j] * (1 - blue_prob) + prob[j - 1] * blue_prob 9 | prob[0] *= 1 - blue_prob 10 | result = 0 11 | for i in range(n // 2 + 1, n + 1): 12 | result += prob[i] 13 | return int(float(1 / result)) 14 | 15 | for n in (4, 15): 16 | print(solve(n)) 17 | -------------------------------------------------------------------------------- /project-euler/p132.py: -------------------------------------------------------------------------------- 1 | def is_prime(n): 2 | d = 2 3 | while d * d <= n: 4 | if n % d == 0: 5 | return False 6 | d += 1 7 | return True 8 | 9 | n = 10 ** 9 10 | p = 2 11 | count = sum = 0 12 | while count < 40: 13 | if p not in (2, 5) and is_prime(p) and (pow(10, n, p * 9) - 1) / 9 % p == 0: 14 | count += 1 15 | sum += p 16 | p += 1 17 | print(sum) 18 | -------------------------------------------------------------------------------- /project-euler/p137.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | # https://www.alpertron.com.ar/QUAD.HTM 4 | 5 | count, x, y = 0, 13, 29 6 | ns = [] 7 | for x0, y0 in ((1, 1), (13, 29), (1, -1), (5, 11), (2, 4)): 8 | count, x, y = 0, x0, y0 9 | while count < 15: 10 | x, y = 9 * x + 4 * y, 20 * x + 9 * y 11 | if (y - 1) % 5 == 0: 12 | count += 1 13 | ns.append((y - 1) // 5) 14 | ns = list(set(ns)) 15 | ns.sort() 16 | print(ns[15 - 1]) 17 | -------------------------------------------------------------------------------- /project-euler/p138.py: -------------------------------------------------------------------------------- 1 | # (5b \pm 4)^2 + 4 = 20L^2 2 | # N(4, 1) = -4 3 | # N(9, 2) = 1 4 | 5 | x, y, count, result = 4, 1, 0, 0 6 | while count < 12: 7 | x, y = 9 * x + 40 * y, 2 * x + 9 * y 8 | if x % 5 == 4 or x % 5 == 1: 9 | # b = (x % 5 == 4 and x - 4 or x + 4) // 5 10 | # L = y 11 | count += 1 12 | result += y 13 | print(result) 14 | -------------------------------------------------------------------------------- /project-euler/p139.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | const int N = 100000000; 7 | 8 | int result; 9 | 10 | void generate(int p1, int q1, int p2, int q2) 11 | { 12 | int p = p1 + p2; 13 | int q = q1 + q2; 14 | if (2 * q * q + 2 * q < N) { 15 | if (q - p & 1) { 16 | int a = q * q - p * p; 17 | int b = 2 * q * p; 18 | int c = q * q + p * p; 19 | if (c % std::abs(a - b) == 0) { 20 | result += (N - 1) / (a + b + c); 21 | } 22 | } 23 | generate(p1, q1, p, q); 24 | generate(p, q, p2, q2); 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | generate(0, 1, 1, 1); 31 | printf("%d\n", result); 32 | } 33 | -------------------------------------------------------------------------------- /project-euler/p140.py: -------------------------------------------------------------------------------- 1 | solutions = [] 2 | for x0, y0 in ( 3 | (7, 1), 4 | (8, 2), 5 | (13, 5), 6 | (17, 7), 7 | (32, 14), 8 | (43, 19), 9 | (83, 37), 10 | (112, 50), 11 | ): 12 | x, y = x0, y0 13 | for _ in range(20): 14 | assert x * x - 5 * y * y == 44 15 | if x > 7 and x % 5 == 2: 16 | solutions.append((x - 7) // 5) 17 | # solutions.append((x, y)) 18 | x, y = 9 * x + 20 * y, 4 * x + 9 * y 19 | solutions = list(set(solutions)) 20 | solutions.sort() 21 | print(sum(solutions[:30]) 22 | -------------------------------------------------------------------------------- /project-euler/p148.py: -------------------------------------------------------------------------------- 1 | def dfs(digits, memo, less, pos): 2 | if pos == -1: 3 | return int(less) 4 | key = (less, pos) 5 | if key in memo: 6 | return memo[key] 7 | result = 0 8 | for d in range(less and 6 or digits[pos], -1, -1): 9 | result += dfs(digits, memo, less, pos - 1) * (d + 1) 10 | less = True 11 | memo[key] = result 12 | return result 13 | 14 | def solve(n): 15 | digits = [] 16 | while n > 0: 17 | digits.append(n % 7) 18 | n //= 7 19 | memo = {} 20 | return dfs(digits, memo, False, len(digits) - 1) 21 | 22 | for n in (100, 10 ** 9): 23 | print(n, solve(n)) 24 | -------------------------------------------------------------------------------- /project-euler/p151.py: -------------------------------------------------------------------------------- 1 | def transform(state, x): 2 | new_state = list(state) 3 | new_state.remove(x) 4 | y = x 5 | while y > 1: 6 | y //= 2 7 | new_state.append(y) 8 | new_state.sort() 9 | return tuple(new_state) 10 | 11 | def solve(memo, state): 12 | if state in memo: 13 | return memo[state] 14 | result = len(state) == 1 and 1. or 0. 15 | prob = 1. / len(state) 16 | for x in state: 17 | result += prob * solve(memo, transform(state, x)) 18 | memo[state] = result 19 | return result 20 | 21 | memo = { (1,) : 0. } 22 | print(solve(memo, (16,)) - 1.) 23 | -------------------------------------------------------------------------------- /project-euler/p154.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int N = 200000; 4 | 5 | int cnt_2[N + 1], cnt_5[N + 1]; 6 | 7 | int main() 8 | { 9 | for (int n = 1; n <= N; ++ n) { 10 | cnt_2[n] = cnt_2[n / 2] + n / 2; 11 | } 12 | for (int n = 1; n <= N; ++ n) { 13 | cnt_5[n] = cnt_5[n / 5] + n / 5; 14 | } 15 | auto result = 0LL; 16 | for (int x = 0; x <= N; ++ x) { 17 | for (int y = 0; x + y <= N; ++ y) { 18 | int z = N - x - y; 19 | result += cnt_2[N] - 12 >= cnt_2[x] + cnt_2[y] + cnt_2[z] && cnt_5[N] - 12 >= cnt_5[x] + cnt_5[y] + cnt_5[z]; 20 | } 21 | } 22 | printf("%lld\n", result); 23 | } 24 | -------------------------------------------------------------------------------- /project-euler/p157.py: -------------------------------------------------------------------------------- 1 | from fractions import gcd 2 | 3 | def solve(n): 4 | solutions = set() 5 | for two in range(2 * n + 1): 6 | for five in range(2 * n + 1): 7 | ap = (2 ** two) * (5 ** five) + 10 ** n 8 | bp = (2 ** (2 * n - two)) * (5 ** (2 * n - five)) + 10 ** n 9 | if ap <= bp: 10 | p = gcd(ap, bp) 11 | a = ap // p 12 | b = bp // p 13 | d = 1 14 | while d * d <= p: 15 | if p % d == 0: 16 | solutions.add((a * d, b * d)) 17 | if d * d != p: 18 | solutions.add((a * (p // d), b * (p // d))) 19 | d += 1 20 | return len(solutions) 21 | 22 | print(solve(1)) 23 | print(sum(solve(n) for n in range(1, 10))) 24 | -------------------------------------------------------------------------------- /project-euler/p158.py: -------------------------------------------------------------------------------- 1 | def count(n, color): 2 | ways = [0] * (n + 1) 3 | ways[0] = 1 4 | for i in range(n): 5 | for j in range(n, 0, -1): 6 | ways[j] += color * ways[j - 1] 7 | return ways 8 | 9 | def solve(n): 10 | ways_1 = count(n, 1) 11 | ways_2 = count(n, 2) 12 | ways = [0] * (n + 1) 13 | for i in range(n + 1): 14 | ways[i] = ways_2[i] - (i + 1) * ways_1[i] 15 | return ways 16 | 17 | print(max(solve(26))) 18 | -------------------------------------------------------------------------------- /project-euler/p159.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int digit_root(int n) 6 | { 7 | auto r = n % 9; 8 | return r ? r : 9; 9 | } 10 | 11 | void update(int& x, int a) 12 | { 13 | x = std::max(x, a); 14 | } 15 | 16 | const int N = 1000000; 17 | 18 | int main() 19 | { 20 | std::vector best(N); 21 | for (int i = 1; i < N; ++ i) { 22 | for (int j = 2; i * j < N; ++ j) { 23 | update(best.at(i * j), best.at(i) + digit_root(j)); 24 | } 25 | } 26 | long long result = 0; 27 | for (int n = 2; n < N; ++ n) { 28 | result += best.at(n); 29 | } 30 | printf("%lld\n", result); 31 | } 32 | -------------------------------------------------------------------------------- /project-euler/p162.py: -------------------------------------------------------------------------------- 1 | def count(max_length, digits): 2 | result = 0 3 | for length in range(max_length): 4 | for first in digits: 5 | if first != 0: 6 | result += pow(len(digits), length) 7 | return result 8 | 9 | from itertools import combinations 10 | 11 | result = 0 12 | for size in range(4): 13 | for combination in combinations((0, 1, 15), size): 14 | ways = count(16, [d for d in range(16) if d not in combination]) 15 | if size % 2 == 0: 16 | result += ways 17 | else: 18 | result -= ways 19 | print(hex(result).upper()[2:]) 20 | -------------------------------------------------------------------------------- /project-euler/p164.py: -------------------------------------------------------------------------------- 1 | def count(memo, n, d1, d0): 2 | if n == 0: 3 | return 1 4 | key = (n, d1, d0) 5 | if key in memo: 6 | return memo[key] 7 | result = 0 8 | for d in range(0, 10 - d1 - d0): 9 | result += count(memo, n - 1, d0, d) 10 | memo[key] = result 11 | return result 12 | 13 | memo, result = {}, 0 14 | for d in range(1, 10): 15 | result += count(memo, 19, 0, d) 16 | print(result) 17 | -------------------------------------------------------------------------------- /project-euler/p168.py: -------------------------------------------------------------------------------- 1 | # at most 100 digits 2 | result = 0 3 | for d0 in range(1, 10): 4 | for d in range(1, 100): 5 | for k in range(1, 10): # 1? 6 | a = d0 * (10 ** d) - d0 * k 7 | b = 10 * k - 1 8 | if a % b == 0: 9 | D = a // b 10 | if len(str(D)) == d: 11 | result += D * 10 + d0 12 | result %= 100000 13 | print(result) 14 | -------------------------------------------------------------------------------- /project-euler/p169.py: -------------------------------------------------------------------------------- 1 | def solve(n): 2 | k = 0 3 | while 2 ** (k + 1) <= n: 4 | k += 1 5 | ways = [1, 0, 0, 0] 6 | for i in range(k, -1, -1): 7 | new_ways = [0] * 4 8 | add = n // (2 ** i) % 2 9 | for r in range(4): 10 | for u in range(3): 11 | rr = r * 2 + add - u 12 | if rr < 4: 13 | new_ways[rr] += ways[r] 14 | ways = new_ways 15 | return ways[0] 16 | 17 | print(solve(10)) 18 | print(solve(10 ** 25)) 19 | -------------------------------------------------------------------------------- /project-euler/p173.py: -------------------------------------------------------------------------------- 1 | def solve(n): 2 | result = 0 3 | for i in range(2, n + 1, 2): 4 | j = i + 2 5 | while i * j <= n: 6 | result += 1 7 | j += 2 8 | return result 9 | 10 | print(solve(100)) 11 | print(solve(10 ** 6)) 12 | -------------------------------------------------------------------------------- /project-euler/p174.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int N = 1000000; 4 | 5 | int count[N + 1]; 6 | 7 | int main() 8 | { 9 | for (int i = 2; i <= N; i += 2) { 10 | for (int j = i + 2; (long long)i * j <= N; j += 2) { 11 | count[i * j] ++; 12 | } 13 | } 14 | int result = 0; 15 | for (int i = 1; i <= N; ++ i) { 16 | result += 1 <= count[i] && count[i] <= 10; 17 | } 18 | printf("%d\n", result); 19 | } 20 | -------------------------------------------------------------------------------- /project-euler/p180.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | from fractions import Fraction 3 | 4 | def generate(k): 5 | return { Fraction(a, b) for b in range(1, k + 1) for a in range(1, b) } 6 | 7 | def solve(k): 8 | candidates = generate(k) 9 | precomputed = { 10 | n : { z ** n : z for z in candidates } for n in range(-2, 3) 11 | } 12 | result = set() 13 | for x in candidates: 14 | for y in candidates: 15 | for n in range(-2, 3): 16 | zn = x ** n + y ** n 17 | if zn in precomputed[n]: 18 | z = precomputed[n][zn] 19 | result.add(x + y + z) 20 | if x == y: 21 | break 22 | result = sum(result) 23 | return result.numerator + result.denominator 24 | 25 | print(solve(35)) 26 | -------------------------------------------------------------------------------- /project-euler/p182.py: -------------------------------------------------------------------------------- 1 | from fractions import gcd 2 | 3 | def count(p, e): 4 | result = 1 # m = 0 5 | q = (p - 1) // gcd(p - 1, e - 1) 6 | return (p - 1 + q - 1) // q 7 | 8 | def solve(p, q): 9 | phi = (p - 1) * (q - 1) 10 | minimum, minimum_count = p * q, 0 11 | for e in range(2, phi): 12 | if gcd(e, phi) != 1: 13 | continue 14 | tmp = count(p, e) * count(q, e) 15 | if tmp < minimum: 16 | minimum, minimum_count = tmp, 0 17 | if tmp == minimum: 18 | minimum_count += e 19 | return minimum_count 20 | 21 | print(solve(1009, 3643)) 22 | -------------------------------------------------------------------------------- /project-euler/p183.py: -------------------------------------------------------------------------------- 1 | from fractions import gcd 2 | from math import log 3 | 4 | def d(n): 5 | result= (0., 1) 6 | for k in range(1, n + 1): 7 | result = max(result, (k * log(n / k), k)) 8 | k = result[1] 9 | k //= gcd(n, k) 10 | for p in (2, 5): 11 | while k % p == 0: 12 | k //= p 13 | return k == 1 and -n or n 14 | 15 | def solve(n): 16 | result = 0 17 | for n in range(5, n + 1): 18 | result += d(n) 19 | return result 20 | 21 | print(solve(100)) 22 | print(solve(10000)) 23 | -------------------------------------------------------------------------------- /project-euler/p186.py: -------------------------------------------------------------------------------- 1 | N = 1000000 2 | 3 | buffer = [ (100003 - 200003 * k + 300007 * k * k * k) % N for k in range(1, 56) ] 4 | 5 | def generate(): 6 | buffer.append((buffer[0] + buffer[-24]) % N) 7 | elem = buffer[0] 8 | del buffer[0] 9 | return elem 10 | 11 | parent = list(range(N)) 12 | size = [1] * N 13 | 14 | def find(u): 15 | if parent[u] != u: 16 | parent[u] = find(parent[u]) 17 | return parent[u] 18 | 19 | minister, count = 524287, 0 20 | while size[find(minister)] * 100 < N * 99: 21 | a = generate() 22 | b = generate() 23 | if a == b: 24 | continue 25 | count += 1 26 | a = find(a) 27 | b = find(b) 28 | if a != b: 29 | parent[a] = b 30 | size[b] += size[a] 31 | print(count) 32 | -------------------------------------------------------------------------------- /project-euler/p188.py: -------------------------------------------------------------------------------- 1 | def get_phi(mod): 2 | result, d = mod, 2 3 | while d * d <= mod: 4 | if mod % d == 0: 5 | result //= d 6 | result *= d - 1 7 | while mod % d == 0: 8 | mod //= d 9 | d += 1 10 | if mod > 1: 11 | result //= mod 12 | result *= mod - 1 13 | return result 14 | 15 | def solve(a, k, mod): 16 | if k == 1: 17 | return a % mod 18 | if mod == 1: 19 | return 0 20 | phi = get_phi(mod) 21 | return pow(a, phi + solve(a, k - 1, phi), mod) 22 | 23 | print(solve(1777, 1855, 10 ** 8)) 24 | -------------------------------------------------------------------------------- /project-euler/p190.py: -------------------------------------------------------------------------------- 1 | from math import floor 2 | from operator import mul 3 | from functools import reduce 4 | 5 | def solve(m): 6 | s = m * (m + 1) / 2 7 | return floor(reduce(mul, [(i / s * m) ** i for i in range(1, m + 1)])) 8 | 9 | print(solve(10)) 10 | print(sum(solve(m) for m in range(2, 16)) 11 | -------------------------------------------------------------------------------- /project-euler/p191.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | def solve(n): 4 | ways = {(0, 0) : 1} 5 | for _ in range(n): 6 | new_ways = defaultdict(lambda: 0) 7 | for state, way in ways.items(): 8 | absent, late = state 9 | if late == 0: 10 | new_ways[(0, 1)] += way 11 | new_ways[(0, late)] += way # on time 12 | if absent < 2: 13 | new_ways[(absent + 1, late)] += way 14 | ways = new_ways 15 | return sum(ways.values()) 16 | 17 | print(solve(4)) 18 | print(solve(30)) 19 | -------------------------------------------------------------------------------- /project-euler/p193.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const auto N = 1LL << 25; 5 | 6 | int main() 7 | { 8 | auto result = N * N; 9 | std::vector mu(N + 1, 2); 10 | std::vector primes; 11 | for (auto d = 2LL; d <= N; ++ d) { 12 | if (mu[d] == 2) { 13 | primes.push_back(d); 14 | mu[d] = -1; 15 | } 16 | for (auto&& p : primes) { 17 | if (d * p > N) { 18 | break; 19 | } 20 | mu[d * p] = -mu[d]; 21 | if (d % p == 0) { 22 | mu[d * p] = 0; 23 | break; 24 | } 25 | } 26 | result += mu[d] * (N * N) / (d * d); 27 | } 28 | printf("%lld\n", result); 29 | } 30 | -------------------------------------------------------------------------------- /project-euler/p197.py: -------------------------------------------------------------------------------- 1 | from math import floor 2 | 3 | def f(x): 4 | return floor(2 ** (30.403243784 - x * x)) * (1e-9) 5 | 6 | u, v = None, -1 7 | for i in range(100000): 8 | u, v = v, f(v) 9 | print(u + v) 10 | -------------------------------------------------------------------------------- /project-euler/p198.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | long long result = 0; 6 | 7 | void generate(int p1, int q1, int p2, int q2) 8 | { 9 | auto pp = 1LL * p1 * q2 + 1LL * p2 * q1; 10 | auto qq = 2LL * q1 * q2; 11 | if (qq > 100000000) { 12 | return; 13 | } 14 | result += pp * 100LL < qq; 15 | auto p = p1 + p2; 16 | auto q = q1 + q2; 17 | generate(p1, q1, p, q); 18 | if (100LL * p < q) { 19 | generate(p, q, p2, q2); 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | generate(0, 1, 1, 1); 26 | printf("%lld\n", result); 27 | } 28 | -------------------------------------------------------------------------------- /project-euler/p199.py: -------------------------------------------------------------------------------- 1 | # Descartes's theorem 2 | from math import sqrt 3 | from collections import defaultdict 4 | 5 | def normalize(k1, k2, k3): 6 | return tuple(sorted([k1, k2, k3])) 7 | 8 | K = 3 - 2 * sqrt(3) 9 | R = 1 / (3 - 2 * sqrt(3)) 10 | covered = 3 11 | ways = { (1, 1, 1) : 1, (K, 1, 1) : 3 } 12 | for _ in range(10): 13 | new_ways = defaultdict(lambda: 0) 14 | for ks, way in ways.items(): 15 | k1, k2, k3 = ks 16 | k4 = k1 + k2 + k3 + 2 * sqrt(k1 * k2 + k2 * k3 + k3 * k1) 17 | covered += 1 / (k4 * k4) * way 18 | new_ways[normalize(k1, k2, k4)] += way 19 | new_ways[normalize(k2, k3, k4)] += way 20 | new_ways[normalize(k3, k1, k4)] += way 21 | ways = new_ways 22 | print(1. - covered / R / R) 23 | -------------------------------------------------------------------------------- /project-euler/p204.py: -------------------------------------------------------------------------------- 1 | def is_prime(n): 2 | d = 2 3 | while d * d <= n: 4 | if n % d == 0: 5 | return False 6 | d += 1 7 | return True 8 | 9 | def dfs(primes, memo, index, bound): 10 | if index < 0: 11 | return 1 12 | key = (index, bound) 13 | if key in memo: 14 | return memo[key] 15 | p, result = primes[index], 0 16 | while bound > 0: 17 | result += dfs(primes, memo, index - 1, bound) 18 | bound //= p 19 | memo[key] = result 20 | return result 21 | 22 | def solve(n, m): 23 | primes = [p for p in range(2, n + 1) if is_prime(p)] 24 | return dfs(primes, {}, len(primes) - 1, m) 25 | 26 | print(solve(5, 10 ** 8)) 27 | print(solve(100, 10 ** 9)) 28 | -------------------------------------------------------------------------------- /project-euler/p205.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | def solve(numbers, sides): 4 | max_sum = numbers * sides 5 | result = [0] * (max_sum + 1) 6 | result[0] = 1 7 | for _ in range(numbers): 8 | for j in range(max_sum, -1, -1): 9 | if result[j] > 0: 10 | for outcome in range(1, sides + 1): 11 | result[j + outcome] += result[j] / Fraction(sides) 12 | result[j] = 0 13 | return result 14 | 15 | peter = solve(9, 4) 16 | culin = solve(6, 6) 17 | result = 0 18 | cumsum = 0 19 | for x in range(37): 20 | result += peter[x] * cumsum 21 | cumsum += culin[x] 22 | print(float(result)) 23 | -------------------------------------------------------------------------------- /project-euler/p206.py: -------------------------------------------------------------------------------- 1 | # mod 10 2 | # mod 1000 3 | # ... 4 | # mod 10^{2k + 1} 5 | 6 | # 19 7 | 8 | candidates = [] 9 | for r in range(0, 10 ** 7, 10): 10 | r2 = r * r % (10 ** 7) 11 | if r2 // 100 % 10 == 9 and r2 // 10000 % 10 == 8 and r2 // 1000000 % 10 == 7: 12 | candidates.append(r) 13 | for h in range(10 ** 3): 14 | for r in candidates: 15 | x = h * (10 ** 7) + r 16 | x2, valid = x * x, True 17 | x2 //= 10 18 | for i in range(9, 0, -1): 19 | if x2 // 10 % 10 != i: 20 | valid = False 21 | break 22 | x2 //= 100 23 | if valid and x2 == 0: 24 | print(x, x * x) 25 | -------------------------------------------------------------------------------- /project-euler/p207.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | from math import floor, sqrt 3 | 4 | def count(m): 5 | x = floor(sqrt(m)) 6 | while (x + 1) * x <= m: 7 | x += 1 8 | return x - 1 9 | 10 | def proportion(m): 11 | t = 0 12 | while 4 ** (t + 1) - 2 ** (t + 1) <= m: 13 | t += 1 14 | return Fraction(t, count(m)) 15 | 16 | for m in (5, 10, 15, 20, 25, 30, 180, 185): 17 | print(m, proportion(m)) 18 | 19 | t = 1 20 | while True: 21 | next = 4 ** (t + 1) - 2 ** (t + 1) 22 | low = 4 ** t - 2 ** t 23 | high = next 24 | while low < high: 25 | middle = (low + high) // 2 26 | if t * 12345 < count(middle): 27 | high = middle 28 | else: 29 | low = middle + 1 30 | if high < next: 31 | break 32 | t += 1 33 | print(high, proportion(high)) 34 | -------------------------------------------------------------------------------- /project-euler/p208.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | def solve(n): 4 | assert n % 5 == 0 5 | ways = { (0, 0, 0, 0, 0) : 1 } 6 | for _ in range(n): 7 | # print(_) 8 | new_ways = defaultdict(lambda: 0) 9 | for s, way in ways.items(): 10 | new_ways[(s[4] + 1, s[0], s[1], s[2], s[3])] += way 11 | new_ways[(s[1], s[2], s[3], s[4], s[0] + 1)] += way 12 | ways = new_ways 13 | result = 0 14 | for s, way in ways.items(): 15 | if s[0] == s[1] == s[2] == s[3] == s[4]: 16 | result += way 17 | return result 18 | 19 | print(solve(25)) 20 | print(solve(70)) 21 | -------------------------------------------------------------------------------- /project-euler/p209.py: -------------------------------------------------------------------------------- 1 | from itertools import product 2 | from functools import reduce 3 | from operator import mul 4 | 5 | # print('graph {') 6 | # for a, b, c, d, e, f in product((0, 1), repeat=6): 7 | # u = ''.join(map(str, (a, b, c, d, e, f))) 8 | # v = ''.join(map(str, (b, c, d, e, f, a ^ (b & c)))) 9 | # print('{}--{}'.format(u, v)) 10 | # print('}') 11 | 12 | def count(n): 13 | if n == 1: 14 | return 1 15 | if n == 2: 16 | return 3 17 | ways = [1, 2] 18 | for i in range(2, n): 19 | ways.append(ways[-1] + ways[-2]) 20 | return ways[n - 1] + ways[n - 3] 21 | 22 | cycles = [1, 2, 3, 6, 6, 46] 23 | print(reduce(mul, map(count, cycles))) 24 | -------------------------------------------------------------------------------- /project-euler/p219.py: -------------------------------------------------------------------------------- 1 | def solve(n): 2 | b, s = 0, (1, 0, 0, 0) 3 | while True: 4 | ns = (s[0] + s[1], s[2], s[3], s[0]) 5 | if sum(ns) >= n: 6 | break 7 | b += 1 8 | s = ns 9 | d = n - sum(s) 10 | ns = [s[0] - d, s[1] + d, s[2], s[3], d] 11 | result = 0 12 | for i in range(5): 13 | result += ns[i] * (b + i) 14 | return result 15 | 16 | print(solve(6)) 17 | print(solve(10 ** 9)) 18 | -------------------------------------------------------------------------------- /project-euler/p225.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | auto count = 124; 6 | auto mod = 1; 7 | while (count) { 8 | mod += 2; 9 | auto a = 1; 10 | auto b = 1; 11 | auto c = 1; 12 | bool valid = true; 13 | do { 14 | std::swap(a, b); 15 | std::swap(b, c); 16 | c += a; 17 | c += b; 18 | while (c >= mod) { 19 | c -= mod; 20 | } 21 | valid &= !!c; 22 | } while ((a != 1 || b != 1 || c != 1) && valid); 23 | if (valid) { 24 | count --; 25 | } 26 | } 27 | printf("%d\n", mod); 28 | } 29 | -------------------------------------------------------------------------------- /project-euler/p227.sage: -------------------------------------------------------------------------------- 1 | from sage.all import * 2 | 3 | def p(d): 4 | return d == 0 and 2. / 3 or 1. / 6 5 | 6 | def solve(n): 7 | n2 = n // 2 8 | a = [[0.] * (n2 + 1) for _ in range(n2 + 1)] 9 | b = [1.] * (n2 + 1) 10 | for d in range(n2 + 1): 11 | a[d][d] = 1. 12 | if d > 0: 13 | for dx in (-1, 0, 1): 14 | for dy in (-1, 0, 1): 15 | dd = (d + n + dy + n - dx) % n 16 | dd = min(dd, n - dd) 17 | a[d][dd] -= p(dx) * p(dy) 18 | else: 19 | b[d] = 0. 20 | return Matrix(a).solve_right(vector(b))[n2] 21 | 22 | print(solve(100)) 23 | -------------------------------------------------------------------------------- /project-euler/p228.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | alphas = set() 4 | for n in range(1864, 1909 + 1): 5 | alpha = 0 6 | delta = Fraction(1, n) 7 | for i in range(n): 8 | alphas.add(alpha) 9 | alpha += delta 10 | print(len(alphas)) 11 | -------------------------------------------------------------------------------- /project-euler/p229.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const auto N = 2000000000; 6 | 7 | std::bitset result, gao; 8 | 9 | int main() 10 | { 11 | result.flip(); 12 | for (auto&& k : std::vector{1, 2, 3, 7}) { 13 | gao.reset(); 14 | for (auto b = 1; k * b * b <= N; ++ b) { 15 | auto sum = 1 + k * b * b; 16 | for (auto a = 3; sum <= N; a += 2) { 17 | gao.set(sum); 18 | sum += a; 19 | } 20 | } 21 | result &= gao; 22 | } 23 | printf("%ld\n", result.count()); 24 | } 25 | -------------------------------------------------------------------------------- /project-euler/p235.py: -------------------------------------------------------------------------------- 1 | def check(n, r): 2 | s = 0 3 | for k in range(n, 0, -1): 4 | s *= r 5 | s += (900 - 3 * k) 6 | return s 7 | 8 | def solve(n, a): 9 | def f(r): 10 | return check(n, r) - a 11 | 12 | low = 1. 13 | high = 1.01 14 | for _ in range(5000): 15 | x = .5 * (low + high) 16 | if f(low) * f(x) < 0: 17 | high = x 18 | else: 19 | low = x 20 | print(check(n, x)) 21 | return x 22 | 23 | print(solve(5000, -6 * 10 ** 11)) 24 | -------------------------------------------------------------------------------- /project-euler/p239.py: -------------------------------------------------------------------------------- 1 | def is_prime(n): 2 | d = 2 3 | while d * d <= n: 4 | if n % d == 0: 5 | return False 6 | d += 1 7 | return True 8 | 9 | primes = [p for p in range(2, 100) if is_prime(p)] 10 | 11 | n, m = len(primes), 100 12 | 13 | binom = [[0] * (m + 1) for _ in range(m + 1)] 14 | for i in range(m + 1): 15 | binom[i][0] = 1 16 | for j in range(1, i + 1): 17 | binom[i][j] = binom[i - 1][j - 1] + binom[i - 1][j] 18 | 19 | factorial = [1] * (m + 1) 20 | for i in range(1, m + 1): 21 | factorial[i] = factorial[i - 1] * i 22 | 23 | correct = n - 22 24 | 25 | result = 0 26 | for i in range(n - correct + 1): 27 | result += (-1) ** i * binom[n][correct] * binom[n - correct][i] * factorial[m - correct - i] 28 | 29 | print(result / factorial[m]) 30 | -------------------------------------------------------------------------------- /project-euler/p242.py: -------------------------------------------------------------------------------- 1 | # f(4 * n + 1, 2 * k + 1) = binom(2 * n, k) mod 2 2 | def recur(mem, a, i, less): 3 | key = (i, less) 4 | if key in mem: 5 | return mem[key] 6 | res = recur(mem, a, i + 1, less or a[i] == 1) 7 | if less or a[i] == 1: 8 | res += 2 * recur(mem, a, i + 1, less) 9 | mem[key] = res 10 | return res 11 | 12 | 13 | def solve(n): 14 | a = list(map(int, bin((n - 1) // 4 + 1)[2:])) 15 | mem = {(len(a), False): 0, (len(a), True): 1} 16 | return recur(mem, a, 0, False) 17 | 18 | 19 | print(solve(10)) 20 | print(solve(10 ** 12)) 21 | -------------------------------------------------------------------------------- /project-euler/p301/p301.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler 301 Nim 2 | solve n = iter 1 2 n 3 | where iter a b n 4 | | n == 0 = a 5 | | otherwise = iter b (a + b) (n - 1) 6 | 7 | main = print $ solve 30 8 | -------------------------------------------------------------------------------- /project-euler/p303/p303.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | def recur(pred, rr): 4 | (c, r) = pred[rr] 5 | if r == 0: 6 | return c 7 | return recur(pred, r) * 10 + c 8 | 9 | def solve(m): 10 | pred, head, queue = [None] * m, 0, [0] 11 | while head < len(queue): 12 | r = queue[head] 13 | head += 1 14 | for c in (0, 1, 2): 15 | if r == 0 and c == 0: 16 | continue 17 | rr = (r * 10 + c) % m 18 | if not pred[rr]: 19 | pred[rr] = (c, r) 20 | if rr == 0: 21 | return recur(pred, 0) 22 | queue.append(rr) 23 | 24 | for n in (2, 3, 7, 42, 89): 25 | print n, solve(n) 26 | result = 0 27 | for n in xrange(1, 10000 + 1): 28 | #print >> sys.stderr, n, solve(n) 29 | result += solve(n) / n 30 | print result 31 | -------------------------------------------------------------------------------- /project-euler/p318/p318.py: -------------------------------------------------------------------------------- 1 | from math import sqrt, log, ceil 2 | 3 | n, result = 2011, 0 4 | for p in xrange(1, n + 1): 5 | for q in xrange(p + 1, n + 1): 6 | if p + q <= n: 7 | r = sqrt(q) - sqrt(p) 8 | if r < 1: 9 | min_n = int(ceil(-2011 * log(10) / log(r) / 2)) 10 | result += min_n 11 | print result 12 | -------------------------------------------------------------------------------- /project-euler/p340/p340.hs: -------------------------------------------------------------------------------- 1 | -- Project Euler 340 Crazy Function 2 | main = print $ (solve (21 ^ 7) (7 ^ 21) (12 ^ 7)) `mod` (10 ^ 9) 3 | 4 | solve a b c = 4 * (a - c) * (b + 1) + arithmeticSum b + (4 * a - 3 * c) * sum a b 5 | where sum a b = let last = (b + 1 - a) `div` a 6 | in a * arithmeticSum last + (last + 1) * (b + 1 - (last + 1) * a) 7 | arithmeticSum n = (1 + n) * n `div` 2 8 | 9 | -------------------------------------------------------------------------------- /project-euler/p346.py: -------------------------------------------------------------------------------- 1 | def solve(n): 2 | b, s = 2, set() 3 | while b * b + b + 1 <= n: 4 | t = b * b + b + 1 5 | while t <= n: 6 | s.add(t) 7 | t = t * b + 1 8 | b += 1 9 | return 1 + sum(s) 10 | 11 | print solve(1000) 12 | print solve(10 ** 12) 13 | -------------------------------------------------------------------------------- /project-euler/p349.py: -------------------------------------------------------------------------------- 1 | STEP = 104 2 | # DIRS = [(1, 0), (0, 1), (-1, 0), (0, -1)] 3 | # x, y, d, blacks = 0, 0, 0, set() 4 | # for i in range(100000): 5 | # p = (x, y) 6 | # if p in blacks: 7 | # blacks.remove(p) 8 | # d += 1 9 | # else: 10 | # blacks.add(p) 11 | # d += 3 12 | # d %= 4 13 | # dx, dy = DIRS[d] 14 | # x, y = x + dx, y + dy 15 | # if (i + 1) % STEP == (10 ** 18) % STEP: 16 | # print len(blacks) % 12 # = 4 17 | # print i / STEP - len(blacks) / 12 # = 36 18 | print ((10 ** 18) / STEP - 36) * 12 + 4 19 | -------------------------------------------------------------------------------- /project-euler/p351/p351.py: -------------------------------------------------------------------------------- 1 | memory = {} 2 | 3 | def count_coprime(n): 4 | if n in memory: 5 | return memory[n] 6 | if n == 1: 7 | return 1 8 | ret, i = n * n, 2 9 | while i <= n: 10 | i_prime = n / (n / i) + 1 11 | ret -= (i_prime - i) * count_coprime(n / i) 12 | i = i_prime 13 | memory[n] = ret 14 | return ret 15 | 16 | def h(n): 17 | return (n * n - count_coprime(n) + n - 1) * 3 18 | 19 | for n in [5, 10, 1000, 100000000]: 20 | print n, h(n) 21 | -------------------------------------------------------------------------------- /project-euler/p400/p400.py: -------------------------------------------------------------------------------- 1 | def add(d, k, v): 2 | d[k] = d.get(k, 0) + v 3 | 4 | def solve(n): 5 | mod = 10 ** 18 6 | result, queues = 0, [{}, {}, {}] 7 | queues[n % 3][0] = 1 8 | for i in xrange(n, 0, -1): 9 | queues[(i + 1) % 3].clear() 10 | for k, v in queues[i % 3].items(): 11 | if k == -1: 12 | result += v 13 | result %= mod 14 | else: 15 | add(queues[(i + 2) % 3], (k ^ sg[i - 2] + 1) - 1, v) 16 | add(queues[(i + 1) % 3], (k ^ sg[i - 1] + 1) - 1, v) 17 | return result 18 | 19 | N = 10000 20 | sg = [-1, 0, 1] 21 | for i in xrange(3, N + 1): 22 | sg.append(sg[-1] + 1 ^ sg[-2] + 1) 23 | print solve(5) 24 | print solve(10) 25 | print solve(10000) 26 | -------------------------------------------------------------------------------- /project-euler/p439/p439-backup.py: -------------------------------------------------------------------------------- 1 | mod = 10 ** 9 2 | 3 | def divisors(n): 4 | global mod 5 | result, i = 0, 1 6 | while i <= n: 7 | j = n / (n / i) + 1 8 | result += (n / i) * (i + j - 1) * (j - i) / 2 9 | i = j 10 | return result % mod 11 | 12 | def solve(n): 13 | if n in memory: 14 | return memory[n] 15 | result = divisors(n) 16 | result *= result 17 | result %= mod 18 | i = 2 19 | while i <= n: 20 | j = n / (n / i) + 1 21 | result += mod - solve(n / i) * (i + j - 1) * (j - i) / 2 % mod 22 | if result >= mod: 23 | result -= mod 24 | i = j 25 | memory[n] = result 26 | return result 27 | 28 | memory = {} 29 | print solve(3) 30 | print solve(10 ** 3) 31 | print solve(10 ** 5) % 1000000000 32 | print solve(10 ** 11) % 1000000000 33 | -------------------------------------------------------------------------------- /project-euler/p491/p491.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int N = 10; 5 | 6 | int power[N + 1]; 7 | long long ways[59049][11]; 8 | 9 | int main() 10 | { 11 | power[0] = 1; 12 | for (int i = 1; i <= N; ++ i) { 13 | power[i] = power[i - 1] * 3; 14 | } 15 | memset(ways, 0, sizeof(ways)); 16 | ways[0][0] = 1; 17 | for (int mask = 0; mask < power[N]; ++ mask) { 18 | for (int r = 0; r < 11; ++ r) { 19 | for (int d = 0; d < 10; ++ d) { 20 | if (!mask && !d) { 21 | continue; 22 | } 23 | if (mask / power[d] % 3 < 2) { 24 | ways[mask + power[d]][(r * 10 + d) % 11] += ways[mask][r]; 25 | } 26 | } 27 | } 28 | } 29 | printf("%lld\n", ways[power[N] - 1][0]); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /project-euler/p500/p500.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | const int MOD = 500500507; 7 | 8 | int main() 9 | { 10 | std::vector > divs(2); 11 | int result = 1; 12 | for (int n = 2, demand = 500500; demand; ++ n) { 13 | int p = 2; 14 | while (p * p <= n && n % p != 0) { 15 | p ++; 16 | } 17 | if (p * p > n) { 18 | p = n; 19 | } 20 | if (divs[n / p].first && divs[n / p].first != p) { 21 | divs.push_back({-1, 0}); 22 | } else { 23 | divs.push_back({p, divs[n / p].second + 1}); 24 | } 25 | if (~divs[n].first && __builtin_popcount(divs[n].second) == 1) { 26 | demand --; 27 | result = (long long)result * n % MOD; 28 | } 29 | } 30 | std::cout << result << std::endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /project-euler/p503/p503.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | def p(n, k, r): 4 | return float((n + 1) * r) / (k + 1) 5 | 6 | def solve(n): 7 | dp = float(n + 1) / 2 8 | for i in xrange(1, n): 9 | low, high = 0, n - i 10 | while low < high: 11 | middle = low + high + 1 >> 1 12 | if p(n, n - i, middle) <= dp: 13 | low = middle 14 | else: 15 | high = middle - 1 16 | dp = (float((n + 1) * (low + 1) * low) / (n - i + 1) / 2 + (n - i - low) * dp) / (n - i) 17 | return dp 18 | 19 | for n in (3, 4, 10, 10 ** 6): 20 | print n, '%.10f' %(float(solve(n))) 21 | -------------------------------------------------------------------------------- /project-euler/p509/p509.py: -------------------------------------------------------------------------------- 1 | mod = 1234567890 2 | patterns = map(int, '00102010301020104010201030102010') 3 | 4 | def convolution(a, b): 5 | c = {} 6 | for i, ai in a.items(): 7 | for j, bj in b.items(): 8 | c[i ^ j] = (c.get(i ^ j, 0) + ai * bj) % mod 9 | return c 10 | 11 | def solve(n): 12 | counts, m = {}, len(patterns) 13 | for i, pi in enumerate(patterns): 14 | if i > 0: 15 | counts[pi] = counts.get(pi, 0) + ((n - i) + m) / m 16 | n /= 32 17 | sg = 5 18 | while n > 0: 19 | counts[sg] = counts.get(sg, 0) + (n + 1) / 2 20 | n /= 2 21 | sg += 1 22 | return convolution(counts, convolution(counts, counts))[0] 23 | 24 | for n in [10, 100, 123456787654321]: 25 | print "S(%d)=%d" %(n, (n * n * n - solve(n)) % mod) 26 | -------------------------------------------------------------------------------- /project-euler/p523/p523.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | def solve(n): 4 | co = [2 ** i - 1 for i in xrange(n)] 5 | dp = [0] * (n + 1) 6 | for i in xrange(1, n): 7 | for j in xrange(i): 8 | dp[i + 1] += Fraction(dp[i] + 1 + co[j], i + 1) 9 | dp[i + 1] += Fraction(dp[i], i + 1) 10 | return dp[n] 11 | 12 | if __name__ == '__main__': 13 | for n in (4, 10, 30): 14 | print 'E(%d) = %.2f' %(n, float(solve(n))) 15 | -------------------------------------------------------------------------------- /project-euler/p555.py: -------------------------------------------------------------------------------- 1 | from numba import jit 2 | 3 | @jit 4 | def sf(m, k, s): 5 | return ((m - s + 1) + (m + k - s - s)) * (k - s) // 2 6 | 7 | @jit 8 | def solve(m): 9 | result = 0 10 | for d in range(1, m): 11 | for s in range(d, m - d + 1, d): 12 | result += sf(m, s + d, s) 13 | return result 14 | 15 | print(solve(10)) 16 | print(solve(1000)) 17 | print(solve(10 ** 6)) 18 | -------------------------------------------------------------------------------- /project-euler/p596.py: -------------------------------------------------------------------------------- 1 | def count(a, b): 2 | return (a + b) * (b - a + 1) / 2 3 | 4 | def solve(n): 5 | result = 0 6 | n *= n 7 | result, i = 0, 1 8 | while i <= n: 9 | j = n / (n / i) 10 | cnt = count(i, j) 11 | cnt -= 4 * count((i + 3) / 4, j / 4) 12 | result += cnt * (n / i) 13 | i = j + 1 14 | return result * 8 + 1 15 | 16 | print solve(2) 17 | print solve(5) 18 | print solve(100) 19 | print solve(10 ** 4) 20 | print solve(10 ** 8) % (10 ** 9 + 7) 21 | -------------------------------------------------------------------------------- /project-euler/p599.sage: -------------------------------------------------------------------------------- 1 | G = SymmetricGroup(24) 2 | 3 | H = G.subgroup([ 4 | G('(7, 8, 14, 13)(3, 9, 18, 12)(4, 15, 17, 6)'), 5 | G('(1, 7, 17, 21)(2, 8, 18, 22)(3, 13, 19, 23)(4, 14, 20, 24)(5, 6, 12, 11)(10, 9, 15, 16)'), 6 | G('(5, 7, 9, 24)(6, 8, 10, 23)(11, 13, 15, 22)(12, 14, 16, 21)(3, 4, 2, 1)(17, 18, 20, 19)'), 7 | G('(3, 9, 18, 12)(1, 10, 20, 11)(4, 15, 17, 6)(2, 16, 19, 5)(7, 8, 14, 13)(22, 21, 23, 24)') 8 | ]) 9 | 10 | def solve(c): 11 | result = 0 12 | for g in H.conjugacy_classes_representatives(): 13 | result += c ^ len(g.cycle_type()) / H.centralizer(g).order() 14 | return result 15 | 16 | print solve(2) 17 | print solve(10) 18 | -------------------------------------------------------------------------------- /project-euler/p601.sage: -------------------------------------------------------------------------------- 1 | def solve(s, n): 2 | l = 1 3 | for i in range(1, s + 1): 4 | l = lcm(l, i) 5 | n = (n - 2) // l 6 | g = gcd(l, s + 1) 7 | d = (s + 1) // g 8 | return n - n // d 9 | 10 | print solve(3, 14) 11 | print solve(6, 10 ** 6) 12 | result = 0 13 | for i in range(1, 32): 14 | result += solve(i, 4 ** i) 15 | print result 16 | -------------------------------------------------------------------------------- /project-euler/p603.sage: -------------------------------------------------------------------------------- 1 | F = GF(10 ** 9 + 7) 2 | 3 | def solve(n, m): 4 | a = matrix.identity(F, 4, 4) 5 | bs = [] 6 | for d in range(10): 7 | b = [[0] * 4 for _ in range(4)] 8 | b[0][0] = b[0][1] = b[1][1] = b[2][3] = b[3][3] = F(1) 9 | b[1][2] = F(d) 10 | b[2][2] = F(10) 11 | bs.append(matrix(b)) 12 | primes = prime_range(n * 20)[:n] 13 | sys.stderr.write("Generated prime\n") 14 | for p in primes: 15 | for d in map(int, str(p)): 16 | a *= bs[d] 17 | sys.stderr.write("Constructed matrix\n") 18 | w = vector([1, 1, 0, 0]) * (a ** m) 19 | return w[2] + w[3] 20 | 21 | print solve(10 ** 6, 10 ** 12) 22 | -------------------------------------------------------------------------------- /project-euler/p605.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | MOD = 10 ** 8 4 | 5 | def solve(n, k): 6 | r = n if k == 1 else k - 1 7 | p = pow(2, n - r - 1, MOD) * (r * (pow(2, n, MOD) - 1) + n) 8 | q = (pow(2, n, MOD) - 1) ** 2 9 | return p * q % MOD 10 | 11 | print(solve(10 ** 8 + 7, 10 ** 4 + 7)) 12 | -------------------------------------------------------------------------------- /project-euler/p78/solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const int MOD = 1000000; 5 | 6 | int pentagonal(int n) { 7 | int k = (n >> 1) + 1; 8 | if (n & 1) { 9 | k *= -1; 10 | } 11 | return k * (3 * k - 1) / 2; 12 | } 13 | 14 | int main() { 15 | std::vector ways(1, 1); 16 | while (ways.back() != 0) { 17 | int n = ways.size(); 18 | ways.push_back(0); 19 | for (int i = 0; pentagonal(i) <= n; ++ i) { 20 | int delta = ways[n - pentagonal(i)]; 21 | if (i >> 1 & 1) { 22 | delta *= -1; 23 | } 24 | ways.back() += delta; 25 | } 26 | ways.back() %= MOD; 27 | } 28 | printf("%d\n", (int)ways.size() - 1); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /regional/neerc-2007/B.cpp: -------------------------------------------------------------------------------- 1 | // Northeastern Europe 2007 2 | // Problem B -- Building for UN 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int n; 8 | 9 | char map(int i) { 10 | if (i < 26) { 11 | return 'a' + i; 12 | } 13 | return 'A' + i - 26; 14 | } 15 | 16 | int main() { 17 | scanf("%d", &n); 18 | printf("%d %d %d\n", 2, n, n); 19 | for (int i = 0; i < n; ++ i) { 20 | for (int j = 0; j < n; ++ j) { 21 | printf("%c", map(i)); 22 | } 23 | puts(""); 24 | } 25 | puts(""); 26 | for (int i = 0; i < n; ++ i) { 27 | for (int j = 0; j < n; ++ j) { 28 | printf("%c", map(j)); 29 | } 30 | puts(""); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /sgu/p100.cpp: -------------------------------------------------------------------------------- 1 | // SGU 100 -- A+B 2 | #include 3 | 4 | int main() { 5 | int a, b; 6 | scanf("%d%d", &a, &b); 7 | printf("%d\n", a + b); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /sgu/p102.cpp: -------------------------------------------------------------------------------- 1 | // SGU 102 -- Coprimes 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int phi(int n) { 7 | int result = n; 8 | for (int i = 2; i * i <= n; ++ i) { 9 | if (n % i == 0) { 10 | result = result / i * (i - 1); 11 | while (n % i == 0) { 12 | n /= i; 13 | } 14 | } 15 | } 16 | if (n > 1) { 17 | result = result / n * (n - 1); 18 | } 19 | return result; 20 | } 21 | 22 | int main() { 23 | int n; 24 | scanf("%d", &n); 25 | printf("%d\n", phi(n)); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /sgu/p105.cpp: -------------------------------------------------------------------------------- 1 | // SGU 105 -- Div 3 2 | #include 3 | using namespace std; 4 | 5 | int solve(int n) { 6 | if (n % 3 < 2) { 7 | return n / 3 * 2; 8 | } 9 | return n / 3 * 2 + 1; 10 | } 11 | 12 | int main() { 13 | int n; 14 | cin >> n; 15 | cout << solve(n) << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /sgu/p107.cpp: -------------------------------------------------------------------------------- 1 | // SGU 107 -- 987654321 problem 2 | #include 3 | 4 | int main() { 5 | int n; 6 | scanf("%d", &n); 7 | if (n < 9) { 8 | puts("0"); 9 | } else if (n == 9) { 10 | puts("8"); 11 | } else { 12 | printf("72"); 13 | for (int i = 0; i < n - 10; ++ i) { 14 | printf("0"); 15 | } 16 | puts(""); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sgu/p113.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check(int n) { 5 | int prime_count = 0; 6 | for (int i = 2; i * i <= n; ++ i) { 7 | while (n % i == 0) { 8 | prime_count ++; 9 | n /= i; 10 | } 11 | } 12 | if (n > 1) { 13 | prime_count ++; 14 | } 15 | return prime_count == 2; 16 | } 17 | 18 | int main() { 19 | int test_count; 20 | scanf("%d", &test_count); 21 | while (test_count > 0) { 22 | test_count --; 23 | int n; 24 | scanf("%d", &n); 25 | puts(check(n)? "Yes": "No"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /sgu/p117.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int n, m, k; 6 | 7 | int pow_mod(int a, int n, int m) { 8 | int result = 1 % m; 9 | while (n > 0) { 10 | if ((n & 1) == 1) { 11 | result = (result * a) % m; 12 | } 13 | a = (a * a) % m; 14 | n >>= 1; 15 | } 16 | return result; 17 | } 18 | 19 | int main() { 20 | scanf("%d%d%d", &n, &m, &k); 21 | int result = 0; 22 | for (int i = 0; i < n; ++ i) { 23 | int a_i; 24 | scanf("%d", &a_i); 25 | if (pow_mod(a_i, m, k) == 0) { 26 | result ++; 27 | } 28 | } 29 | printf("%d\n", result); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /sgu/p118.cpp: -------------------------------------------------------------------------------- 1 | // SGU 118 -- Digital Root 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int test_count; 8 | scanf("%d", &test_count); 9 | while (test_count > 0) { 10 | test_count --; 11 | int n; 12 | scanf("%d", &n); 13 | int a[n]; 14 | for (int i = 0; i < n; ++ i) { 15 | scanf("%d", a + i); 16 | } 17 | if (a[0] == 0) { 18 | puts("0"); 19 | } else { 20 | int result = 0; 21 | for (int i = n - 1; i >= 0; -- i) { 22 | result = ((long long)result + 1) * a[i] % 9; 23 | } 24 | printf("%d\n", result == 0? 9: result); 25 | } 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /sgu/p127.cpp: -------------------------------------------------------------------------------- 1 | // SGU 127 -- Telephone directory 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int m, n, number_count[10]; 7 | 8 | int main() { 9 | scanf("%d%d", &m, &n); 10 | memset(number_count, 0, sizeof(number_count)); 11 | for (int i = 0; i < n; ++ i) { 12 | int number; 13 | scanf("%d", &number); 14 | int first = number / 1000; 15 | number_count[first] ++; 16 | } 17 | int result = 2; 18 | for (int i = 0; i < 10; ++ i) { 19 | result += (number_count[i] + (m - 1)) / m; 20 | } 21 | printf("%d\n", result); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /sgu/p130.cpp: -------------------------------------------------------------------------------- 1 | // SGU 130 -- Circle 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int N = 33; 7 | 8 | int n; 9 | long long ways[N]; 10 | 11 | int main() { 12 | cin >> n; 13 | memset(ways, 0, sizeof(ways)); 14 | ways[0] = 1; 15 | for (int i = 1; i <= n; ++ i) { 16 | for (int j = 0; j <= i - 1; ++ j) { 17 | ways[i] += ways[j] * ways[i - 1 - j]; 18 | } 19 | } 20 | cout << ways[n] << " " << n + 1 << endl; 21 | } 22 | -------------------------------------------------------------------------------- /sgu/p135.cpp: -------------------------------------------------------------------------------- 1 | // SGU 135 -- Drawing Lines 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | long long n; 7 | cin >> n; 8 | cout << 1 + n * (n + 1) / 2 << endl; 9 | } 10 | -------------------------------------------------------------------------------- /sgu/p139.cpp: -------------------------------------------------------------------------------- 1 | // SGU 139 -- Help Needed! 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int map[16]; 7 | 8 | int main() { 9 | for (int i = 0; i < 16; ++ i) { 10 | scanf("%d", map + i); 11 | } 12 | int result = 0; 13 | for (int i = 0; i < 16; ++ i) { 14 | if (map[i] == 0) { 15 | result += (3 - (i / 4)) + (3 - (i % 4)); 16 | } 17 | for (int j = i + 1; j < 16; ++ j) { 18 | if (map[i] > map[j]) { 19 | result ++; 20 | } 21 | } 22 | } 23 | puts(result % 2 == 1? "YES": "NO"); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /sgu/p157.cpp: -------------------------------------------------------------------------------- 1 | // SGU 157 -- Patience 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int result[13] = {1, 2, 5, 14, 47, 189, 891, 4815, 29547, 203173, 1548222, 12966093, 118515434}; 7 | 8 | int main() { 9 | int n; 10 | scanf("%d", &n); 11 | printf("%d\n", result[n - 1]); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /sgu/p163.cpp: -------------------------------------------------------------------------------- 1 | // SGU 163 -- Wise King 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int n, k; 7 | 8 | int power(int a, int n) { 9 | int result = 1; 10 | for (int i = 0; i < n; ++ i) { 11 | result *= a; 12 | } 13 | return result; 14 | } 15 | 16 | int main() { 17 | scanf("%d%d", &n, &k); 18 | int result = 0; 19 | for (int i = 0; i < n; ++ i) { 20 | int a_i; 21 | scanf("%d", &a_i); 22 | if (power(a_i, k) > 0) { 23 | result += power(a_i, k); 24 | } 25 | } 26 | printf("%d\n", result); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /sgu/p169.cpp: -------------------------------------------------------------------------------- 1 | // SGU 169 -- Numbers 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int length; 8 | scanf("%d", &length); 9 | int prefix = 0; 10 | for (int i = 0; i < length; ++ i) { 11 | prefix = (prefix * 10 + (i == length - 1? 0: 1)) % 2520; 12 | } 13 | int result = 0; 14 | for (int digit = 1; digit < 9; ++ digit) { 15 | if ((prefix + digit) % digit == 0 && (prefix + digit + 1) % (digit + 1) == 0) { 16 | result ++; 17 | } 18 | } 19 | printf("%d\n", result); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /sgu/p175.cpp: -------------------------------------------------------------------------------- 1 | // SGU 175 -- Encoding 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int solve(int n, int q) { 7 | if (n == 1) { 8 | return 1; 9 | } 10 | int k = n / 2; 11 | if (q <= k) { 12 | return n - k + solve(k, k - q + 1); 13 | } 14 | return solve(n - k, n - q + 1); 15 | } 16 | 17 | int main() { 18 | int n, q; 19 | scanf("%d%d", &n, &q); 20 | printf("%d\n", solve(n, q)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /sgu/p178.cpp: -------------------------------------------------------------------------------- 1 | // SGU 178 -- Golden chain 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | long long n; 8 | cin >> n; 9 | if (n == 1) { 10 | puts("0"); 11 | } else { 12 | int result = 1; 13 | while (true) { 14 | long long total = result; 15 | int left = result + 1; 16 | while (left > 0) { 17 | left --; 18 | long long next = total + 1; 19 | total += next; 20 | } 21 | if (total >= n) { 22 | printf("%d\n", result); 23 | return 0; 24 | } 25 | result ++; 26 | } 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /sgu/p184.cpp: -------------------------------------------------------------------------------- 1 | // SGU 184 -- Patties 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int a, b, c, x, y, z; 9 | scanf("%d%d%d%d%d%d", &a, &b, &c, &x, &y, &z); 10 | printf("%d\n", min(min(a / x, b / y), c / z)); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /sgu/p186.cpp: -------------------------------------------------------------------------------- 1 | // SGU 186 -- The Chain 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int n; 9 | scanf("%d", &n); 10 | int length[n]; 11 | for (int i = 0; i < n; ++ i) { 12 | scanf("%d", length + i); 13 | } 14 | sort(length, length + n); 15 | int rest = n - 1; 16 | int result = 0; 17 | for (int i = 0; i < n; ++ i) { 18 | if (rest <= 0) { 19 | break; 20 | } 21 | result += min(rest, length[i]); 22 | rest -= length[i] + 1; 23 | } 24 | printf("%d\n", result); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /sgu/p196.cpp: -------------------------------------------------------------------------------- 1 | // SGU 196 -- Matrix Multiplication 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 10001; 8 | 9 | int n, m, degree[N]; 10 | 11 | int main() { 12 | scanf("%d%d", &n, &m); 13 | memset(degree, 0, sizeof(degree)); 14 | for (int i = 0; i < m; ++ i) { 15 | int a, b; 16 | scanf("%d%d", &a, &b); 17 | degree[a] ++; 18 | degree[b] ++; 19 | } 20 | long long result = 0; 21 | for (int i = 1; i <= n; ++ i) { 22 | result += degree[i] * degree[i]; 23 | } 24 | cout << result << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /sgu/p220: -------------------------------------------------------------------------------- 1 | p221 -------------------------------------------------------------------------------- /sgu/p228.cpp: -------------------------------------------------------------------------------- 1 | p120.cpp -------------------------------------------------------------------------------- /sgu/p231.cpp: -------------------------------------------------------------------------------- 1 | // SGU 231 -- Prime Sum 2 | #include 3 | #include 4 | 5 | const int N = 1000000; 6 | 7 | int n; 8 | bool is_prime[N + 1]; 9 | 10 | int main() { 11 | scanf("%d", &n); 12 | memset(is_prime, true, sizeof is_prime); 13 | for (int i = 2; i <= n; ++ i) { 14 | if (is_prime[i]) { 15 | for (long long j = (long long)i * i; j <= n; j += i) { 16 | is_prime[j] = false; 17 | } 18 | } 19 | } 20 | int answer = 0; 21 | for (int i = 3; i + 2 <= n; ++ i) { 22 | answer += is_prime[i] && is_prime[i + 2]; 23 | } 24 | printf("%d\n", answer); 25 | for (int i = 3; i + 2 <= n; ++ i) { 26 | if (is_prime[i] && is_prime[i + 2]) { 27 | printf("%d %d\n", 2, i); 28 | } 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /sgu/p249.cpp: -------------------------------------------------------------------------------- 1 | // SGU 249 -- Matrix 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | vector construct(int n) { 8 | vector result(1, 0); 9 | for (int i = 1; i < 1 << n; ++ i) { 10 | result.push_back(result.back() ^ (i & -i)); 11 | } 12 | return result; 13 | } 14 | 15 | int main() { 16 | int n, m; 17 | scanf("%d%d", &n, &m); 18 | vector row = construct(n); 19 | vector colume = construct(m); 20 | for (int i = 0; i < 1 << n; ++ i) { 21 | for (int j = 0; j < 1 << m; ++ j) { 22 | printf("%d%c", (row[i] << m) | colume[j], j == (1 << m) - 1? '\n': ' '); 23 | } 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /sgu/p347.cpp: -------------------------------------------------------------------------------- 1 | // SGU 347 -- Join the Strings 2 | #include 3 | #include 4 | #include 5 | 6 | const int N = 100; 7 | 8 | std::string strings[N]; 9 | 10 | bool compare(const std::string &a, const std::string &b) { 11 | return a + b < b + a; 12 | } 13 | 14 | int main() { 15 | int n; 16 | std::cin >> n; 17 | for (int i = 0; i < n; ++ i) { 18 | std::cin >> strings[i]; 19 | } 20 | std::sort(strings, strings + n, compare); 21 | for (int i = 0; i < n; ++ i) { 22 | std::cout << strings[i]; 23 | } 24 | std::cout << std::endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /sgu/p355.cpp: -------------------------------------------------------------------------------- 1 | // SGU 355 -- Numbers Painting 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 10000; 8 | 9 | int n, maximum[N + 1]; 10 | 11 | int main() { 12 | scanf("%d", &n); 13 | memset(maximum, 0, sizeof(maximum)); 14 | for (int i = 1; i <= n; ++ i) { 15 | maximum[i] = max(maximum[i], 1); 16 | for (int j = 2 * i; j <= n; j += i) { 17 | maximum[j] = max(maximum[j], maximum[i] + 1); 18 | } 19 | } 20 | int limit = *max_element(maximum + 1, maximum + 1 + n); 21 | printf("%d\n", limit); 22 | for (int i = 1; i <= n; ++ i) { 23 | printf("%d%c", maximum[i], i == n? '\n': ' '); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /sgu/p361.cpp: -------------------------------------------------------------------------------- 1 | // SGU 361 -- National Flag 2 | #include 3 | #include 4 | #include 5 | 6 | const int N = 200; 7 | 8 | int n, m; 9 | 10 | int main() { 11 | scanf("%d%d", &n, &m); 12 | for (int i = 0; i < n; ++ i) { 13 | for (int j = 0; j < m; ++ j) { 14 | putchar(i % 3 + j % 3 == 2 ? '#' : '0'); 15 | } 16 | puts(""); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sgu/p375.cpp: -------------------------------------------------------------------------------- 1 | // SGU 375 -- Amplifiers 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int n; 9 | scanf("%d", &n); 10 | if (n % 2 == 0) { 11 | puts("No solution"); 12 | } else { 13 | vector ways; 14 | while (n != 1) { 15 | if ((n + 1) % 4 == 2) { 16 | ways.push_back(1); 17 | n = (n + 1) / 2; 18 | } else { 19 | ways.push_back(2); 20 | n = (n - 1) / 2; 21 | } 22 | } 23 | printf("%d\n", (int)ways.size()); 24 | for (int i = (int)ways.size() - 1; i >= 0; -- i) { 25 | printf("%d%c", ways[i], i == 0? '\n': ' '); 26 | } 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /sgu/p409.cpp: -------------------------------------------------------------------------------- 1 | // SGU 409 -- Berland Flag 2 | #include 3 | #include 4 | 5 | const int N = 20; 6 | 7 | int n, k, pattern[N][N]; 8 | 9 | int main() { 10 | scanf("%d%d", &n, &k); 11 | for (int i = 0; i < n; ++ i) { 12 | for (int j = 0; j < n; ++ j) { 13 | pattern[i][j] = i * n + j < k ? '*' : '.'; 14 | } 15 | } 16 | for (int i = 0; i < n * n; ++ i) { 17 | for (int j = 0; j < n * n; ++ j) { 18 | int x = j / n; 19 | int y = i / n; 20 | putchar(pattern[(x + i) % n][(y + j) % n]); 21 | } 22 | puts(""); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /sgu/p444.cpp: -------------------------------------------------------------------------------- 1 | // 444. Headstrong Student 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int N = 1000000; 7 | 8 | int mark[N]; 9 | 10 | int main() { 11 | int a, b; 12 | scanf("%d%d", &a, &b); 13 | a %= b; 14 | memset(mark, -1, sizeof(mark)); 15 | int length = 0; 16 | while (a) { 17 | if (mark[a] != -1) { 18 | printf("%d %d\n", mark[a], length - mark[a]); 19 | return 0; 20 | } 21 | mark[a] = length; 22 | (a *= 10) %= b; 23 | length ++; 24 | } 25 | printf("%d %d\n", length, 0); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /training/2012-06-18-mid-central-usa-2006-and-2007/A.cpp: -------------------------------------------------------------------------------- 1 | // Problem A -- Quicksum 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | char buffer[300]; 8 | while (true) { 9 | fgets(buffer, 300, stdin); 10 | if (*buffer == '#') { 11 | break; 12 | } 13 | int length = strlen(buffer); 14 | int result = 0; 15 | for (int i = 0; i < length; ++ i) { 16 | if ('A' <= buffer[i] && buffer[i] <= 'Z') { 17 | result += (i + 1) * (buffer[i] -'A' + 1); 18 | } 19 | } 20 | printf("%d\n", result); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /training/2012-07-09-usu-personal-contest-2007/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int n, p, ways[10000001]; 6 | 7 | int main() { 8 | scanf("%d%d", &n, &p); 9 | memset(ways, 0, sizeof(ways)); 10 | ways[2] = 1 % p; 11 | for (int i = 3; i <= n; ++ i) { 12 | ways[i] = ways[i - 1]; 13 | if ((i & 1) == 0) { 14 | ways[i] += ways[i >> 1]; 15 | ways[i] %= p; 16 | } 17 | } 18 | printf("%d\n", ways[n]); 19 | return 0; 20 | } 21 | --------------------------------------------------------------------------------