├── Competitve Programming 3, Steven Halim ├── BookChapterStevenFelixFinal25October2009.pdf ├── Code │ ├── ch1 │ │ ├── ch1_01_factorial.java │ │ ├── ch1_02_scanf.cpp │ │ └── ch1_02_scanf_input.txt │ ├── ch2 │ │ ├── ch2_01_array_vector.cpp │ │ ├── ch2_01_array_vector.java │ │ ├── ch2_02_algorithm_collections.cpp │ │ ├── ch2_02_algorithm_collections.java │ │ ├── ch2_03_bit_manipulation.cpp │ │ ├── ch2_03_bit_manipulation.java │ │ ├── ch2_04_stack_queue.cpp │ │ ├── ch2_04_stack_queue.java │ │ ├── ch2_05_map_set.cpp │ │ ├── ch2_05_map_set.java │ │ ├── ch2_06_priority_queue.cpp │ │ ├── ch2_06_priority_queue.java │ │ ├── ch2_07_graph_ds.cpp │ │ ├── ch2_07_graph_ds.java │ │ ├── ch2_08_unionfind_ds.cpp │ │ ├── ch2_08_unionfind_ds.java │ │ ├── ch2_09_segmenttree_ds.cpp │ │ ├── ch2_09_segmenttree_ds.java │ │ ├── ch2_10_fenwicktree_ds.cpp │ │ ├── ch2_10_fenwicktree_ds.java │ │ └── in_07.txt │ ├── ch3 │ │ ├── ch3_01_UVa750.cpp │ │ ├── ch3_01_UVa750.java │ │ ├── ch3_02_UVa11450_td.cpp │ │ ├── ch3_02_UVa11450_td.java │ │ ├── ch3_03_UVa11450_bu.cpp │ │ ├── ch3_03_UVa11450_bu.java │ │ ├── ch3_04_Max1DRangeSum.cpp │ │ ├── ch3_04_Max1DRangeSum.java │ │ ├── ch3_05_UVa108.cpp │ │ ├── ch3_05_UVa108.java │ │ ├── ch3_06_LIS.cpp │ │ ├── ch3_06_LIS.java │ │ ├── ch3_07_UVa10130.cpp │ │ ├── ch3_07_UVa10130.java │ │ ├── ch3_08_UVa674.cpp │ │ ├── ch3_08_UVa674.java │ │ ├── ch3_09_UVa10496.cpp │ │ ├── ch3_09_UVa10496.java │ │ ├── ch3_10_UVa10943.cpp │ │ ├── ch3_10_UVa10943.java │ │ ├── ch3_11_UVa10003.cpp │ │ └── ch3_11_UVa10003.java │ ├── ch4 │ │ ├── IntegerPair.java │ │ ├── IntegerTriple.java │ │ ├── ch4_01_dfs.cpp │ │ ├── ch4_01_dfs.java │ │ ├── ch4_02_UVa469.cpp │ │ ├── ch4_02_UVa469.java │ │ ├── ch4_03_kruskal_prim.cpp │ │ ├── ch4_03_kruskal_prim.java │ │ ├── ch4_04_bfs.cpp │ │ ├── ch4_04_bfs.java │ │ ├── ch4_05_dijkstra.cpp │ │ ├── ch4_05_dijkstra.java │ │ ├── ch4_06_bellman_ford.cpp │ │ ├── ch4_06_bellman_ford.java │ │ ├── ch4_07_floyd_warshall.cpp │ │ ├── ch4_07_floyd_warshall.java │ │ ├── ch4_08_edmonds_karp.cpp │ │ ├── ch4_08_edmonds_karp.java │ │ ├── ch4_09_mcbm.cpp │ │ ├── ch4_09_mcbm.java │ │ ├── in_01.txt │ │ ├── in_03.txt │ │ ├── in_04.txt │ │ ├── in_05.txt │ │ ├── in_06.txt │ │ ├── in_07.txt │ │ └── in_08.txt │ ├── ch5 │ │ ├── ch5_01_UVa10925.java │ │ ├── ch5_02_UVa10551.java │ │ ├── ch5_03_UVa10235.java │ │ ├── ch5_04_UVa10814.java │ │ ├── ch5_05_UVa1230.java │ │ ├── ch5_06_primes.cpp │ │ ├── ch5_06_primes.java │ │ ├── ch5_07_UVa350.cpp │ │ └── ch5_07_UVa350.java │ ├── ch6 │ │ ├── ch6-out.txt │ │ ├── ch6.txt │ │ ├── ch6_01_basic_string.cpp │ │ ├── ch6_01_basic_string.html │ │ ├── ch6_01_basic_string.java │ │ ├── ch6_02_kmp.cpp │ │ ├── ch6_02_kmp.java │ │ ├── ch6_03_str_align.cpp │ │ ├── ch6_03_str_align.java │ │ ├── ch6_04_sa.cpp │ │ └── ch6_04_sa.java │ ├── ch7 │ │ ├── ch7_01_points_lines.cpp │ │ ├── ch7_01_points_lines.java │ │ ├── ch7_02_circles.cpp │ │ ├── ch7_02_circles.java │ │ ├── ch7_03_triangles.cpp │ │ ├── ch7_03_triangles.java │ │ ├── ch7_04_polygon.cpp │ │ └── ch7_04_polygon.java │ ├── ch8 │ │ ├── ch8_01_UVa10181.cpp │ │ ├── ch8_01_UVa10181.java │ │ ├── ch8_02_UVa10911.cpp │ │ ├── ch8_02_UVa10911.java │ │ ├── ch8_03_UVa1231.cpp │ │ ├── ch8_03_UVa1231.java │ │ ├── ch8_04_UVa1079.cpp │ │ └── ch8_04_UVa1079.java │ └── ch9 │ │ ├── GaussianElimination.cpp │ │ ├── GaussianElimination.java │ │ ├── LCA.cpp │ │ ├── LCA.java │ │ ├── Pollardsrho.cpp │ │ ├── Pollardsrho.java │ │ ├── SparseTable.cpp │ │ ├── SparseTable.java │ │ ├── UVa10229.cpp │ │ ├── UVa10229.java │ │ ├── UVa10986.cpp │ │ ├── UVa10986.java │ │ ├── UVa11616.cpp │ │ ├── UVa11616.java │ │ ├── UVa11817.cpp │ │ ├── UVa11817.java │ │ ├── UVa11838.cpp │ │ └── UVa11838.java ├── Competitive_Programming_3.pdf ├── Slides │ ├── week01_introduction.pdf │ ├── week02_ds_libraries.pdf │ ├── week03_search.pdf │ ├── week04_dp_1.pdf │ ├── week05_dp_2.pdf │ ├── week05_graph_1.pdf │ ├── week07_graph_1_network_flow.pdf │ ├── week08_graph_2_matching.pdf │ ├── week09_maths.pdf │ ├── week10_string.pdf │ ├── week11_geometry.pdf │ └── week12_harder_stuffs.pdf └── skillset.xls ├── Library ├── Fegla │ ├── ACM training sessions.odt │ ├── Suffix arrays.odt │ ├── Useful Mathematical Relations.pdf │ └── coachLibrary.docx ├── Library.aux ├── Library.log ├── Library.pdf ├── Library.synctex.gz ├── Library.tex └── Library.toc └── Solutions ├── -in-progress ├── 00104 Arbitage .pdf ├── 00104 Arbitage RTE.cpp ├── 00601 path WA.cpp ├── 101137 E WA.cpp ├── 101137 F WA.cpp ├── 10150 Doublets RTE.cpp ├── 1099.pdf ├── 12479 Interesting Route !!.cpp ├── 2016, 04, 24, ANARC07G TLE.cpp ├── 2016, 05, 01, 01229 Sub-dictionary WA_2.cpp ├── 2016, 05, 07, LITE TLE.cpp ├── 2016, 05, 1, 01229 WA.cpp ├── 2016, 05, 1, 01229 WA_2.cpp ├── 2016, 05, 1, 01229.pdf ├── 2016, 05, 12, 11294 Wedding !!.cpp ├── 2016, 05, 12, 11294 Wedding !!.pdf ├── 2016, 06, 09 12125 March of the Penguins WA.cpp ├── 219 C TLE.cpp ├── 234 C WA.cpp ├── 4 11167 Monkeys in the Emei Mountain_WA.cpp ├── 5 1700 Game Strategy WA.cpp ├── 567 C TLE.cpp ├── 598 D TLE.cpp ├── 8 01222 Bribing FIBA RTE.cpp ├── 8 10163 Storage Keepers WA.cpp ├── 8 10604 Chemical Reaction WA.cpp ├── BUZZ !!.cpp ├── Let's go to the movies TLE.cpp ├── Live Archvie Christmas Tree.cpp └── Pyramids TLE.cpp ├── CodeForces ├── 101064 G.cpp ├── 101102 B The Little Match Girl.cpp ├── 101102 J.cpp ├── 101102 K Topological Sort.cpp ├── 103 B.cpp ├── 104 A.cpp ├── 115 A.cpp ├── 118 D.cpp ├── 125C Hobbits' Party.cpp ├── 131 E.cpp ├── 165 C.cpp ├── 166 E.cpp ├── 182E Wooden Fence.cpp ├── 189A Cut Ribbon.cpp ├── 202A - LLPS.cpp ├── 208251 D.cpp ├── 217 A.cpp ├── 248 B.cpp ├── 255 C.cpp ├── 276C -Little Girl and Maximum Sum.cpp ├── 277 A.cpp ├── 279C Ladder.cpp ├── 27B.cpp ├── 28B.cpp ├── 291C Network Mask.cpp ├── 292 D.cpp ├── 2B - The least round way.cpp ├── 313 B.cpp ├── 349 B.cpp ├── 34D - Road Map.cpp ├── 371C - Hamburgers.cpp ├── 381A - Sereja and Dima.cpp ├── 431 D.cpp ├── 459 E.cpp ├── 467 C.cpp ├── 474 D.cpp ├── 475 A.cpp ├── 486 B. OR in Matrix.cpp ├── 490 B. Queue.cpp ├── 493 D.cpp ├── 4B.cpp ├── 4D.cpp ├── 51C - Three Base Stations.cpp ├── 524C The Art of Dealing with ATM.cpp ├── 546 D.cpp ├── 578 C.cpp ├── 597 A.cpp ├── 598 A.cpp ├── 598 B.cpp ├── 59B.cpp ├── 616D - Longest k-Good Segment.cpp ├── 622 A.cpp ├── 622 B.cpp ├── 625 C.cpp ├── 628 A.cpp ├── 628 B.cpp ├── 628 C.cpp ├── 659 A.cpp ├── 659 B.cpp ├── 659 C.cpp ├── 659 D.cpp ├── 659 E.cpp ├── 659 F.cpp ├── 660 A.cpp ├── 660 B.cpp ├── 660 C.cpp ├── 660 D.cpp ├── 682C Alyona and the Tree.cpp ├── 689 A.cpp ├── 689 B.cpp ├── 689 C.cpp ├── 702C - Cellular Network.cpp ├── 702D - Road to Post Office.cpp ├── 703 A. Mishka and Game.cpp ├── 703 B. Mishka and trip.cpp ├── 709 A. Juicer.cpp ├── 709 C. Letters Cyclic Shift.cpp ├── 710C Magic Odd Square.cpp ├── 717C Potions Homework.cpp ├── 721C Journey.cpp ├── 722C Destroying Array.cpp ├── 723C Polycarp at the Radio (1).cpp ├── 723C Polycarp at the Radio.cpp ├── 723D Lakes in Berland (1).cpp ├── 723D Lakes in Berland.cpp ├── 74C - Chessboard Billiard.cpp ├── 84C Biathlon.cpp ├── 94 A.cpp ├── 96B - Lucky Numbers (easy).cpp ├── A2OJ ladder 16 │ ├── 109C.cpp │ ├── 109C.cpp~ │ ├── 113B.cpp │ ├── 113B.cpp~ │ ├── 115A.cpp │ ├── 118D.cpp │ ├── 144D.cpp │ ├── 144D.cpp~ │ ├── 189A.cpp │ ├── 204A.cpp │ ├── 204B.cpp │ ├── 217A.cpp │ ├── 225C.cpp │ ├── 230B.cpp │ ├── 242C.cpp │ ├── 246D.cpp │ ├── 255C.cpp │ ├── 258A.cpp │ ├── 264B.cpp │ ├── 271D.cpp │ ├── 271D.cpp~ │ ├── 276C.cpp │ ├── 295A.cpp │ ├── 295B.cpp │ ├── 334A.cpp │ ├── 339C.cpp │ ├── 339D.cpp │ ├── 349B.cpp │ ├── 359D.cpp~ │ ├── 364A.cpp │ ├── 369C.cpp │ ├── 371C.cpp │ ├── 372B.cpp │ ├── 375B.cpp │ ├── 383D.cpp │ ├── 383D.cpp~ │ ├── 400D.cpp │ ├── 400D.cpp~ │ ├── 427C.cpp │ ├── 431C.cpp │ ├── 446A.cpp │ ├── 455C.cpp │ ├── 455C.cpp~ │ ├── 459E.cpp │ ├── 463C.cpp │ ├── 463C.cpp~ │ ├── 471D.cpp │ ├── 471D.cpp~ │ ├── 476A.cpp │ ├── 476B.cpp │ ├── 494A.cpp │ ├── 546D.cpp │ ├── 547B.cpp │ ├── 550D.cpp │ ├── 550D.cpp~ │ ├── 59A.cpp │ ├── 61E.cpp │ └── 61E.cpp~ ├── AAST ACM ICPC 2016 - Individuals Qualification #2 │ ├── A - Balanced Strings.cpp │ ├── B - Brownies vs. Candies vs. Cookies.cpp │ ├── C - Towers of Hanoi Grid.cpp │ ├── D - Lemonade Stand.cpp │ ├── F - Medal Ranking.cpp │ ├── H - Rain Gauge.cpp │ ├── I - Reach for the Stars.cpp │ ├── K - Find the Twins.cpp │ └── contest_link.txt ├── AUC ACM ICPC 2016 - Individuals Qualification #1 │ ├── B - Heavy Caffeine Helps.cpp │ ├── D - Crazy Vacation.cpp │ ├── F - Mountain Scenes.cpp │ ├── I - Tourists.cpp │ ├── J - Balloons Colors.cpp │ ├── K - Primal Partitions.cpp │ └── contest_link.txt ├── AUC ACM ICPC 2016 - Individuals Qualification #3 │ ├── A - Equilaterals Intersection.cpp │ ├── C - Gold for the Bride.cpp │ ├── I. International Wedding.cpp │ ├── J. Delete the Letters.cpp │ ├── L - Give Me My Balloon!.cpp │ └── contest_link.txt └── USP Tryouts 2010 │ ├── B - Pharaoh's Bank.cpp │ ├── C - It-miha.cpp │ ├── E - Nefertiti, the Queen of Egypt.cpp │ ├── G - Ramses' Games.cpp │ └── Problems - Codeforces.pdf ├── Codility ├── SlalomSkiing.cpp └── SlalomSkiing.cpp~ ├── Foobar ├── 01 │ ├── challenge_2.java │ ├── challenge_3_3.java │ ├── challenge_4_1.java │ ├── challenge_4_2.java │ ├── challenge_5.java │ └── decode.py └── 02 │ ├── challenge_2_2.java │ ├── challenge_3_1.py │ ├── challenge_3_2.py │ ├── challenge_3_3.py │ ├── challenge_4_1.py │ ├── challenge_4_2.py │ ├── challenge_5.py │ └── decode.py ├── ICPC ├── 2013 ACPC │ ├── 1.cpp │ ├── 10.cpp │ ├── 11.cpp │ ├── 12.cpp │ ├── 2.cpp │ ├── 3.cpp │ ├── 5.cpp │ ├── 6.cpp │ ├── 7.cpp │ └── 9.cpp ├── 2015 ECPC │ ├── A. Arcade Game.cpp │ ├── A. Arcade Game_2.cpp │ ├── B. Unlucky Teacher.cpp │ ├── C Connecting Graph.cpp │ ├── D. Frozen Rivers.cpp │ ├── E. Palmyra.cpp │ ├── F. Geometry.cpp │ ├── G. It's All About Wisdeom.cpp │ ├── I. Salem.cpp │ ├── J. Game.cpp │ ├── K. PhD math.cpp │ ├── L - Candy Jars.cpp │ ├── M. Building Force Fields.cpp │ └── statements.pdf ├── 2015 SCPC │ ├── B.cpp │ ├── C.cpp │ ├── F.cpp │ ├── G.cpp │ ├── K.cpp │ └── L.cpp ├── 2016 ACPC │ ├── Andorra.cpp │ └── Gotta Catch 'Em All_WA.cpp ├── 2016 ECPC │ ├── A.cpp │ ├── G.cpp │ └── Problems - Codeforces.pdf ├── 2016 MCPC │ ├── A.cpp │ ├── B.cpp │ ├── D.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ ├── L.cpp │ ├── Problems - Codeforces.pdf │ ├── h.java │ ├── h_soln1.cpp │ ├── h_soln2.cpp │ ├── k (1).java │ ├── k.cpp │ ├── k.java │ ├── k_soln1.cpp │ └── k_soln2.cpp ├── 2017 ECPC │ ├── A.cpp │ ├── E.cpp │ ├── H.cpp │ └── Problems - Codeforces.pdf └── 2017 ICPC │ ├── Desktop │ ├── Balloon Colors.pdf │ └── kattislogin │ ├── samps │ ├── airport │ │ ├── airport-sample1.ans │ │ ├── airport-sample1.in │ │ ├── airport-sample2.ans │ │ └── airport-sample2.in │ ├── clue │ │ ├── clue-sample1.ans │ │ ├── clue-sample1.in │ │ ├── clue-sample2.ans │ │ ├── clue-sample2.in │ │ ├── clue-sample3.ans │ │ └── clue-sample3.in │ ├── improbable │ │ ├── improbable-sample1.ans │ │ ├── improbable-sample1.in │ │ ├── improbable-sample2.ans │ │ └── improbable-sample2.in │ ├── money │ │ ├── money-sample1.ans │ │ ├── money-sample1.in │ │ ├── money-sample2.ans │ │ └── money-sample2.in │ ├── posterize │ │ ├── posterize-sample1.ans │ │ ├── posterize-sample1.in │ │ ├── posterize-sample2.ans │ │ ├── posterize-sample2.in │ │ ├── posterize-sample3.ans │ │ └── posterize-sample3.in │ ├── replicate │ │ ├── replicate-sample1.ans │ │ ├── replicate-sample1.in │ │ ├── replicate-sample2.ans │ │ ├── replicate-sample2.in │ │ ├── replicate-sample3.ans │ │ └── replicate-sample3.in │ ├── scenery │ │ ├── scenery-sample1.ans │ │ ├── scenery-sample1.in │ │ ├── scenery-sample2.ans │ │ ├── scenery-sample2.in │ │ ├── scenery-sample3.ans │ │ └── scenery-sample3.in │ ├── secretchamber │ │ ├── secretchamber-sample1.ans │ │ ├── secretchamber-sample1.in │ │ ├── secretchamber-sample2.ans │ │ └── secretchamber-sample2.in │ ├── sonofpipestream │ │ ├── sonofpipestream-sample1.ans │ │ ├── sonofpipestream-sample1.in │ │ ├── sonofpipestream-sample2.ans │ │ └── sonofpipestream-sample2.in │ ├── speed │ │ ├── speed-sample1.ans │ │ ├── speed-sample1.in │ │ ├── speed-sample2.ans │ │ └── speed-sample2.in │ ├── tarotshamboast │ │ ├── tarotshamboast-sample1.ans │ │ ├── tarotshamboast-sample1.in │ │ ├── tarotshamboast-sample2.ans │ │ └── tarotshamboast-sample2.in │ └── visual │ │ ├── visual-sample1.ans │ │ ├── visual-sample1.in │ │ ├── visual-sample2.ans │ │ ├── visual-sample2.in │ │ ├── visual-sample3.ans │ │ ├── visual-sample3.in │ │ ├── visual-sample4.ans │ │ └── visual-sample4.in │ └── workspace │ └── ACM │ ├── .cproject │ ├── .project │ ├── .settings │ └── language.settings.xml │ ├── A.cpp │ ├── C.cpp │ ├── C2.cpp │ ├── Debug │ ├── ACM │ ├── C2.d │ ├── C2.o │ ├── makefile │ ├── objects.mk │ ├── sources.mk │ └── subdir.mk │ ├── F.cpp │ └── I.cpp ├── Live Archive ├── 2238 - Fixed Partition Memory Management.cpp ├── 2474 Balloons in a Box.java ├── 2478 - Island Hopping.cpp ├── 2605 WhereNow Vacations.cpp ├── 3435 Define Moment.cpp ├── 3915 Monkey Vines.cpp ├── 3916 Model Rocket Height.cpp ├── 3917 Tiling a Grid With Dominoes.cpp ├── 4220 Shrinking Polygons.cpp ├── 4764 Bing it.cpp ├── 490C Hacking Cypher.cpp ├── 5135 Mining your own business.cpp ├── 5789 Army Buddies.cpp ├── 5790 Ball Stacking.cpp ├── 5798 Jupiter Atacks!.cpp ├── 6776 - Metal Processing Plant.cpp ├── 7150 Amalgamated Artichokes.cpp ├── 7152 Catering.cpp ├── 7556 Jump on Buildings.cpp ├── 7558 Cat and Mouse.cpp ├── 7560 Chota Mouse and his Birthday Cake.cpp ├── 7561 Longest Palindrome.cpp ├── 7578 - Ceiling Function.java ├── 7711 - Confusing Date Format.cpp ├── 7712 - Confusing Manuscript.cpp ├── 7722 - Tale of a Happy Man.cpp └── 7722 - Tale of a Happy Man.cpp~ ├── SPOJ ├── 2 MULTQ3.cpp ├── 2 POSTERS.cpp ├── 3 AGGRCOW.cpp ├── 3 PIE.cpp ├── 3 TOPCODE (hard DP).cpp ├── 3 TOPCODE TLE.cpp ├── 3 TRT.cpp ├── 4 FASTFLOW.cpp ├── 5 AE2A - Dice.cpp ├── 5 DCEPC14G.cpp ├── 5 PRIMES1 (using Miller-Rabin).cpp ├── 5 PRIMES1 (using Segment Sieve).cpp ├── 6 NHAY.cpp ├── 6 PHONELST.cpp ├── 6 PLD.cpp ├── AIBOHP.cpp ├── ANARC05H.cpp ├── ASSIGN.cpp ├── AcronymMaker.cpp ├── BDOI16E - Village Fair.cpp ├── BENEFACT.cpp ├── BRCKTS2.cpp ├── BUGLIFE.cpp ├── CCHESS.cpp ├── CERC07K.cpp ├── CLEANRBT.cpp ├── DCEPC701.cpp ├── DISQUERY.cpp ├── DQUERY.cpp ├── EDIST.cpp ├── EQDIV.cpp ├── ESJAIL.cpp ├── FISHER.cpp ├── GOT - heave-light decomposition.cpp ├── GSS1.cpp ├── GSS3.cpp ├── HIGHWAYS.cpp ├── HISTOGRA.cpp ├── HORRIBLE.cpp ├── INVCNT.cpp ├── JEDNAKOST.cpp ├── LABYR1.cpp ├── LITE.cpp ├── Lucius Dungeon.cpp ├── MELE3.cpp ├── MICEMAZE.cpp ├── MIXTURES.cpp ├── MKTHNUM - merge segment tree.cpp ├── MKTHNUM - persistent segment tree.cpp ├── MLASERP.cpp ├── MSTRING.cpp ├── NOCHANGE.cpp ├── NOVICE43.cpp ├── PYRA.cpp ├── RDNWK.cpp ├── ROADS.cpp ├── SHOP.cpp ├── SSHUFFLE.cpp ├── STSTRING.cpp ├── SUBMERGE.cpp ├── TRAFFICN.cpp ├── TRSTAGE.cpp └── YODANESS.cpp ├── TJU ├── 2862. CityStar.cpp ├── 2863 Escape.cpp ├── 2864 Holiday Gifts.cpp ├── 2865. Soccer.cpp ├── 2867. Picking Problem.cpp ├── 2868 Fire Ballon.cpp ├── 2869 Nuanran's Game.cpp └── 2870 The K-th City.cpp └── UVA ├── 00108 Maximum Sum.cpp ├── 00142 Mouse Clicks.cpp ├── 00147.cpp ├── 00193.cpp ├── 00200.cpp ├── 00247.cpp ├── 00259.cpp ├── 00264 Count on Cantor.cpp ├── 00273 Jack Straws.cpp ├── 00273 Jack Straws_2.cpp ├── 00294 Divisors.cpp ├── 00295 Fatman_2.cpp ├── 00315.cpp ├── 00324 Factorial Frequencies.java ├── 00325 Identifying Legal Pascal Real Constants.java ├── 00343 What Base Is This.cpp ├── 00348 Optimal Array Multiplication Sequence.cpp ├── 00357 Let Me Count The Ways.cpp ├── 00374 Big Mod.cpp ├── 00377 Cowculations.cpp ├── 00382.cpp ├── 00384 Slurpys.java ├── 00389 Basically Speaking.cpp ├── 00394.cpp ├── 00406 Prime Cuts.cpp ├── 00414.cpp ├── 00416.cpp ├── 00417 Word Index.cpp ├── 00437 The Tower of Babylon.cpp ├── 00439 Knight Moves.cpp ├── 00441.cpp ├── 00443 humble numbers.cpp ├── 00458 The Decoder.cpp ├── 00460 Overlapping Rectangles.cpp ├── 00464 Sentence Phrase Generator.java ├── 00473 Raucous Rockers.cpp ├── 00488 Triangle Wave.cpp ├── 00494 Kindergarten Counting Game.cpp ├── 00498 Polly the Polynomial.cpp ├── 00507.cpp ├── 00526 String Distance and Transform Process.cpp ├── 00542 France ’98.cpp ├── 00544.cpp ├── 00558.cpp ├── 00563 Crimewave.cpp ├── 00583 Prime Factors.cpp ├── 00588 Video Surveillance.cpp ├── 00594 One Little, Three Endians.cpp ├── 00607 scheduling lectures.cpp ├── 00612 DNA sorting.cpp ├── 00616.cpp ├── 00620 Cellular Structure.java ├── 00622 Grammar Evaluation (there are comments).java ├── 00623 500!.java ├── 00644 Immediate Decodability.cpp ├── 00674.cpp ├── 00681 Convex Hull Finding.cpp ├── 00686 Goldbach’s Conjecture (II).cpp ├── 00696 How Many Knights.cpp ├── 00713 Adding Reversed Numbers.java ├── 00714 Copying Books.cpp ├── 00721.cpp ├── 00725 Division.cpp ├── 00743 The MTM Machine.cpp ├── 00760 DNA Sequencing.cpp ├── 00763 Fibinary Numbers.java ├── 00793.cpp ├── 00796.cpp ├── 00811 - The Fortified Forest.cc ├── 00812 Trade on Verweggistan.cpp ├── 00820.cpp ├── 00821.cpp ├── 00847 A Multiplication Game.cpp ├── 00872.cpp ├── 00882 The Mailbox Manufacturers Problem.cpp ├── 00884 Factorial Factors.cpp ├── 00897 Anagrammatic Primes.cpp ├── 00902 Password Search.cpp ├── 00907.cpp ├── 00914 Jumping Champion.cpp ├── 00920.cpp ├── 00925.cpp ├── 00927.cpp ├── 00941 Permutation.cpp ├── 00967 Circular.cpp ├── 00967 Circular_2.cpp ├── 00976 Bridge Building.cpp ├── 00985 Round and Round Maze.cpp ├── 00988.cpp ├── 00989 Su Doku.cpp ├── 00991 Sate Salutations.cpp ├── 01040 The Traveling Judges Problem.cpp ├── 01056.cpp ├── 01079 - A Careful Approach.cc ├── 01093 - Castles.cc ├── 01111 Trash Removal.cpp ├── 01112 Mice and Maze.cpp ├── 01121 Subsequence.cpp ├── 01133.cpp ├── 01172 The Bridges of Kolsberg.cpp ├── 01184 Air Raid.cpp ├── 01195 Calling Extraterrestrial Intelligence Again.cpp ├── 01201 Taxi Cab Scheme.cpp ├── 01203 Argus.cpp ├── 01210 Sum of Consecutive Prime Numbers.cpp ├── 01211 Atomic Car Race.cpp ├── 01220 Party at Hali-Bula.cpp ├── 01224 Tile Code.cpp ├── 01225.cpp ├── 01230 MODEX.java ├── 01231 ACORN.cpp ├── 01242 Necklace.cpp ├── 01244 Palindromic paths.cpp ├── 01251 - Repeated Substitution with Sed.cpp ├── 01263 Mines.cpp ├── 01280 - Curvy Little Bottles.cpp ├── 01347 Tour.cpp ├── 04146 ICPC Team Strategy.cpp ├── 100 The 3n + 1 problem.cpp ├── 10003 Cutting Sticks.cpp ├── 10004.cpp ├── 10005 Packing Polygons.cpp ├── 10007 Count the Trees.java ├── 10015 Joseph's Cousin.cpp ├── 10017 The Never Ending Towers of Hanoi.cpp ├── 10029 Edit Step Ladders.cpp ├── 10032 Tug of War.cpp ├── 10042 Smith Numbers.cpp ├── 10047 The Monocycle.java ├── 10048.cpp ├── 10054.cpp ├── 10056 What is the Probability.cpp ├── 10058 Jimmi's Riddles.java ├── 10069 Distinct Subsequences_building_table.java ├── 10069 Distinct Subsequences_dynamic_programming.java ├── 10074 Take the Land.java ├── 10075 Airlines.cpp ├── 10078 The Art Gallery.cpp ├── 10079 Pizza Cutting.cpp ├── 10080 Gopher II.cpp ├── 10081 Tight Words.cpp ├── 10090 Marbles.cpp ├── 10092.cpp ├── 10104 Euclid Problem.cpp ├── 1011 Is Bigger Smarter (Abouelkheir code).cpp ├── 10110 Light, more light.cpp ├── 10111 Find the Winning Move.cpp ├── 10129.cpp ├── 10130.cpp ├── 10131.cpp ├── 10139 FActovisors.cpp ├── 10140 Prime Distance.cpp ├── 10149 Yahtzee.cpp ├── 10154 Weights and Measures.cpp ├── 10161 Ant on a Chessboard.cpp ├── 10164 Number Game.cpp ├── 10165 Stone Game.cpp ├── 10168 Summation of Four Primes.cpp ├── 10171.cpp ├── 10176 Ocean Deep! - Make it shallow!!.cpp ├── 10179 Irreducible Basic Functions.cpp ├── 10192 Vacation.cpp ├── 10199.cpp ├── 10200 Prime Time.cpp ├── 10212 The Last Non-zero Digit.cpp ├── 10218 Let’s Dance!!.cpp ├── 10219 Find the Ways!.cpp ├── 10235 Simply Emirp.cpp ├── 10238 Throw the Dice.java ├── 10243 Fire! Fire!! Fire!!!.cpp ├── 10245 The Closest Pair Problem.cpp ├── 10252 Common Permutation.cpp ├── 10261 Ferry Loading.cpp ├── 10263.cpp ├── 10268 498-bis.cpp ├── 10271 Chopsticks.cpp ├── 10285 Longest Run on a Snowboard.cpp ├── 10298 Power Strings.cpp ├── 10299 - Relatives.cc ├── 10304 Optimal Binary Search Tree.cpp ├── 10304 Optimal Binary Search Tree_2.cpp ├── 10305 Ordering Tasks - 1.cpp ├── 10305 Ordering Tasks - 2.cpp ├── 10309 - Turn the Lights Off.cpp ├── 10311 - Goldbach and Euler.cc ├── 10312 Expression Bracketing.cpp ├── 10313 Pay the Price (1).cpp ├── 10313 Pay the Price.cpp ├── 10313 Pay the Price_2(bottom-up) (1).cpp ├── 10313 Pay the Price_2(bottom-up).cpp ├── 10319 Manhattan.cpp ├── 10327 Flip Sort.cpp ├── 10328 Coin Toss.java ├── 10330.cpp ├── 10334 Ray Through Glasses.java ├── 10338 Mischievous Children.cpp ├── 10341.cpp ├── 10346.cpp ├── 10359 Tiling.java ├── 10360.cpp ├── 10364 Square.cpp ├── 10368 Euclid's Game.cpp ├── 10369.cpp ├── 10382.cpp ├── 1039 Simplified GSM Network.cpp ├── 10393 The One-Handed Typist.cpp ├── 10394 Twin Primes.cpp ├── 10394.cpp ├── 10401.cpp ├── 10404 Bachet's Game.cpp ├── 10407 Simple Division.cpp ├── 10408 Farey sequences.cpp ├── 10419 Sum-up the Primes.cpp ├── 10427 Naughty Sleepy Boys.cpp ├── 10459 The Tree Root.cpp ├── 10482 The Candyman Can.cpp ├── 10484 Divisibility of Factors.cpp ├── 10489 - Boxes of Chocolates.cc ├── 10490 - Mr. Azad and his Son!!!!!.cc ├── 10491 Cows and Cars.cpp ├── 10507 Waking up brain.cpp ├── 10518 How Many Calls.cpp ├── 10526 Very Easy !!!.java ├── 10533 Digit Primes.cpp ├── 10536 Game of Euler.cpp ├── 10539 Almost Prime Numbers.cpp ├── 10541 Stripe.java ├── 10551 Basic Remains.java ├── 10557 XYZZY.cpp ├── 10576 Y2K Accounting Bug.cpp ├── 10578 The Game of 31.cpp ├── 10581.cpp ├── 10586 Polynomial Remains.cpp ├── 10589.cpp ├── 10600 ACM Contest and Blackout.cpp ├── 10608.cpp ├── 10616 Divisible Group Sums.cpp ├── 10633 - Rare Easy Problem.cc ├── 10635 Prince and Princess.cpp ├── 10637 Coprimes.cpp ├── 10645 Menu.cpp ├── 10650 Determinate Prime.cpp ├── 10651 Pebble Solitaire.cpp ├── 10652 Board Wrapping.cpp ├── 10655 Contemplation! Algebra.cpp ├── 10673 Play with Floor and Ceil.cpp ├── 10678 Grazing Cow.cpp ├── 10680 LCM.cpp ├── 10684 The jackpot.cpp ├── 10689 Yet another Number System.cpp ├── 10690 Expression Again_1_2.4s.cpp ├── 10690 Expression Again_2_0.5s(reduced space complexity).cpp ├── 10690 Expression Again_3_0.15s (pruning).cpp ├── 10699 Count the Factors.cpp ├── 10717 Mint.cpp ├── 10720 Graph Construction.cpp ├── 10721 Bar Codes.cpp ├── 10722 Super Lucky Numbers.cpp ├── 10731.cpp ├── 10734 Triangle Partitioning.cpp ├── 10738 Riemann vs Mertens.cpp ├── 10746 Crime Wave - The Sequel_DP.cpp ├── 10746 Crime Wave - The Sequel_MAX_FLOW.cpp ├── 10759 Dice Throwing.cpp ├── 10765.cpp ├── 10773.cpp ├── 10774 Repeated Josephus.cpp ├── 10777 God! Save me.cpp ├── 10779.cpp ├── 10780 - Again Prime No Time.cpp ├── 10784 Diagonal.cpp ├── 10790 How Many Points of Intersection.cpp ├── 10800 - Not That Kind of Graph.cpp ├── 10805 Cockroach Escape Networks.cpp ├── 10814 Simplifying Fractions.java ├── 10817 Headmaster's Headache.cpp ├── 10819 Trouble of 13-Dots.cpp ├── 10820 Send a Table.cpp ├── 10852 - Less Prime.cc ├── 10854 Number of Paths.cpp ├── 10856 Recover Factorial.cpp ├── 10871 Primed Subsequence.cpp ├── 10876 - Factory Robot.cc ├── 10878.cpp ├── 10880 Colin and Ryan.cpp ├── 10891 Game of Sum.cpp ├── 10892 LCM Cardinality.cpp ├── 10898 Combo Deal.cpp ├── 10911 Forming Quiz Teams.cpp ├── 10913 - Walking on a Grid.txt ├── 10917 A Walk Through the Forest.cpp ├── 10918 Tri Tiling.cpp ├── 10922 2 the 9s.cpp ├── 10924 Prime Words.cpp ├── 10927.cpp ├── 10929 You can say 11.cpp ├── 10929 You can say 11.java ├── 10931 Parity.cpp ├── 10937 Blackheart the Pirate.cpp ├── 10938 Flea circus.cpp ├── 10940 Throwing cards away II.cpp ├── 10944 Nuts for nuts...cpp ├── 10948 - The primary problem.cc ├── 10957 So Doku Checker.cpp ├── 1096 The Islands.cpp ├── 10983 - Buy one, get the rest free.cpp ├── 10986.cpp ├── 10990 Another New Function.cpp ├── 11002 Towards Zero.cpp ├── 11005 Cheapest Base.cpp ├── 11021 Tribbles.cpp ├── 11042 Complex, difficult and complicated.cpp ├── 11045.cpp ├── 11048 Automatic correction of misspellings.cpp ├── 11060.cpp ├── 11064 Number Theory.cpp ├── 11065 A Gentlemen’s Agreement.cpp ├── 11069 A Graph Problem.cpp ├── 11070 The good old times.cpp ├── 11080 Place the Guards.cpp ├── 11084 Anagram Division.cpp ├── 11085 Back to the 8-Queens.cpp ├── 11086 Composite Prime.cpp ├── 11088 End up with More Teams.cpp ├── 11105 Semi-prime H-numbers.cpp ├── 11111 Generalized Matrioshkas.cpp ├── 11125 Arrange Some Marbles.cpp ├── 11127 Triple-Free Binary Strings.cpp ├── 11133 Eigensequence.cpp ├── 11151.cpp ├── 11155 Be Efficient.cpp ├── 11159.cpp ├── 11172 Relational Operator.cpp ├── 11173 Grey Codes.cpp ├── 11176 Winning Streak.cpp ├── 11176 Winning Streak_2.cpp ├── 11181 Probability(or)Given.cpp ├── 11195 Another n-Queen Problem.cpp ├── 11198 Dancing Digits.cpp ├── 11203 Can you decide it for ME.cpp ├── 11204 Musical instruments.cpp ├── 11205 The broken pedometer.cpp ├── 11218 KTV.cpp ├── 11226 - Reaching the fix-point.cc ├── 11227 The silver bullet.cpp ├── 11228.cpp ├── 11231 Black and white painting.cpp ├── 11235 Frequent Values.cpp ├── 11258 String Partition.cpp ├── 11262 Weird Fence.cpp ├── 11265 The Sultan's Problem.cpp ├── 11267 The Hire-a-Coder Business Model.cpp ├── 11267 The Hire-a-Coder Business Model.java ├── 11270 Tiling Dominoes.cpp ├── 11282 Mixing Invitations.cpp ├── 11287 Pseudoprime Numbers.cpp ├── 11291 Smeech.cpp ├── 11292.cpp ├── 11310 Delivery Debacle.java ├── 11311 Exclusively Edible.cpp ├── 11311 Exclusively Edible_2.cpp ├── 11324 The Largest Clique.cpp ├── 11327 - Enumerating Rational Numbers.cc ├── 11344 The Huge One.cpp ├── 11346 Probability.cpp ├── 11351 Last Man Standing.cpp ├── 11353 - A Different Kind of Sorting.cc ├── 11357 Ensuring Truth.java ├── 11371 Number Theory for Newbies.cpp ├── 11374.cpp ├── 11378 Bey Battle.cpp ├── 11381 Elegant Strings.cpp ├── 11385 Da Vinci Code.java ├── 11391 Blobs in the Board.cpp ├── 11401 Triangle Counting.cpp ├── 11402 Ahoy, Pirates.cpp ├── 11405 Can U Win.cpp ├── 11408 Count DePrimes.cpp ├── 11413 Fill the Containers.cpp ├── 11415 Count the Factorials.cpp ├── 11428 Cubes.cpp ├── 11432 Busy Programmer.cpp ├── 11450_1.cpp ├── 11450_2.cpp ├── 11456 Trainsorting.cpp ├── 11462 Age Sort.cpp ├── 11463.cpp ├── 11464 Even Parity.cpp ├── 11466 Largest Prime Divisor.cpp ├── 11472 Beautiful Numbers.cpp ├── 11474 Dying Tree.cpp ├── 11475 Extend to Palindrome.cpp ├── 11489 Integer Game.cpp ├── 11500 Vampires.cpp ├── 11503.cpp ├── 11504 Dominos.cpp ├── 11506.cpp ├── 11512 GATTACA.cpp ├── 11515 Cranes.cpp ├── 11516 WiFi.cpp ├── 11517 Exact Change.cpp ├── 11525 Permutation.cpp ├── 11536 Smallest Sub-Array.cpp ├── 11538 Chess Queen.cpp ├── 11554 Hapless Hedonism.cpp ├── 11576 Scrolling Sign.cpp ├── 11588 Image Coding.cpp ├── 11597 Spanning Subtree.cpp ├── 11610 Reverse Prime.cpp ├── 11628 Another lottery.cpp ├── 11631.cpp ├── 11635 Hotel booking.cpp ├── 11646 Athletics Track.cpp ├── 11686.cpp ├── 11695 Flight Planning.cpp ├── 11709.cpp ├── 11718 Fantasy of a Summation.cpp ├── 11721.cpp ├── 11723.cpp ├── 11728 Alternate Task.cpp ├── 11730 Number Transformation.cpp ├── 11747.cpp ├── 11770.cpp ├── 11790 Murcia's Skyline.cpp ├── 11813 Shopping.cpp ├── 11821 High-Precision Number.java ├── 11827 Maximum GCD.cpp ├── 11831.cpp ├── 11834 Elevator.cpp ├── 11838.cpp ├── 11858 Frosh Week.java ├── 11875.cpp ├── 11876 N + NOD(N).cpp ├── 11878 Homework Checker.cpp ├── 11879 Multiple of 17.java ├── 11889 Benefit.cpp ├── 11951.cpp ├── 11955 Binomial Theorem.cpp ├── 11957 Checkers.cpp ├── 11959 Dice.cpp ├── 11960 Divisor Game.cpp ├── 11966 Galactic Bonding.cpp ├── 11967 Hic-Hac-Hoe.cpp ├── 11974 Switch The Lights.java ├── 12004 Bubble Sort.cpp ├── 12024 Hats.cpp ├── 12028 A Gift from the Setter.cpp ├── 12030 Help the Winners.cpp ├── 12043 - Divisors.cc ├── 12083 Guardian of Decency.java ├── 12101 Prime Path.cpp ├── 12114 Bachelor Arithmetic.cpp ├── 12135 Switch Bulbs.java ├── 12155 ASCII Diamond.cpp ├── 12159 Gun Fight.cpp ├── 12168 Cat vs. Dog.cpp ├── 12210 A Match Making Problem.cpp ├── 12238 Ants Colony.cpp ├── 12293 Box Game.cpp ├── 12318 Digital Roulette.cpp ├── 1232 SKYLINE.cpp ├── 12324 Philip J. Fry Problem .cpp ├── 12405 Scarecow.cpp ├── 12428 Enemy At The Gates.cpp ├── 1243 Polynomial-time Reductions.cpp ├── 12444 Bits and Pieces.cpp ├── 12455.cpp ├── 12457 Tennis contest.cpp ├── 1246 - Find Terrorists.cc ├── 12460 Careful Teacher.cpp ├── 12461 Airplane(DP).cpp ├── 12461 Airplane_2 (figure out the formula).cpp ├── 12469 Stones.cpp ├── 12482 Short Story Competition.cpp ├── 1252 Twenty Questions.cpp ├── 12532 Interval Product.cpp ├── 12543 Longest Word.java ├── 1390 Interconnect.cpp ├── 295 Fatman.cpp ├── 4607 Robot Challenge.cpp ├── 515 King.cpp ├── 516 Prime Land.cpp ├── 536 Tree Recovery.java ├── 543 Goldbach's Conjecture.cpp ├── 547 - DDF.cc ├── 562 Dividing coins.cpp ├── 562 Dividing coins_2(bottom - up).cpp ├── 575 Skew Binary.cpp ├── 662 Fast Food.cpp ├── 673 Parentheses Balance.cpp ├── 702 The Vindictive Coach.cpp └── 756 - Biorhythms.cc /Competitve Programming 3, Steven Halim/BookChapterStevenFelixFinal25October2009.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/BookChapterStevenFelixFinal25October2009.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch1/ch1_01_factorial.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | 4 | class Main { // standard Java class name in UVa OJ 5 | public static void main(String[] args) { 6 | BigInteger fac = BigInteger.ONE; 7 | for (int i = 2; i <= 25; i++) 8 | fac = fac.multiply(BigInteger.valueOf(i)); // it is in the library! 9 | System.out.println(fac); 10 | } } 11 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch1/ch1_02_scanf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int N; // using global variables in contests can be a good strategy 5 | char x[110]; // make it a habit to set array size a bit larger than needed 6 | 7 | int main() { 8 | scanf("%d\n", &N); 9 | while (N--) { // we simply loop from N, N-1, N-2, ..., 0 10 | scanf("0.%[0-9]...\n", &x); // `&' is optional when x is a char array 11 | // note: if you are surprised with the trick above, 12 | // please check scanf details in www.cppreference.com 13 | printf("the digits are 0.%s\n", x); 14 | } } // return 0; 15 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch1/ch1_02_scanf_input.txt: -------------------------------------------------------------------------------- 1 | 3 2 | 0.1227... 3 | 0.517611738... 4 | 0.7341231223444344389923899277... 5 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch2/ch2_01_array_vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int arr[5] = {7,7,7}; // initial size (5) and initial value {7,7,7,0,0} 7 | vector v(5, 5); // initial size (5) and initial value {5,5,5,5,5} 8 | 9 | printf("arr[2] = %d and v[2] = %d\n", arr[2], v[2]); // 7 and 5 10 | 11 | for (int i = 0; i < 5; i++) { 12 | arr[i] = i; 13 | v[i] = i; 14 | } 15 | 16 | printf("arr[2] = %d and v[2] = %d\n", arr[2], v[2]); // 2 and 2 17 | 18 | // arr[5] = 5; // static array will generate index out of bound error 19 | // uncomment the line above to see the error 20 | 21 | v.push_back(5); // but vector will resize itself 22 | printf("v[5] = %d\n", v[5]); // 5 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch2/in_07.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 0 10 0 0 100 0 3 | 10 0 7 0 8 0 4 | 0 7 0 9 0 0 5 | 0 0 9 0 20 5 6 | 100 8 0 20 0 0 7 | 0 0 0 5 0 0 8 | 6 9 | 2 2 10 5 100 10 | 3 1 10 3 7 5 8 11 | 2 2 7 4 9 12 | 3 3 9 5 20 6 5 13 | 3 1 100 2 8 4 20 14 | 1 4 5 15 | 7 16 | 1 2 10 17 | 1 5 100 18 | 2 3 7 19 | 2 5 8 20 | 3 4 9 21 | 4 5 20 22 | 4 6 5 23 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch3/ch3_04_Max1DRangeSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n = 9, A[] = { 4, -5, 4, -3, 4, 4, -4, 4, -5 }; // a sample array A 7 | int running_sum = 0, ans = 0; 8 | for (int i = 0; i < n; i++) // O(n) 9 | if (running_sum + A[i] >= 0) { // the overall running sum is still +ve 10 | running_sum += A[i]; 11 | ans = max(ans, running_sum); // keep the largest RSQ overall 12 | } 13 | else // the overall running sum is -ve, we greedily restart here 14 | running_sum = 0; // because starting from 0 is better for future 15 | // iterations than starting from -ve running sum 16 | printf("Max 1D Range Sum = %d\n", ans); // should be 9 17 | } // return 0; 18 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch3/ch3_04_Max1DRangeSum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class ch3_04_Max1DRangeSum { 4 | public static void main(String[] args) { 5 | int n = 9, A[] = { 4, -5, 4, -3, 4, 4, -4, 4, -5 }; // a sample array A 6 | int running_sum = 0, ans = 0; 7 | for (int i = 0; i < n; i++) // O(n) 8 | if (running_sum + A[i] >= 0) { // the overall running sum is still +ve 9 | running_sum += A[i]; 10 | ans = Math.max(ans, running_sum); // keep the largest RSQ overall 11 | } 12 | else // the overall running sum is -ve, we greedily restart here 13 | running_sum = 0; // because starting from 0 is better for future 14 | // iterations than starting from -ve running sum 15 | System.out.printf("Max 1D Range Sum = %d\n", ans); // should be 9 16 | } } 17 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch3/ch3_08_UVa674.cpp: -------------------------------------------------------------------------------- 1 | /* Coin Change */ 2 | 3 | // O(NV) DP solution 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int N = 5, V, coinValue[5] = {1, 5, 10, 25, 50}, memo[6][7500]; 10 | // N and coinValue are fixed for this problem, max V is 7489 11 | 12 | int ways(int type, int value) { 13 | if (value == 0) return 1; 14 | if (value < 0 || type == N) return 0; 15 | if (memo[type][value] != -1) return memo[type][value]; 16 | return memo[type][value] = ways(type + 1, value) + ways(type, value - coinValue[type]); 17 | } 18 | 19 | int main() { 20 | memset(memo, -1, sizeof memo); // we only need to initialize this once 21 | while (scanf("%d", &V) != EOF) 22 | printf("%d\n", ways(0, V)); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/IntegerPair.java: -------------------------------------------------------------------------------- 1 | class IntegerPair implements Comparable { 2 | Integer _first, _second; 3 | 4 | public IntegerPair(Integer f, Integer s) { 5 | _first = f; 6 | _second = s; 7 | } 8 | 9 | public int compareTo(Object o) { 10 | if (!this.first().equals((IntegerPair)o).first()) 11 | return this.first() - ((IntegerPair)o).first(); 12 | else 13 | return this.second() - ((IntegerPair)o).second(); 14 | } 15 | 16 | Integer first() { return _first; } 17 | Integer second() { return _second; } 18 | } 19 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/IntegerTriple.java: -------------------------------------------------------------------------------- 1 | class IntegerTriple implements Comparable { 2 | Integer _first, _second, _third; 3 | 4 | public IntegerTriple(Integer f, Integer s, Integer t) { 5 | _first = f; 6 | _second = s; 7 | _third = t; 8 | } 9 | 10 | public int compareTo(Object o) { 11 | if (!this.first().equals(((IntegerTriple)o).first())) 12 | return this.first() - ((IntegerTriple)o).first(); 13 | else if (!this.second().equals(((IntegerTriple)o).second())) 14 | return this.second() - ((IntegerTriple)o).second(); 15 | else 16 | return this.third() - ((IntegerTriple)o).third(); 17 | } 18 | 19 | Integer first() { return _first; } 20 | Integer second() { return _second; } 21 | Integer third() { return _third; } 22 | 23 | public String toString() { return first() + " " + second() + " " + third(); } 24 | } 25 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/in_01.txt: -------------------------------------------------------------------------------- 1 | 9 2 | 1 1 0 3 | 3 0 0 2 0 3 0 4 | 2 1 0 3 0 5 | 3 1 0 2 0 4 0 6 | 1 3 0 7 | 0 8 | 2 7 0 8 0 9 | 1 6 0 10 | 1 6 0 11 | 12 | 8 13 | 2 1 0 2 0 14 | 2 2 0 3 0 15 | 2 3 0 5 0 16 | 1 4 0 17 | 0 18 | 0 19 | 0 20 | 1 6 0 21 | 22 | 3 23 | 1 1 0 24 | 1 2 0 25 | 1 0 0 26 | 27 | 6 28 | 1 1 0 29 | 3 0 0 2 0 4 0 30 | 1 1 0 31 | 1 4 0 32 | 3 1 0 3 0 5 0 33 | 1 4 0 34 | 35 | 6 36 | 1 1 0 37 | 5 0 0 2 0 3 0 4 0 5 0 38 | 1 1 0 39 | 1 1 0 40 | 2 1 0 5 0 41 | 2 1 0 4 0 42 | 43 | 8 44 | 1 1 0 45 | 1 3 0 46 | 1 1 0 47 | 2 2 0 4 0 48 | 1 5 0 49 | 1 7 0 50 | 1 4 0 51 | 1 6 0 52 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/in_03.txt: -------------------------------------------------------------------------------- 1 | 5 7 2 | 0 1 4 3 | 0 2 4 4 | 0 3 6 5 | 0 4 6 6 | 1 2 2 7 | 2 3 8 8 | 3 4 9 9 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/in_04.txt: -------------------------------------------------------------------------------- 1 | 13 16 2 | 0 1 1 2 2 3 0 4 1 5 2 6 3 7 5 6 3 | 4 8 8 9 5 10 6 11 7 12 9 10 10 11 11 12 4 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/in_05.txt: -------------------------------------------------------------------------------- 1 | 5 7 2 2 | 2 1 2 3 | 2 3 7 4 | 2 0 6 5 | 1 3 3 6 | 1 4 6 7 | 3 4 5 8 | 0 4 1 9 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/in_06.txt: -------------------------------------------------------------------------------- 1 | 5 5 0 2 | 0 1 1 3 | 0 2 10 4 | 1 3 2 5 | 2 3 -10 6 | 3 4 3 7 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/in_07.txt: -------------------------------------------------------------------------------- 1 | 5 9 2 | 0 1 2 3 | 0 2 1 4 | 0 4 3 5 | 1 3 4 6 | 2 1 1 7 | 2 4 1 8 | 3 0 1 9 | 3 2 3 10 | 3 4 5 11 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch4/in_08.txt: -------------------------------------------------------------------------------- 1 | 4 0 1 2 | 2 2 70 3 30 3 | 2 2 25 3 70 4 | 3 0 70 3 5 1 25 5 | 3 0 30 2 5 1 70 6 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch5/ch5_02_UVa10551.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | 4 | class Main { /* UVa 10551 - Basic Remains, 0.345s in Java */ 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | while (true) { 8 | int b = sc.nextInt(); 9 | if (b == 0) break; 10 | String p_str = sc.next(); 11 | BigInteger p = new BigInteger(p_str, b); // special constructor! 12 | String m_str = sc.next(); 13 | BigInteger m = new BigInteger(m_str, b); // 2nd parameter is the radix/base 14 | System.out.println((p.mod(m)).toString(b)); // can output in any radix/base 15 | } } } 16 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch5/ch5_04_UVa10814.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | 4 | class Main { /* UVa 10814 - Simplifying Fractions, 0.212s in Java */ 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | int N = sc.nextInt(); 8 | while (N-- > 0) { // unlike in C/C++, we have to supply > 0 in (N-- > 0) 9 | BigInteger p = sc.nextBigInteger(); 10 | String ch = sc.next(); // we ignore the division sign in input 11 | BigInteger q = sc.nextBigInteger(); 12 | BigInteger gcd_pq = p.gcd(q); // wow :) 13 | System.out.println(p.divide(gcd_pq) + " / " + q.divide(gcd_pq)); 14 | } } } 15 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch5/ch5_05_UVa1230.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | 4 | class Main { /* UVa 1230 - LA 4104 - MODEX, 0.189s in Java */ 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | int c = sc.nextInt(); 8 | while (c-- > 0) { 9 | BigInteger x = BigInteger.valueOf(sc.nextInt()); // valueOf converts 10 | BigInteger y = BigInteger.valueOf(sc.nextInt()); // simple integer 11 | BigInteger n = BigInteger.valueOf(sc.nextInt()); // into BigInteger 12 | System.out.println(x.modPow(y, n)); // look ma, it's in the library ;) 13 | } } } 14 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Code/ch6/ch6-out.txt: -------------------------------------------------------------------------------- 1 | i love cs3232 competitive programming and algorithm. of course i love my wife grace suryani very much too. test that there are thirty chars m12345678901234567890123456789 ...... hehehe the line arose only contains six . cannot stop... 2 | 33 58 96 3 | algorithm wife 4 | 0 5 | 569 3 79 6 | -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Competitive_Programming_3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Competitive_Programming_3.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week01_introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week01_introduction.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week02_ds_libraries.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week02_ds_libraries.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week03_search.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week03_search.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week04_dp_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week04_dp_1.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week05_dp_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week05_dp_2.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week05_graph_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week05_graph_1.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week07_graph_1_network_flow.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week07_graph_1_network_flow.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week08_graph_2_matching.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week08_graph_2_matching.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week09_maths.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week09_maths.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week10_string.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week10_string.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week11_geometry.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week11_geometry.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/Slides/week12_harder_stuffs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/Slides/week12_harder_stuffs.pdf -------------------------------------------------------------------------------- /Competitve Programming 3, Steven Halim/skillset.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Competitve Programming 3, Steven Halim/skillset.xls -------------------------------------------------------------------------------- /Library/Fegla/ACM training sessions.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Library/Fegla/ACM training sessions.odt -------------------------------------------------------------------------------- /Library/Fegla/Suffix arrays.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Library/Fegla/Suffix arrays.odt -------------------------------------------------------------------------------- /Library/Fegla/Useful Mathematical Relations.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Library/Fegla/Useful Mathematical Relations.pdf -------------------------------------------------------------------------------- /Library/Fegla/coachLibrary.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Library/Fegla/coachLibrary.docx -------------------------------------------------------------------------------- /Library/Library.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Library/Library.pdf -------------------------------------------------------------------------------- /Library/Library.synctex.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Library/Library.synctex.gz -------------------------------------------------------------------------------- /Solutions/-in-progress/00104 Arbitage .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/-in-progress/00104 Arbitage .pdf -------------------------------------------------------------------------------- /Solutions/-in-progress/1099.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/-in-progress/1099.pdf -------------------------------------------------------------------------------- /Solutions/-in-progress/12479 Interesting Route !!.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/-in-progress/12479 Interesting Route !!.cpp -------------------------------------------------------------------------------- /Solutions/-in-progress/2016, 05, 1, 01229.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/-in-progress/2016, 05, 1, 01229.pdf -------------------------------------------------------------------------------- /Solutions/-in-progress/2016, 05, 12, 11294 Wedding !!.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/-in-progress/2016, 05, 12, 11294 Wedding !!.pdf -------------------------------------------------------------------------------- /Solutions/-in-progress/234 C WA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAX 100005 6 | using namespace std; 7 | int n; 8 | int t[MAX]; 9 | int main(){ 10 | // freopen("Input.txt", "r", stdin); 11 | scanf("%d", &n); 12 | int posBefore[MAX]; 13 | int negAfter[MAX]; 14 | memset(negAfter, 0, sizeof negAfter); 15 | posBefore[0] = 0; 16 | for (int i = 1; i <= n; ++i) { 17 | scanf("%d", t + i); 18 | posBefore[i] = posBefore[i - 1]; 19 | if (t[i] >= 0) posBefore[i] += 1; 20 | if (t[i] <= 0) negAfter[i] = 1; 21 | } 22 | for (int i = n - 1; i >= 1; --i) 23 | negAfter[i] += negAfter[i + 1]; 24 | int ans = INT_MAX; 25 | for (int i = 1; i < n; ++i){ // k can range from 1 to n-1 26 | ans = min(ans, posBefore[i] + negAfter[i+1]); 27 | } 28 | printf("%d", ans); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/103 B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 105 4 | using namespace std; 5 | int n, m; 6 | vector adj[MAX]; 7 | bool vis[MAX]; 8 | void dfs(int s){ // source 9 | vis[s] = true; 10 | for (auto iter = adj[s].begin(); iter != adj[s].end(); ++iter) 11 | if (!vis[*iter]) dfs(*iter); 12 | } 13 | int main(){ 14 | freopen("Input.txt", "r", stdin); 15 | freopen("Output.txt", "w", stdout); 16 | scanf("%d%d", &n, &m); 17 | if (n != m) { 18 | printf("NO"); 19 | return 0; 20 | } 21 | int x, y; 22 | for (int i = 0; i < n; ++i){ 23 | scanf("%d%d", &x, &y); 24 | adj[x].push_back(y); 25 | adj[y].push_back(x); 26 | } 27 | dfs(1); 28 | for (int i = 1; i <= n; ++i) 29 | if (!vis[i]) 30 | { 31 | printf("NO"); 32 | return 0; 33 | } 34 | printf("FHTAGN!"); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/104 A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | scanf("%d", &n); 8 | if (n <= 10 || n > 21) { printf("0"); return 0; } 9 | if (n == 20) { printf("15"); return 0; } 10 | printf("4"); return 0; 11 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/115 A.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #define Num 2005 5 | using namespace std; 6 | 7 | int main(){ 8 | int n, x; 9 | int p[Num]; 10 | bool processed[Num]; 11 | scanf("%d", &n); 12 | for (int i = 0; i < n; i++){ 13 | processed[i] = false; 14 | p[i] = i; 15 | scanf("%d", &x); 16 | if (x != -1) p[i] = --x; 17 | } 18 | // 19 | int cur, Max = 1; 20 | for (int i = 0; i < n; ++i){ 21 | if (processed[i]) continue; 22 | processed[i] = true; 23 | x = i; 24 | cur = 1; 25 | while (p[x] != x) processed[x = p[x]] = true, cur++; 26 | Max = max(Max, cur); 27 | } 28 | printf("%d\n", Max); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/125C Hobbits' Party.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vectoradj[150]; 4 | inline void construct_graph(const int size){ 5 | int counter = 1; 6 | for (int i = 1; i <= size; ++i) 7 | for (int j = i + 1; j <= size; ++j) 8 | adj[i].push_back(counter), adj[j].push_back(counter), ++counter; 9 | } 10 | int main(void){ 11 | vectorv; 12 | v.push_back(3); 13 | for (int i = 4; v.back() < 10000; ++i) 14 | v.push_back(i*(i-1)/2); 15 | int n; 16 | scanf("%d", &n); 17 | int ind = upper_bound(v.begin(), v.end(), n) - v.begin(); 18 | ind += 2; 19 | printf("%d\n", ind); 20 | construct_graph(ind); 21 | for (int i = 1; i <= ind; ++i) 22 | { 23 | vector&v = adj[i]; 24 | for (int j = 0; j < v.size(); ++j) 25 | printf("%d%c", v[j], j == v.size() - 1 ? '\n' : ' '); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/CodeForces/166 E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 10000001 4 | #define MOD 1000000007 5 | using namespace std; 6 | int Dp[4][MAX]; 7 | int main(){ 8 | for (int i = 0; i < 4; ++i) 9 | Dp[i][0] = !i; 10 | int ans; 11 | for (int j = 1; j < MAX; ++j) 12 | for (int i = 0; i < 4; ++i) 13 | { 14 | ans = 0; 15 | for (int k = 1; k <= 3; ++k) 16 | ans = (ans + Dp[(i + k) % 4][j - 1]) % MOD; 17 | Dp[i][j] = ans; 18 | } 19 | 20 | int n; 21 | cin >> n; 22 | cout << Dp[0][n]; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/189A Cut Ribbon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 4040 3 | #define MOD 1000000007 4 | using namespace std; 5 | int memo[MAX]; 6 | int a, b, c; 7 | inline int dp(const int i){ 8 | if (i < 0) return -1000000000; 9 | int & ret = memo[i]; 10 | if (ret != -1) return ret; 11 | return ret = 1 + max(max(dp(i - a), dp(i - b)), dp(i - c)); 12 | } 13 | int main(void){ 14 | memset(memo, 0xFF, sizeof memo); 15 | memo[0] = 0; 16 | int n; 17 | scanf("%d%d%d%d", &n, &a, &b, &c); 18 | printf("%d\n", dp(n)); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/CodeForces/202A - LLPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 15 4 | using namespace std; 5 | char s[MAX]; 6 | char sub[MAX]; 7 | char ans[MAX]; 8 | int n; 9 | void generate_sub(int x) { 10 | int counter = 0; 11 | for (int i = 0; i < n; ++i) 12 | // take character i 13 | if (x & (1 << i)) 14 | sub[counter++] = s[i]; 15 | sub[counter] = 0; 16 | } 17 | bool pal() { 18 | int i = 0, j = strlen(sub) - 1; 19 | while (i <= j) { 20 | if (sub[i] != sub[j]) 21 | return false; 22 | ++i; 23 | --j; 24 | } 25 | return true; 26 | } 27 | int main(void) { 28 | // freopen("Input.txt", "r", stdin); 29 | scanf("%s", s); 30 | n = strlen(s); 31 | ans[0] = 0; 32 | for (int i = 0; i < (1 << n); ++i) { 33 | generate_sub(i); 34 | if (!pal()) 35 | continue; 36 | if (strcmp(ans, sub) < 0) 37 | strcpy(ans, sub); 38 | } 39 | printf("%s", ans); 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Solutions/CodeForces/208251 D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef unsigned long long int ll; 4 | 5 | ll n, k; 6 | int main(void) 7 | { 8 | ll TC; cin >> TC; 9 | while(TC--) 10 | { 11 | cin >> n >> k; 12 | ll counter = 0LLU; 13 | for (ll t = k; t <= n && t > 0LLU; t *= 10LLU) 14 | counter += min(n - t + 1LLU, t / k); 15 | printf("%.7lf\n", (double) double(counter) / double(n)); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/CodeForces/248 B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 100005 4 | using namespace std; 5 | int main(){ 6 | int n; 7 | scanf("%d", &n); 8 | if (n < 3){ 9 | printf("-1"); 10 | return 0; 11 | } 12 | if (n == 3) { 13 | printf("210"); 14 | return 0; 15 | } 16 | char num[MAX]; 17 | memset(num, '0', n*sizeof(char)); 18 | num[0] = '1'; 19 | num[n] = 0; 20 | int toAdd = 50; 21 | for (int i = 0; i < n - 4; ++i){ 22 | toAdd *= 10; 23 | toAdd %= 210; 24 | } 25 | int counter = 1; 26 | while (toAdd){ 27 | num[n-(counter++)] += (toAdd%10); 28 | toAdd /= 10; 29 | } 30 | printf("%s", num); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/291C Network Mask.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100005 3 | using namespace std; 4 | int n, k; 5 | int arr[MAX]; 6 | int tmp[MAX]; 7 | int main(void){ 8 | // freopen("Input.txt", "r", stdin); 9 | scanf("%d%d", &n, &k); 10 | for (int i = 0, a, b, c, d; i < n; ++i){ 11 | scanf("%d.%d.%d.%d", &a, &b, &c, &d); 12 | arr[i] = (a << 24) | (b << 16) | (c << 8) | d; 13 | } 14 | for (int i = 31; i > 0; --i){ 15 | int mask = ~((1 << i) - 1); 16 | for (int j = 0; j < n; ++j) 17 | tmp[j] = arr[j] & mask; 18 | sort(tmp, tmp + n); 19 | int count = 0; 20 | for (int j = 0; j < n;){ 21 | int network = tmp[j]; 22 | ++count; 23 | while(j < n && tmp[j] == network) ++j; 24 | } 25 | if (count == k){ 26 | printf("%d.%d.%d.%d",(mask & 0xFF000000) >> 24, (mask & 0xFF0000) >> 16, (mask&0xFF00) >> 8, (mask & 0xFF)); 27 | return 0; 28 | } 29 | } 30 | printf("-1"); 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Solutions/CodeForces/313 B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | freopen("Input.txt", "r", stdin); 7 | freopen("Output.txt", "w", stdout); 8 | 9 | char s[100005]; 10 | scanf("%s", s); 11 | 12 | int num[100005]; 13 | num[0] = 0; 14 | int i; 15 | for (i = 1; s[i]; i++){ 16 | num[i] = num[i - 1] + (s[i] == s[i-1]); 17 | } 18 | int m, r, l; 19 | scanf("%d", &m); 20 | while (m--){ 21 | scanf("%d%d", &l, &r); 22 | printf("%d\n", num[--r]-num[--l]); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/34D - Road Map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 50010 3 | using namespace std; 4 | int n, r1, r2; 5 | vectoradj[MAX]; 6 | bool vis[MAX] = {}; 7 | int p[MAX]; 8 | void dfs(const int i){ 9 | vis[i] = true; 10 | const vector&v = adj[i]; 11 | for (int j = 0; j < v.size(); ++j){ 12 | int k = v[j]; 13 | if (!vis[k]) 14 | p[k] = i, dfs(k); 15 | } 16 | } 17 | int main(void){ 18 | // freopen("Input.txt", "r", stdin); 19 | scanf("%d%d%d", &n, &r1, &r2); 20 | for (int i = 1, x; i <= n; ++i) 21 | if (i != r1) 22 | scanf("%d", &x), adj[i].push_back(x), adj[x].push_back(i); 23 | dfs(r2); 24 | for (int i = 1; i <= n; ++i) 25 | if (i != r2) 26 | printf("%d ", p[i]); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/CodeForces/381A - Sereja and Dima.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1005 3 | using namespace std; 4 | int a[MAX]; 5 | int n; 6 | int main(void){ 7 | // freopen("Input.txt", "r", stdin); 8 | scanf("%d", &n); 9 | for (int i = 0; i < n; ++i) 10 | scanf("%d", a + i); 11 | int ans[2] = {0, 0}; 12 | int j = 0, k = n - 1; 13 | for (int i = 0; i < n; ++i){ 14 | if (a[j] < a[k])ans[i&1] += a[k--]; 15 | else ans[i&1] += a[j++]; 16 | } 17 | printf("%d %d\n", ans[0], ans[1]); 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/CodeForces/431 D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define MAX 105 6 | #define MOD 1000000007 7 | int k, d; 8 | int memo[MAX][2]; 9 | int dp(int n, bool done){ 10 | if (memo[n][done] != -1) return memo[n][done]; 11 | if (!done && n < d) return memo[n][done] = 0; 12 | if (!n) return memo[n][done] = 1; 13 | int ans = 0; 14 | for (int i = 1; i <= min(k, n); ++i) ans = (ans + dp(n - i, i >= d || done))%MOD; 15 | return memo[n][done] = ans; 16 | } 17 | int main(){ 18 | memset(memo, 0xFF, sizeof memo); 19 | int n; 20 | scanf("%d%d%d", &n, &k, &d); 21 | printf("%d", dp(n, 0)); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/493 D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int n; 7 | scanf("%d", &n); 8 | if (n & 1) printf("black"); 9 | else printf("white\n1 2"); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/4B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 30 5 | using namespace std; 6 | int main(){ 7 | int d, sumTime; 8 | int minTotal = 0, maxTotal = 0; 9 | int minTime[MAX]; 10 | int maxTime[MAX]; 11 | scanf("%d%d", &d, &sumTime); 12 | for (int i = 0; i < d; ++i){ 13 | scanf("%d%d", &minTime[i], &maxTime[i]); 14 | minTotal += minTime[i]; 15 | maxTotal += maxTime[i]; 16 | } 17 | if (sumTime < minTotal || sumTime > maxTotal){ 18 | printf("NO"); 19 | return 0; 20 | } 21 | sumTime -= minTotal; 22 | printf("YES\n"); 23 | for (int i = 0; i < d && sumTime; ++i){ 24 | int x = min(sumTime, maxTime[i] - minTime[i]); 25 | minTime[i] += x; 26 | sumTime -= x; 27 | } 28 | for (int i = 0; i < d; ++i) printf("%d ", minTime[i]); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/524C The Art of Dealing with ATM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int memo[22][5005]; // memo[i][j] = value of i bills of type j 4 | int den[5005]; 5 | int n, k; 6 | int main(void){ 7 | // freopen("Input.txt", "r", stdin); 8 | memset(memo[0], 0, sizeof memo[0]); 9 | scanf("%d%d", &n, &k); 10 | for (int i = 0; i < n; ++i) 11 | scanf("%d", den + i); 12 | for (int i = 1; i <= k; ++i) 13 | for (int j = 0; j < n; ++j) 14 | memo[i][j] = i*den[j]; 15 | int q; 16 | scanf("%d", &q); 17 | while(q--){ 18 | int val; scanf("%d", &val); 19 | int ans = INT_MAX; 20 | for (int i = 0; i <= k; ++i) 21 | for (int j = 0; j < n; ++j){ 22 | int cur = val - memo[i][j]; 23 | for (int l = k - i; l >= 0; --l) 24 | if (binary_search(memo[l], memo[l] + n, cur)) 25 | ans = min(ans, i + l); 26 | } 27 | if (ans == INT_MAX) 28 | printf("-1\n"); 29 | else printf("%d\n", ans); 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Solutions/CodeForces/597 A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | long long k, a, b; 5 | cin >> k >> a >> b; 6 | if (a>0 && a%k)a = a / k + 1; 7 | else a /= k; 8 | if (b<0 && b%k) b = b/ k - 1; 9 | else b /= k; 10 | cout << b-a+1; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/598 A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int numTwos(int n) 5 | { 6 | int num = 0; 7 | int x = n; 8 | while (x){ 9 | num++; 10 | x >>= 1; 11 | } 12 | return num; 13 | } 14 | typedef long long LL; 15 | int main(){ 16 | int t, n; 17 | scanf("%d\n", &t); 18 | LL sum; 19 | LL sum2; 20 | int power2; 21 | while (t--){ 22 | scanf("%d\n", &n); 23 | sum = LL(LL(n)*LL(n + 1))/2LL; 24 | power2 = numTwos(n); 25 | sum2 = 1; 26 | while (power2--){ sum2 *= 2; } 27 | sum2--; 28 | sum -= sum2; 29 | sum -= sum2; 30 | cout << sum << '\n'; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/598 B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | string s, sub; 8 | cin >> s; 9 | int m; 10 | int l, r, k; 11 | scanf("%d\n", &m); 12 | m--; 13 | while (m--){ 14 | scanf("%d %d %d\n", &l, &r, &k); 15 | l--; 16 | sub = s.substr(l, r - l); 17 | k %= sub.length(); 18 | sub = sub.substr(sub.length() - k) + sub.substr(0, sub.length() - k); 19 | s.replace(l, r-l, sub); 20 | } 21 | scanf("%d %d %d", &l, &r, &k); 22 | l--; 23 | sub = s.substr(l, r - l); 24 | k %= sub.length(); 25 | sub = sub.substr(sub.length() - k) + sub.substr(0, sub.length() - k); 26 | s.replace(l, r - l, sub); 27 | // 28 | cout << s; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/59B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 105 5 | using namespace std; 6 | int n; 7 | int main(){ 8 | freopen("Input.txt", "r", stdin); 9 | scanf("%d", &n); 10 | int x; 11 | int numOdd = 0; 12 | int minOdd = INT_MAX; 13 | int ans = 0; // don't forget to initialize! 14 | for (int i = 0; i < n; ++i){ 15 | scanf("%d", &x); 16 | ans += x; 17 | if (!(x & 1)) continue; 18 | ++numOdd; 19 | minOdd = min(minOdd, x); 20 | } 21 | if (!numOdd){ 22 | printf("0"); 23 | return 0; 24 | } 25 | if (!(numOdd & 1)) ans -= minOdd; 26 | printf("%d", ans); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/616D - Longest k-Good Segment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 500005 4 | using namespace std; 5 | int counter[1000005] = {0}; 6 | typedef long long ll; 7 | int a[MAX]; 8 | int n, k; 9 | int main(void){ 10 | // freopen("Input.txt", "r", stdin); 11 | scanf("%d%d", &n, &k); 12 | for (int i = 0; i < n; ++i) 13 | scanf("%d", a + i); 14 | int ans_i = 0, ans_j = 0, ans = 0; 15 | setcur; 16 | for (int i = 0, j = 0;j < n; ++j){ 17 | int temp = a[j]; 18 | counter[temp]++; 19 | cur.insert(temp); 20 | if (cur.size() > k){ 21 | // if you exceed the allowed number of differences, fix it 22 | while(1){ 23 | if (!--counter[temp = a[i++]]){ 24 | cur.erase(temp); 25 | break; 26 | } 27 | } 28 | } 29 | if (ans < j - i) 30 | ans_i = i, ans_j = j, ans = j-i; 31 | } 32 | printf("%d %d", ans_i + 1, ans_j + 1); 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Solutions/CodeForces/622 A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | long long n; 8 | cin >> n; 9 | long long i = 1; 10 | while (i*(i+1LL)/2LL < n) i++; 11 | i--; 12 | cout << n - i*(i + 1LL) / 2LL; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/622 B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int h, m, a; 8 | scanf("%d:%d\n%d", &h, &m, &a); 9 | h += (a / 60); 10 | m += (a % 60); 11 | h += (m >= 60); 12 | h %= 24; 13 | m %= 60; 14 | cout << setfill('0') << setw(2) << h << ':' << setw(2) << m; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/625 C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | freopen("Input.txt", "r", stdin); 7 | freopen("Output.txt", "w", stdout); 8 | int n, k; 9 | scanf("%d%d", &n, &k); 10 | int val1 = n*n + 1, val2 = n - k + 1; 11 | printf("%d\n", n*(val1)-(val2)*(n*(n + 1)) / 2); 12 | int counter = 1, count = 1, counting; 13 | int x, y; 14 | for (int i = 0; i < n; i++){ 15 | counting = 1; 16 | for (int j = 1; j < k; j++){ 17 | printf("%d ", counter++); counting++; 18 | } 19 | y = val1 - count++*(val2); 20 | if (counting == n) { printf("%d", y); counting++; } 21 | else { printf("%d ", y); counting++; } 22 | for (int j = 1; j <= n-k; j++){ 23 | if (counting == n){ printf("%d", y + j); counting++; } 24 | else { printf("%d ", y + j); counting++; } 25 | } 26 | printf("\n"); 27 | } 28 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/628 A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | freopen("Input.txt", "r", stdin); 7 | freopen("Output.txt", "w", stdout); 8 | int n, b, p; 9 | scanf("%d%d%d", &n, &b, &p); 10 | int numTowels = n * p; 11 | int numBottles = 0; 12 | int k, m; 13 | while (n > 1){ 14 | k = -1; 15 | m = n; 16 | while (m) 17 | { 18 | k++; 19 | m >>= 1; 20 | } 21 | k = 1 << k; 22 | n -= (k/2); 23 | numBottles += (k*b + k/2); 24 | } 25 | printf("%d %d", numBottles, numTowels); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/628 B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | freopen("Input.txt", "r", stdin); 10 | freopen("Output.txt", "w", stdout); 11 | char s[300000 + 5]; 12 | long long n = 0; 13 | scanf("%s", s); 14 | for (int i = 1; s[i] != '\0'; i++){ 15 | if (!(s[i]-48 & 3)) 16 | n++; 17 | if (!(((s[i-1] - 48)*10 + (s[i] - 48) )&3)) 18 | n += i; 19 | } 20 | if (!(s[0]&3))n++; 21 | cout << n; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/628 C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | freopen("Input.txt", "r", stdin); 8 | freopen("Output.txt", "w", stdout); 9 | int n, k; 10 | scanf("%d%d", &n, &k); 11 | char s[100005]; 12 | scanf("%s", s); 13 | int i = 0, m = k; 14 | char x, y, c; 15 | while (m > 0 && (c = s[i])){ 16 | if ((x = c - 'a') > (y = 'z' - c)){ 17 | m -= x; 18 | s[i] = 'a'; 19 | } 20 | else { 21 | m -= y; 22 | s[i] = 'z'; 23 | } 24 | i++; 25 | } 26 | if (m > 0) { printf("-1"); return 0; } 27 | if (m < 0) { if (s[i - 1] + m >= 'a') s[i - 1] += m; else s[i - 1] -= m; } 28 | printf("%s", s); 29 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/659 A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n, a, b; 5 | scanf("%d%d%d", &n, &a, &b); 6 | // 'b' BEING NEGATIVE IS A CORNER CASE 7 | b += (100 * n); 8 | b %= n; 9 | a += b; 10 | a %= n; 11 | // 'a' BEING ZERO IS A CORNER CASE 12 | if (!a) printf("%d", n); 13 | else printf("%d", a); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/659 B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef pair ii; 6 | char name[100005][12]; 7 | vectorv[10005]; 8 | int main(){ 9 | // freopen("input.txt", "r", stdin); 10 | int n, m; 11 | scanf("%d%d", &n, &m); 12 | for (int i = 0; i < n; ++i) { 13 | int x, y; 14 | scanf("%s%d%d", name[i], &x, &y); 15 | v[x].push_back(ii(-y, i)); 16 | } 17 | for (int i = 1; i <= m; ++i) { 18 | sort(v[i].begin(), v[i].end()); 19 | if (v[i].size() < 3 || v[i][1].first != v[i][2].first) printf("%s %s\n", name[v[i][0].second], name[v[i][1].second]); 20 | else printf("?\n"); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/659 C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | // freopen("Input.txt", "r", stdin); 6 | int n, m; 7 | int a[100005]; 8 | scanf("%d%d", &n, &m); 9 | for (int i = 0; i < n; ++i) scanf("%d", a + i); 10 | sort(a, a + n); 11 | int k = 0; 12 | int b[100005]; 13 | // THIS IS HOW YOU CAN LOOP OVER ELEMENTS NOT IN THE ARRAY 14 | int j = 0; 15 | int i = 1; 16 | while (m >= i){ 17 | for (; i != a[j] && m >= i; ++i) 18 | { 19 | m -= i; 20 | b[k++] = i; 21 | } 22 | ++i; 23 | ++j; 24 | } 25 | printf("%d\n", k); 26 | for (int i = 0; i < k; ++i) printf("%d ", b[i]); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/689 C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | ll cube(ll k){ 5 | return k*(k*k); 6 | } 7 | ll m; 8 | ll ans; 9 | bool can (ll n){ 10 | // count the number of sequences if you have capacity n 11 | ans = 0LL; 12 | for (ll k = 2LL;; ++k){ 13 | ll t = cube(k); 14 | ll i = n/t; 15 | if (!i) break; 16 | ans += i; 17 | } 18 | return ans >= m; 19 | } 20 | int main(void){ 21 | cin >> m; 22 | ll lo = 0, hi = (1LL << 63LL) - 1LL; 23 | while(lo != hi){ 24 | // minimize n 25 | ll mid = min((lo + hi)/2LL, hi - 1); 26 | if (can(mid)) 27 | hi = mid; 28 | else lo = mid + 1; 29 | } 30 | can(lo); 31 | if (ans == m) cout << lo; 32 | else cout << -1; 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Solutions/CodeForces/702C - Cellular Network.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 100100 4 | using namespace std; 5 | int n, m; 6 | int city[MAX], tower[MAX]; 7 | inline void read(void) 8 | { 9 | scanf("%d%d", &n, &m); 10 | for (int i = 0; i < n; ++i) 11 | scanf("%d", city + i); 12 | for (int i = 0; i < m; ++i) 13 | scanf("%d", tower + i); 14 | } 15 | int main(void) 16 | { 17 | #ifndef ONLINE_JUDGE 18 | freopen("Input.txt", "r", stdin); 19 | #endif 20 | read(); 21 | int ans = 0; 22 | for (int i = 0; i < n; ++i) 23 | { 24 | const int j = lower_bound(tower, tower + m, city[i]) - tower; 25 | int cur_dist = INT_MAX; 26 | if (j < m) 27 | cur_dist = min(cur_dist, tower[j] - city[i]); 28 | if (j - 1 >= 0) 29 | cur_dist = min(cur_dist, city[i] - tower[j - 1]); 30 | ans = max(ans, cur_dist); 31 | } 32 | 33 | printf("%d\n", ans); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/703 A. Mishka and Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void){ 4 | int m = 0, c = 0; 5 | int n; scanf("%d", &n); 6 | for(int x, y; n--;){ 7 | scanf("%d%d", &x, &y); 8 | m += (x > y); 9 | c += (y > x); 10 | } 11 | if (m > c) printf("Mishka"); 12 | else if (c > m) printf("Chris"); 13 | else printf("Friendship is magic!^^"); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Solutions/CodeForces/709 A. Juicer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100100 3 | using namespace std; 4 | int arr[MAX]; 5 | int n, b, d; 6 | int main() { 7 | scanf("%d%d%d", &n, &b, &d); 8 | for (int i = 0; i < n; ++i) 9 | { 10 | scanf("%d", arr + i); 11 | } 12 | int ans = 0; 13 | int cur_size = 0; 14 | for (int i = 0; i < n; ++i) 15 | { 16 | if (arr[i] > b) 17 | { 18 | continue; 19 | } 20 | cur_size += arr[i]; 21 | if (cur_size > d) 22 | { 23 | cur_size = 0; 24 | ++ans; 25 | } 26 | } 27 | printf("%d", ans); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/709 C. Letters Cyclic Shift.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n; 4 | char s[100100]; 5 | inline int first_non_a(int i) 6 | { 7 | while(i < n && s[i] == 'a') 8 | ++i; 9 | return i; 10 | } 11 | inline int first_a(int i) 12 | { 13 | while(i < n && s[i] != 'a') 14 | ++i; 15 | return i; 16 | } 17 | int main() { 18 | scanf("%s", s); 19 | n = strlen(s); 20 | int start = first_non_a(0); 21 | if (start == n) 22 | { 23 | s[n - 1] = 'z'; 24 | printf("%s", s); 25 | return 0; 26 | } 27 | int end = first_a(start + 1); 28 | for (int i = start; i < end; ++i) 29 | { 30 | --s[i]; 31 | } 32 | printf("%s", s); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/710C Magic Odd Square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 64 3 | using namespace std; 4 | int n; 5 | int memo[MAX][MAX]; 6 | bool vis[MAX][MAX]; 7 | void solve(void) { 8 | int i = 0, j = n >> 1; 9 | for (int v = 1; v <= n * n; ++v) 10 | { 11 | memo[i][j] = v; 12 | vis[i][j] = 1; 13 | const int x = (i - 1 + n) % n; 14 | const int y = (j + 1) % n; 15 | if (!vis[x][y]) 16 | { 17 | i = x; 18 | j = y; 19 | } 20 | else 21 | { 22 | i = (i + 1) % n; 23 | } 24 | } 25 | } 26 | int main(void) 27 | { 28 | scanf("%d", &n); 29 | solve(); 30 | for (int i = 0; i < n; ++i) 31 | for (int j = 0; j < n; ++j) 32 | printf("%d%c", memo[i][j], j == n - 1 ? '\n' : ' '); 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Solutions/CodeForces/717C Potions Homework.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100100 3 | #define MOD 10007LL 4 | using namespace std; 5 | typedef long long int ll; 6 | int arr[MAX]; 7 | int n; 8 | int main(void) 9 | { 10 | scanf("%d", &n); 11 | for (int i = 0; i < n; ++i) 12 | scanf("%d", arr + i); 13 | sort(arr, arr + n); 14 | long long int ans = 0; 15 | for (int i = 0; i < n; ++i) 16 | ans = ((ll) ans + ll(arr[i]) * 1LL * ll(arr[n - 1 - i] * 1LL)) % MOD; 17 | cout << ans; 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/115A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define Num 2005 4 | using namespace std; 5 | 6 | int main(){ 7 | int n, x; 8 | int p[Num]; 9 | bool processed[Num]; 10 | scanf("%d", &n); 11 | for (int i = 0; i < n; i++){ 12 | processed[i] = false; 13 | p[i] = i; 14 | scanf("%d", &x); 15 | if (x != -1) p[i] = --x; 16 | } 17 | // 18 | int cur, Max = 1; 19 | for (int i = 0; i < n; ++i){ 20 | if (processed[i]) continue; 21 | processed[i] = true; 22 | x = i; 23 | cur = 1; 24 | while (p[x] != x) processed[x = p[x]] = true, cur++; 25 | Max = max(Max, cur); 26 | } 27 | printf("%d\n", Max); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/189A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 4040 3 | #define MOD 1000000007 4 | using namespace std; 5 | int memo[MAX]; 6 | int a, b, c; 7 | inline int dp(const int i){ 8 | if (i < 0) return -1000000000; 9 | int & ret = memo[i]; 10 | if (ret != -1) return ret; 11 | return ret = 1 + max(max(dp(i - a), dp(i - b)), dp(i - c)); 12 | } 13 | int main(void){ 14 | memset(memo, 0xFF, sizeof memo); 15 | memo[0] = 0; 16 | int n; 17 | scanf("%d%d%d%d", &n, &a, &b, &c); 18 | printf("%d\n", dp(n)); 19 | } 20 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/230B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | vector primes; 5 | vector t_primes; 6 | inline void sieve() 7 | { 8 | const int bound = 1000000; 9 | bitset bs; 10 | bs.set(); 11 | bs[0] = bs[1] = 0; 12 | for (int i = 2; i <= bound; ++i) 13 | { 14 | if (bs[i]) 15 | { 16 | primes.push_back(i); 17 | if ((ll) 1LL * i * i <= bound) 18 | for (int j = i * i; j <= bound; j += i) 19 | { 20 | bs[j] = 0; 21 | } 22 | } 23 | } 24 | } 25 | int main() 26 | { 27 | sieve(); 28 | for (int p : primes) 29 | { 30 | t_primes.push_back((ll) p * p); 31 | } 32 | int n; cin >> n; 33 | for(ll x; n--; ) 34 | { 35 | cin >> x; 36 | cout << (binary_search(t_primes.begin(), t_primes.end(), x) ? "YES" : "NO") << endl; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/246D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100005 3 | using namespace std; 4 | int n, m; 5 | int color[MAX]; 6 | set adj[MAX]; 7 | int ans_idx = MAX, ans_val = 0; 8 | inline void read() 9 | { 10 | cin >> n >> m; 11 | for (int i = 0; i < n; ++i) 12 | { 13 | cin >> color[i]; 14 | ans_idx = min(ans_idx, color[i]); 15 | } 16 | for (int a, b; m--; ) 17 | { 18 | cin >> a >> b; 19 | --a; --b; 20 | a = color[a]; 21 | b = color[b]; 22 | if (a != b) 23 | { 24 | adj[a].insert(b); 25 | adj[b].insert(a); 26 | } 27 | } 28 | } 29 | int main() 30 | { 31 | read(); 32 | for (int i = 0; i < MAX; ++i) 33 | { 34 | if (adj[i].size() > ans_val) 35 | { 36 | ans_val = adj[i].size(); 37 | ans_idx = i; 38 | } 39 | } 40 | cout << ans_idx; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/258A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | string s; 4 | inline string solve() 5 | { 6 | for (int i = 0; i < s.length(); ++i) 7 | { 8 | if (s[i] == '0') 9 | { 10 | return s.substr(0, i) + s.substr(i + 1); 11 | } 12 | } 13 | return string(s.length() - 1, '1'); 14 | } 15 | int main() 16 | { 17 | cin >> s; 18 | cout << solve(); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/334A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 102 3 | using namespace std; 4 | int n; 5 | int ans[MAX][MAX]; 6 | int main() 7 | { 8 | cin >> n; 9 | // loop over rows! 10 | int a = 1, b = n * n; 11 | for (int r = 0; r < n; ++r) 12 | { 13 | for (int c1 = 0, c2 = n - 1; c1 < c2; ++c1, --c2) 14 | { 15 | ans[r][c1] = a++; 16 | ans[r][c2] = b--; 17 | } 18 | } 19 | for (int r = 0; r < n; ++r) 20 | { 21 | for (int c = 0; c < n; ++c) 22 | { 23 | printf("%d%c", ans[r][c], c == n - 1 ? '\n' : ' '); 24 | } 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/349B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | char s[1000000]; 7 | int Count; 8 | int main() { 9 | Count = 0; 10 | int v; 11 | int a[10]; 12 | scanf("%d", &v); 13 | for (int i = 1; i <= 9; ++i) scanf("%d", &a[i]); 14 | int Min = INT_MAX; 15 | for (int i = 1; i <= 9; ++i) Min = min(Min, a[i]); 16 | if (v < Min){ 17 | printf("-1"); return 0; 18 | } 19 | Count = v/Min; 20 | int x; 21 | for (int i = 9;; --i) if (a[i] == Min) {x = i; break;} 22 | memset(s, x, Count); 23 | int paint = v%Min; 24 | 25 | for (int i = Count-1; i>= 0 && paint; --i){ 26 | paint += a[s[i]]; 27 | for (int j = 9; j >= 1; --j) 28 | if (paint >= a[j]){ 29 | s[i] = j; 30 | paint -= a[j]; 31 | } 32 | } 33 | for (int i = Count -1; i >= 0; --i) printf("%d", int(s[i])); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/383D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1002 3 | #define MOD 1000000007 4 | using namespace std; 5 | int n, a[MAX]; 6 | int memo[MAX][MAX * 10 * 2]; 7 | inline int dp(const int i, const int j) 8 | { 9 | int & ret = memo[i][j + MAX * 10]; 10 | if (ret != -1) return ret; 11 | ret = (j == 0); 12 | if (i < n) 13 | { 14 | ret = (ret + dp(i + 1, j - a[i])) % MOD; 15 | ret = (ret + dp(i + 1, j + a[i])) % MOD; 16 | } 17 | return ret; 18 | } 19 | inline void init() 20 | { 21 | memset(memo, -1, sizeof memo); 22 | } 23 | inline void read() 24 | { 25 | scanf("%d", &n); 26 | for (int i = 0; i < n; ++i) scanf("%d", a + i); 27 | } 28 | int main() 29 | { 30 | read(); 31 | init(); 32 | int ans = 0; 33 | for (int i = 0; i < n; ++i) 34 | ans = (ans + dp(i, 0) - 1) % MOD; 35 | printf("%d", ans); 36 | return 0; 37 | } 38 | /* 39 | 4 40 | 1 1 1 1 41 | */ 42 | 43 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/383D.cpp~: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1002 3 | #define MOD 1000000007 4 | using namespace std; 5 | int n, a[MAX]; 6 | int memo[MAX][MAX * 10 * 2]; 7 | inline int dp(const int i, const int j) 8 | { 9 | int & ret = memo[i][j + MAX * 10]; 10 | if (ret != -1) return ret; 11 | ret = (j == 0); 12 | if (i < n) 13 | { 14 | ret = (ret + dp(i + 1, j - a[i])) % MOD; 15 | ret = (ret + dp(i + 1, j + a[i])) % MOD; 16 | } 17 | return ret; 18 | } 19 | inline void init() 20 | { 21 | memset(memo, -1, sizeof memo); 22 | } 23 | inline void read() 24 | { 25 | scanf("%d", &n); 26 | for (int i = 0; i < n; ++i) scanf("%d", a + i); 27 | } 28 | int main() 29 | { 30 | read(); 31 | init(); 32 | int ans = 0; 33 | for (int i = 0; i < n; ++i) 34 | ans = (ans + dp(i, 0) - 1) % MOD; 35 | printf("%d", ans); 36 | return 0; 37 | } 38 | /* 39 | 4 40 | 1 1 1 1 41 | */ 42 | 43 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/431C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define MAX 105 6 | #define MOD 1000000007 7 | int k, d; 8 | int memo[MAX][2]; 9 | int dp(int n, bool done){ 10 | if (memo[n][done] != -1) return memo[n][done]; 11 | if (!done && n < d) return memo[n][done] = 0; 12 | if (!n) return memo[n][done] = 1; 13 | int ans = 0; 14 | for (int i = 1; i <= min(k, n); ++i) ans = (ans + dp(n - i, i >= d || done))%MOD; 15 | return memo[n][done] = ans; 16 | } 17 | int main(){ 18 | memset(memo, 0xFF, sizeof memo); 19 | int n; 20 | scanf("%d%d%d", &n, &k, &d); 21 | printf("%d", dp(n, 0)); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/476A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 10010 3 | #define OO 1000000 4 | using namespace std; 5 | int n, m; 6 | int memo[MAX][10]; 7 | /* 8 | * what is the minimum number of jumps, required to climb a distance of i, 9 | * knowing that your current number of moves is j 10 | */ 11 | inline int dp(const int i, const int j) 12 | { 13 | int & ret = memo[i][j]; 14 | // memorization 15 | if (ret != -1) return ret; 16 | // base case 17 | if (!i) return ret = (!j ? 0 : OO); 18 | // recursion 19 | return ret = 1 + min(i >= 2 ? dp(i - 2, (j + 1) % m) : OO, dp(i - 1, (j + 1) % m)); 20 | } 21 | int main() 22 | { 23 | memset(memo, -1, sizeof memo); 24 | cin >> n >> m; 25 | const int ans = dp(n, 0); 26 | cout << (ans < OO ? ans : -1); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Solutions/CodeForces/A2OJ ladder 16/59A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 105 3 | using namespace std; 4 | string s; 5 | int count_lower, count_upper; 6 | int main() 7 | { 8 | cin >> s; 9 | for (const char * c = s.c_str(); *c; ++c) 10 | { 11 | if (islower(*c)) 12 | ++count_lower; 13 | else ++count_upper; 14 | } 15 | if (count_upper > count_lower) 16 | { 17 | for (char * c = const_cast(s.c_str()); *c; ++c) 18 | { 19 | if (islower(*c)) 20 | { 21 | *c = toupper(*c); 22 | } 23 | } 24 | } 25 | else 26 | { 27 | for (char * c = const_cast(s.c_str()); *c; ++c) 28 | { 29 | if (isupper(*c)) 30 | { 31 | *c = tolower(*c); 32 | } 33 | } 34 | } 35 | cout << s; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AAST ACM ICPC 2016 - Individuals Qualification #2/B - Brownies vs. Candies vs. Cookies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int s, b, m; 4 | int main(void) 5 | { 6 | int TC; scanf("%d", &TC); 7 | for (int tc = 1; tc <= TC; ++tc) 8 | { 9 | scanf("%d%d", &s, &b); 10 | printf("Practice #%d: %d %d\n", tc, s, b); 11 | scanf("%d", &m); 12 | for(int x; m--;) 13 | { 14 | scanf("%d", &x); 15 | while(b <= x) 16 | { 17 | b <<= 1; 18 | } 19 | 20 | b -= x; 21 | printf("%d %d\n", x, b); 22 | } 23 | printf("\n"); 24 | } 25 | return 0; 26 | } 27 | /* 28 | 2 29 | 20 60 30 | 8 31 | 15 32 | 10 33 | 20 34 | 18 35 | 9 36 | 12 37 | 2 38 | 10 39 | 15 100 40 | 4 41 | 1 42 | 2 43 | 3 44 | 5 45 | */ 46 | 47 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AAST ACM ICPC 2016 - Individuals Qualification #2/C - Towers of Hanoi Grid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int d, n; 4 | int main(void) 5 | { 6 | int TC; scanf("%d", &TC); 7 | for(int tc = 1; tc <= TC; ++tc) 8 | { 9 | scanf("%d%d", &d, &n); 10 | printf("Grid #%d: ", tc); 11 | const int free = (n - 1) * (n - 1); 12 | if (free >= d - 1) 13 | { 14 | printf("%d", (d << 1) * (n - 1)); 15 | } 16 | else 17 | { 18 | printf("impossible"); 19 | } 20 | printf("\n\n"); 21 | } 22 | return 0; 23 | } 24 | /* 25 | 3 26 | 2 2 27 | 100 8 28 | 3 100 29 | */ 30 | 31 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AAST ACM ICPC 2016 - Individuals Qualification #2/F - Medal Ranking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void) 4 | { 5 | int TC; scanf("%d", &TC); 6 | while(TC--) 7 | { 8 | int a, b, c, d, e, f; 9 | scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f); 10 | int tot_1 = a + b + c; 11 | int tot_2 = d + e + f; 12 | printf("%d %d %d %d %d %d\n", a, b, c, d, e, f); 13 | bool win_count = tot_1 > tot_2; 14 | bool win_color = (a != d) ? (a > d) : ((b != e) ? b > e : c > f); 15 | printf("%s\n\n", win_count && win_color ? "both" : win_count ? "count" : win_color ? "color" : "none"); 16 | } 17 | return 0; 18 | } 19 | /* 20 | 5 21 | 10 5 15 10 1 0 22 | 10 5 15 10 6 10 23 | 12 5 10 5 20 30 24 | 10 0 15 10 5 30 25 | 10 5 15 10 5 15 26 | */ 27 | 28 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AAST ACM ICPC 2016 - Individuals Qualification #2/K - Find the Twins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void) 4 | { 5 | int TC; scanf("%d", &TC); 6 | while(TC--) 7 | { 8 | bool mack = false, zack = false; 9 | for (int i = 0, x; i < 10; ++i) 10 | { 11 | scanf("%d", &x); 12 | printf("%d%c", x, i == 9 ? '\n' : ' '); 13 | mack |= (x == 18); 14 | zack |= (x == 17); 15 | } 16 | 17 | printf("%s\n\n", mack && zack ? "both" : mack ? "mack" : zack ? "zack" : "none"); 18 | } 19 | return 0; 20 | } 21 | /* 22 | 4 23 | 11 99 88 17 19 20 12 13 33 44 24 | 11 12 13 14 15 16 66 88 19 20 25 | 20 18 55 66 77 88 17 33 44 11 26 | 12 23 34 45 56 67 78 89 91 18 27 | */ 28 | 29 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AAST ACM ICPC 2016 - Individuals Qualification #2/contest_link.txt: -------------------------------------------------------------------------------- 1 | http://codeforces.com/group/bJPnH3mR5X/contest/208581 2 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AUC ACM ICPC 2016 - Individuals Qualification #1/B - Heavy Caffeine Helps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(void) 5 | { 6 | int TC; scanf("%d", &TC); 7 | for (int ox, x, y; TC--;) 8 | { 9 | scanf("%d%d%d", &ox, &x, &y); 10 | const int inc = max(ox - x, 0); 11 | printf("%d\n", inc / y + (bool(inc % y))); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AUC ACM ICPC 2016 - Individuals Qualification #1/J - Balloons Colors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 2505 4 | using namespace std; 5 | int main(void) 6 | { 7 | // freopen("Input.txt", "r", stdin); 8 | int TC; scanf("%d", &TC); 9 | bool a, b; 10 | for (int r, g, b, t, i, j; TC--;) 11 | { 12 | scanf("%d%d%d", &r, &g, &b); 13 | t = r | (g << 8) | (b << 16); 14 | for (i = 0, j = 23; i < j; ++i, --j) 15 | { 16 | a = bool(t & (1 << i)); 17 | b = bool(t & (1 << j)); 18 | if (a != b) 19 | { 20 | t &= ~(1 << i); 21 | t &= ~(1 << j); 22 | } 23 | } 24 | printf("%.3d %.3d %.3d\n", t & 0xFF, (t >> 8) & 0xFF, (t >> 16) & 0xFF); 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solutions/CodeForces/AUC ACM ICPC 2016 - Individuals Qualification #1/contest_link.txt: -------------------------------------------------------------------------------- 1 | http://codeforces.com/group/bJPnH3mR5X/contest/208627 2 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AUC ACM ICPC 2016 - Individuals Qualification #3/A - Equilaterals Intersection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const double PI = acos(-1.0); 4 | int n, s; 5 | int main(void) 6 | { 7 | freopen("equilaterals.in", "r", stdin); 8 | const double factor = sin(PI / 3.0) / 3.0; 9 | int TC; scanf("%d", &TC); 10 | while(TC--) 11 | { 12 | scanf("%d%d", &n, &s); 13 | const int num_sides = n * 3; 14 | const double alpha = (double) PI / num_sides; 15 | const double l = (double) s * factor; 16 | 17 | printf("%.4lf\n", l * l * tan(alpha) * num_sides); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AUC ACM ICPC 2016 - Individuals Qualification #3/C - Gold for the Bride.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void) 4 | { 5 | freopen("gold.in", "r", stdin); 6 | int TC; scanf("%d", &TC); 7 | for(int a, b, c; TC--; ) 8 | { 9 | scanf("%d%d%d", &a, &b, &c); 10 | printf("%s\n", a == b && b == c ? "yes" : "no"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Solutions/CodeForces/AUC ACM ICPC 2016 - Individuals Qualification #3/contest_link.txt: -------------------------------------------------------------------------------- 1 | http://codeforces.com/group/bJPnH3mR5X/contest/208690 2 | -------------------------------------------------------------------------------- /Solutions/CodeForces/USP Tryouts 2010/Problems - Codeforces.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/CodeForces/USP Tryouts 2010/Problems - Codeforces.pdf -------------------------------------------------------------------------------- /Solutions/Foobar/01/decode.py: -------------------------------------------------------------------------------- 1 | import ast 2 | import base64 3 | import sys 4 | 5 | MESSAGE = '''GU4AHQwaBxwAT1JbQk4UGgoYFkhfSFUCDQUfDQ4eFwpUSEhBRQwAHAocDwoXT15BRQwVDgALFhxU SEhBRQAdCx0cBgYRBBdGTklUCQwRCwoFDR8EDB1USFVZRRodBB0CCQwXT0NZRR0SChAIFhpUSFVZ RRwSDhdGTklUDgAWRU9JSFUWCwdSTxI=''' 6 | KEY = 'bishoyboshra' 7 | 8 | #for i, c in enumerate(base64.b64decode(MESSAGE)): 9 | # sys.stdout.write(chr(ord(c) ^ ord(KEY[i % len(KEY)]))) 10 | 11 | message = ast.literal_eval( 12 | ''.join( 13 | [(chr(ord(c) ^ ord(KEY[i % len(KEY)]))) 14 | for i, c in enumerate(base64.b64decode(MESSAGE))] 15 | ) 16 | ) 17 | 18 | for k, v in message.iteritems(): 19 | print('%s: %s' % (k, v)) 20 | 21 | -------------------------------------------------------------------------------- /Solutions/Foobar/02/decode.py: -------------------------------------------------------------------------------- 1 | import base64 2 | import sys 3 | 4 | MESSAGE = '''GU4AHQwaBxwAT1JbGRIFGxYJG15OT1QLHQ1VUAMOBg1IWVhPVA0BFVxQDwwXT0NZRQoVDh0TTUZF SUlISBAMDAENFghbWQdOX0hIGAEHGg0EBFRQDB1USFVZRRodBB0CUlAGTl9ISAsDDREBBhIeFVhJ VBsOHwdIX0hVB1ZaRUlJSEgOCwFSTw8=''' 5 | KEY = 'bishoyboshra95' 6 | 7 | for i, c in enumerate(base64.b64decode(MESSAGE)): 8 | sys.stdout.write(chr(ord(c) ^ ord(KEY[i % len(KEY)]))) 9 | 10 | -------------------------------------------------------------------------------- /Solutions/ICPC/2013 ACPC/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int t; 5 | string s; 6 | cin >> t; 7 | while(t--) { 8 | cin >> s; 9 | unsigned long long ans = 1; 10 | int i = 0; 11 | while(s[i] == '?') i++; 12 | int j = s.size()-1; 13 | while(s[j] == '?') j--; 14 | char c = s[i]; 15 | bool start = false; 16 | int count = 0; 17 | //cout << i << " " << j << endl; 18 | for(int x = i+1; x <= j; x++) { 19 | if(s[x] == '?') { 20 | start = true; 21 | count++; 22 | } else if(start) { 23 | //cout << c << " " << s[x] << endl; 24 | if(s[x] != c) ans = (ans * (count + 1)) % 1000000007; 25 | start = false; 26 | count = 0; 27 | c = s[x]; 28 | } else { 29 | c = s[x]; 30 | } 31 | } 32 | cout << ans << endl; 33 | } 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Solutions/ICPC/2013 ACPC/12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t, n, x, y; 6 | cin >> t; 7 | while(t--) { 8 | cin >> n >> x >> y; 9 | if(x <= n) { 10 | if(y == 1) { 11 | for(int i=1; i<=n+1; i++) { 12 | if(i != x) cout << i; 13 | if(i != n+1 && i != x) cout << " "; 14 | } 15 | } else { 16 | for(int i=1; i<=n; i++) { 17 | cout << i; 18 | if(i != n) cout << " "; 19 | } 20 | } 21 | } else { 22 | if(y == 1) { 23 | for(int i=1; i<=n; i++) { 24 | cout << i; 25 | if(i != n) cout << " "; 26 | } 27 | } else { 28 | for(int i=1; i 2 | using namespace std; 3 | 4 | int a[105]; 5 | int n, x, y; 6 | 7 | int main(){ 8 | int t; cin >> t; 9 | while(t--){ 10 | cin >> n >> x >> y; 11 | for (int i = 0; i < n; i ++){ 12 | cin >> a[i]; 13 | } 14 | bool hard = a[n-1] == y; 15 | bool easy = a[0] == x; 16 | if(hard && easy){ 17 | cout << "BOTH" << endl; 18 | } 19 | else if (hard){ 20 | cout << "HARD" << endl; 21 | } 22 | else if(easy){ 23 | cout << "EASY" << endl; 24 | } 25 | else{ 26 | cout << "OKAY" << endl; 27 | } 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Solutions/ICPC/2013 ACPC/6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | map dx; 5 | map dy; 6 | 7 | int main(){ 8 | dx['R'] = 1; 9 | dx['L'] = -1; 10 | dy['R'] = dy['L'] = 0; 11 | dy['U'] = 1; 12 | dy['D'] = -1; 13 | dx['U'] = dx['D'] = 0; 14 | int t; cin >> t; 15 | while(t--){ 16 | int x = 0, y = 0; 17 | int n_qm = 0; 18 | string inst; cin >> inst; 19 | for(char c : inst){ 20 | if(c != '?'){ 21 | x += dx[c]; 22 | y += dy[c]; 23 | } 24 | else{ 25 | n_qm ++; 26 | } 27 | } 28 | cout << x - n_qm << " " << y - n_qm << " " << x + n_qm << " " << y + n_qm << endl; 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/ICPC/2013 ACPC/7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; scanf("%d", &t); 6 | while(t--){ 7 | int n, m, x; 8 | scanf("%d %d %d", &n, &m, &x); 9 | // if(n % m == 0){ 10 | // if(x == m){ 11 | // printf("YES\n"); 12 | // } 13 | // else{ 14 | // printf("NO\n"); 15 | // } 16 | // } 17 | // else{ 18 | // if(x == n % m){ 19 | // printf("YES\n"); 20 | // } 21 | // else{ 22 | // printf("NO\n"); 23 | // } 24 | // } 25 | int winner = (n % m == 0) ? m : n % m; 26 | if(winner == x){ 27 | printf("YES\n"); 28 | } 29 | else{ 30 | printf("NO\n"); 31 | } 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Solutions/ICPC/2015 ECPC/F. Geometry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void main(){ 4 | int TC; 5 | scanf("%d", &TC; 6 | for (int x, y; TC--;) 7 | scanf("%d%d", &x, &y), printf("%s\n", x == y ? "Square" : "Rectangle"); 8 | } 9 | -------------------------------------------------------------------------------- /Solutions/ICPC/2015 ECPC/I. Salem.cpp: -------------------------------------------------------------------------------- 1 | #define Max 101 2 | #include 3 | #include 4 | #define LSOne(i) (i & (-i)) 5 | using namespace std; 6 | int counter; 7 | int x[Max]; 8 | int dist(int x, int y){ 9 | int count = 0; 10 | while (x || y){ 11 | if ((x & 1) ^ (y & 1)) count++; 12 | x >>= 1; y >>= 1; 13 | } 14 | return count; 15 | } 16 | inline int distance(const int x, const int y){ 17 | int z = x ^ y, ans = 0; 18 | while(z) ++ans, z -= LSOne(z); 19 | return ans; 20 | } 21 | int main(){ 22 | int t, n; 23 | scanf("%d", &t); 24 | while (t--){ 25 | scanf("%d", &n); 26 | counter = 0; 27 | while (n--) scanf("%d", &x[counter++]); 28 | int num = 0; 29 | for (int i = 0; i < counter - 1; i++) 30 | for (int j = i + 1; j < counter; j++) num = max(num, dist(x[i], x[j])); 31 | printf("%d\n", num); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Solutions/ICPC/2015 ECPC/K. PhD math.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1000100 3 | using namespace std; 4 | typedef long long int ll; 5 | int n; 6 | int d[MAX]; 7 | int rem[MAX][200]; 8 | ll a, b, p; 9 | inline void init_d(void){ 10 | for (int i = 0; i < n; ++i){ 11 | a *= 10LL; 12 | d[i] = a/b; 13 | a %= b; 14 | } 15 | } 16 | int main(void){ 17 | int TC; scanf("%d", &TC); 18 | while(TC--) 19 | { 20 | cin >> a >> b >> n >> p; 21 | init_d(); 22 | memset(rem, 0, sizeof rem); 23 | rem[0][d[0] % p] = 1; 24 | for (int i = 1; i < n; ++i) 25 | { 26 | const int cur_d = d[i]; 27 | int * v = rem[i]; 28 | int * u = rem[i-1]; 29 | v[cur_d % p] += 1; 30 | for (int j = 0; j < p; ++j) 31 | v[(j*10 + cur_d) % p] += u[j]; 32 | } 33 | ll ans = 0LL; 34 | for (int i = 0; i < n; ++i) 35 | ans += rem[i][0]; 36 | cout << ans << "\n"; 37 | } 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Solutions/ICPC/2015 ECPC/statements.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/ICPC/2015 ECPC/statements.pdf -------------------------------------------------------------------------------- /Solutions/ICPC/2015 SCPC/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 128 3 | using namespace std; 4 | 5 | char arr[MAX * MAX]; 6 | int ans[MAX]; 7 | int n; 8 | 9 | int main(void) 10 | { 11 | int TC; scanf("%d", &TC); 12 | while(TC--) 13 | { 14 | scanf("%d", &n); 15 | memset(ans, 0, sizeof ans); 16 | for (int iter = 0; iter < 3; ++iter) 17 | { 18 | scanf("%s", arr); 19 | int j = 0; 20 | for (int i = 0; i < n; ++i) 21 | { 22 | int repeat = 3; 23 | while(repeat--) 24 | { 25 | if (arr[j++] == '*') 26 | ans[i] += 4; 27 | } 28 | } 29 | } 30 | 31 | int x = 0; 32 | for (int i = 0; i < n; ++i) 33 | x = max(x, ans[i]); 34 | printf("%d\n", x); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Solutions/ICPC/2016 ECPC/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | inline int get_grundy(const int i, const int b) 4 | { 5 | if (b & 1) 6 | return i & 1; 7 | const int m = i % (b + 1); 8 | return (m == b) ? 2 : (m & 1); 9 | } 10 | inline int winner(const vector & v) 11 | { 12 | int ans = 0; 13 | for (int i = 0; i < v.size(); ++i) 14 | ans ^= v[i]; 15 | return ans ? 1 : 2; 16 | } 17 | int main(void) 18 | { 19 | // freopen("powers.in", "r", stdin); 20 | int TC; scanf("%d", &TC); 21 | for(int g; TC--;) 22 | { 23 | scanf("%d", &g); 24 | vector v; 25 | for(int b, n; g--;) 26 | scanf("%d%d", &b, &n), v.push_back(get_grundy(n, b)); 27 | printf("%d\n", winner(v)); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/ICPC/2016 ECPC/Problems - Codeforces.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/ICPC/2016 ECPC/Problems - Codeforces.pdf -------------------------------------------------------------------------------- /Solutions/ICPC/2016 MCPC/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100010 3 | using namespace std; 4 | int arr[MAX], n, k; 5 | 6 | bool check(int diam) 7 | { 8 | int i = 0; 9 | 10 | for (int iter = 0; iter < k && i < n; ++iter) 11 | { 12 | const int start = arr[i]; 13 | int j = i + 1; 14 | while(j < n && arr[j] - start <= diam) 15 | ++j; 16 | i = j; 17 | } 18 | 19 | return i == n; 20 | } 21 | 22 | int main(){ 23 | 24 | #ifdef ONLINE_JUDGE 25 | freopen("going.in", "r", stdin); 26 | #endif 27 | int t; 28 | scanf("%d", &t); 29 | while (t--) 30 | { 31 | scanf("%d %d", &n, &k); 32 | for (int i = 0; i < n; ++i) 33 | scanf("%d", arr + i); 34 | sort(arr, arr + n); 35 | int s = 0, e = arr[n - 1] - arr[0]; 36 | while (s != e) 37 | { 38 | int m = (s + e) >> 1; 39 | if (check(m)) 40 | e = m; 41 | else 42 | s = m + 1; 43 | } 44 | printf("%.3lf\n", (double) s * 0.5); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Solutions/ICPC/2016 MCPC/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | set s; 4 | inline void init() 5 | { 6 | s.clear(); 7 | } 8 | int n; 9 | inline void read(void) 10 | { 11 | scanf("%d", &n); 12 | char name[100]; 13 | for (int i = 0; i < 2 * n - 1; ++i) 14 | { 15 | scanf("%s", name); 16 | if (s.find(name) == s.end()) 17 | { 18 | s.insert(name); 19 | } 20 | else s.erase(name); 21 | } 22 | } 23 | int main(void) 24 | { 25 | freopen("joy.in", "r", stdin); 26 | int TC; scanf("%d", &TC); 27 | while(TC--) 28 | { 29 | init(); 30 | read(); 31 | printf("%s\n", (*s.begin()).c_str()); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Solutions/ICPC/2016 MCPC/L.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | inline ll solve() 5 | { 6 | ll ans = 0LL; 7 | int n, a; 8 | scanf("%d", &n); 9 | while(n--) 10 | { 11 | scanf("%d", &a); 12 | ans = ans + ll((a / 30) + bool(a % 30)); 13 | } 14 | return ans; 15 | } 16 | int main(void) 17 | { 18 | freopen("leap.in", "r", stdin); 19 | int TC; scanf("%d", &TC); 20 | while(TC--) 21 | { 22 | cout << solve() << '\n'; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Solutions/ICPC/2016 MCPC/Problems - Codeforces.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/ICPC/2016 MCPC/Problems - Codeforces.pdf -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ECPC/Problems - Codeforces.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/ICPC/2017 ECPC/Problems - Codeforces.pdf -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/Desktop/Balloon Colors.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/ICPC/2017 ICPC/Desktop/Balloon Colors.pdf -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/Desktop/kattislogin: -------------------------------------------------------------------------------- 1 | Kattis account name: team-070 2 | Kattis password: fo5v4f9AD 3 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/airport/airport-sample1.ans: -------------------------------------------------------------------------------- 1 | 76.157731059 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/airport/airport-sample1.in: -------------------------------------------------------------------------------- 1 | 7 2 | 0 20 3 | 40 0 4 | 40 20 5 | 70 50 6 | 50 70 7 | 30 50 8 | 0 50 9 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/airport/airport-sample2.ans: -------------------------------------------------------------------------------- 1 | 4510.149110617 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/airport/airport-sample2.in: -------------------------------------------------------------------------------- 1 | 3 2 | 0 2017 3 | -2017 -2017 4 | 2017 0 5 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/clue/clue-sample1.ans: -------------------------------------------------------------------------------- 1 | AGM 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/clue/clue-sample1.in: -------------------------------------------------------------------------------- 1 | 1 2 | B I P C F 3 | A G M - - - 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/clue/clue-sample2.ans: -------------------------------------------------------------------------------- 1 | E?? 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/clue/clue-sample2.in: -------------------------------------------------------------------------------- 1 | 2 2 | A B C D H 3 | F G M M 4 | F H M - * 5 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/clue/clue-sample3.ans: -------------------------------------------------------------------------------- 1 | ??? 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/clue/clue-sample3.in: -------------------------------------------------------------------------------- 1 | 3 2 | A C M S D 3 | B G S - G 4 | A H S - - S 5 | C J S * 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/improbable/improbable-sample1.ans: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/improbable/improbable-sample1.in: -------------------------------------------------------------------------------- 1 | 5 5 2 | 1 4 0 5 2 3 | 2 1 2 0 1 4 | 0 2 3 4 4 5 | 0 3 0 3 1 6 | 1 2 2 1 1 7 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/improbable/improbable-sample2.ans: -------------------------------------------------------------------------------- 1 | 30 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/improbable/improbable-sample2.in: -------------------------------------------------------------------------------- 1 | 2 3 2 | 50 20 3 3 | 20 10 3 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/money/money-sample1.ans: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/money/money-sample1.in: -------------------------------------------------------------------------------- 1 | 2 2 2 | 1 3 3 | 2 1 4 | 3 5 5 | 7 2 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/money/money-sample2.ans: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/money/money-sample2.in: -------------------------------------------------------------------------------- 1 | 1 2 2 | 10 10 3 | 9 11 4 | 11 9 5 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/posterize/posterize-sample1.ans: -------------------------------------------------------------------------------- 1 | 66670000 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/posterize/posterize-sample1.in: -------------------------------------------------------------------------------- 1 | 2 1 2 | 50 20000 3 | 150 10000 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/posterize/posterize-sample2.ans: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/posterize/posterize-sample2.in: -------------------------------------------------------------------------------- 1 | 2 2 2 | 50 20000 3 | 150 10000 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/posterize/posterize-sample3.ans: -------------------------------------------------------------------------------- 1 | 37500000 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/posterize/posterize-sample3.in: -------------------------------------------------------------------------------- 1 | 4 2 2 | 0 30000 3 | 25 30000 4 | 50 30000 5 | 255 30000 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/replicate/replicate-sample1.ans: -------------------------------------------------------------------------------- 1 | .# 2 | ## 3 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/replicate/replicate-sample1.in: -------------------------------------------------------------------------------- 1 | 10 10 2 | .#...#...# 3 | ##..##..## 4 | ##.#.##... 5 | ##.#.##... 6 | .#...##### 7 | ...##..#.# 8 | ......###. 9 | ##.#.##... 10 | #..#..#..# 11 | ##..##..## 12 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/replicate/replicate-sample2.ans: -------------------------------------------------------------------------------- 1 | #### 2 | #..# 3 | #.## 4 | ###. 5 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/replicate/replicate-sample2.in: -------------------------------------------------------------------------------- 1 | 8 8 2 | ##..#.## 3 | #.####.# 4 | .#.#.#.. 5 | .##.#.## 6 | .#.#.#.. 7 | .##.#.## 8 | #..#.### 9 | ##.#.##. 10 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/replicate/replicate-sample3.ans: -------------------------------------------------------------------------------- 1 | # 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/replicate/replicate-sample3.in: -------------------------------------------------------------------------------- 1 | 5 4 2 | #.... 3 | ..### 4 | ..### 5 | ..### 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/scenery/scenery-sample1.ans: -------------------------------------------------------------------------------- 1 | yes 2 | 3 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/scenery/scenery-sample1.in: -------------------------------------------------------------------------------- 1 | 2 10 2 | 0 15 3 | 5 20 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/scenery/scenery-sample2.ans: -------------------------------------------------------------------------------- 1 | no 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/scenery/scenery-sample2.in: -------------------------------------------------------------------------------- 1 | 2 10 2 | 1 15 3 | 0 20 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/scenery/scenery-sample3.ans: -------------------------------------------------------------------------------- 1 | yes 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/scenery/scenery-sample3.in: -------------------------------------------------------------------------------- 1 | 2 10 2 | 5 30 3 | 10 20 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/secretchamber/secretchamber-sample1.ans: -------------------------------------------------------------------------------- 1 | yes 2 | no 3 | no 4 | yes 5 | yes 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/secretchamber/secretchamber-sample1.in: -------------------------------------------------------------------------------- 1 | 9 5 2 | c t 3 | i r 4 | k p 5 | o c 6 | r o 7 | t e 8 | t f 9 | u h 10 | w p 11 | we we 12 | can the 13 | work people 14 | it of 15 | out the 16 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/secretchamber/secretchamber-sample2.ans: -------------------------------------------------------------------------------- 1 | yes 2 | no 3 | yes 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/secretchamber/secretchamber-sample2.in: -------------------------------------------------------------------------------- 1 | 3 3 2 | a c 3 | b a 4 | a b 5 | aaa abc 6 | abc aaa 7 | acm bcm 8 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/sonofpipestream/sonofpipestream-sample1.ans: -------------------------------------------------------------------------------- 1 | 0.000000000 1.360000000 2 | 0.000000000 1.000000000 3 | 0.000000000 -1.000000000 4 | 0.000000000 0.360000000 5 | 0.880000000 0.000000000 6 | -0.880000000 -0.360000000 7 | 1.02037965897 8 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/sonofpipestream/sonofpipestream-sample1.in: -------------------------------------------------------------------------------- 1 | 6 6 3.0 0.66 2 | 2 4 8 3 | 4 6 1 4 | 3 6 1 5 | 4 5 5 6 | 1 5 7 7 | 3 5 3 8 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/sonofpipestream/sonofpipestream-sample2.ans: -------------------------------------------------------------------------------- 1 | 5 0 2 | 5 5 3 | 4.2 3.14159 4 | 4.2 3.14159 5 | -4.2 -3.14159 6 | 5 7 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/sonofpipestream/sonofpipestream-sample2.in: -------------------------------------------------------------------------------- 1 | 5 5 1.0 0.5 2 | 1 2 10 3 | 2 3 10 4 | 3 4 10 5 | 4 5 10 6 | 3 5 10 7 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/speed/speed-sample1.ans: -------------------------------------------------------------------------------- 1 | 3.000000000 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/speed/speed-sample1.in: -------------------------------------------------------------------------------- 1 | 3 5 2 | 4 -1 3 | 4 0 4 | 10 3 5 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/speed/speed-sample2.ans: -------------------------------------------------------------------------------- 1 | -0.508653377 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/speed/speed-sample2.in: -------------------------------------------------------------------------------- 1 | 4 10 2 | 5 3 3 | 2 2 4 | 3 6 5 | 3 1 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/tarotshamboast/tarotshamboast-sample1.ans: -------------------------------------------------------------------------------- 1 | PS 2 | PP 3 | RR 4 | SS 5 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/tarotshamboast/tarotshamboast-sample1.in: -------------------------------------------------------------------------------- 1 | 3 4 2 | PP 3 | RR 4 | PS 5 | SS 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/tarotshamboast/tarotshamboast-sample2.ans: -------------------------------------------------------------------------------- 1 | PRSPS 2 | PPSPP 3 | SSSSS 4 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/tarotshamboast/tarotshamboast-sample2.in: -------------------------------------------------------------------------------- 1 | 20 3 2 | PRSPS 3 | SSSSS 4 | PPSPP 5 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/visual/visual-sample1.ans: -------------------------------------------------------------------------------- 1 | 2 2 | 1 3 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/visual/visual-sample1.in: -------------------------------------------------------------------------------- 1 | 2 2 | 4 7 3 | 9 8 4 | 14 17 5 | 19 18 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/visual/visual-sample2.ans: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/visual/visual-sample2.in: -------------------------------------------------------------------------------- 1 | 2 2 | 4 7 3 | 14 17 4 | 9 8 5 | 19 18 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/visual/visual-sample3.ans: -------------------------------------------------------------------------------- 1 | syntax error 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/visual/visual-sample3.in: -------------------------------------------------------------------------------- 1 | 2 2 | 4 8 3 | 9 7 4 | 14 18 5 | 19 17 6 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/visual/visual-sample4.ans: -------------------------------------------------------------------------------- 1 | syntax error 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/samps/visual/visual-sample4.in: -------------------------------------------------------------------------------- 1 | 3 2 | 1 1 3 | 4 8 4 | 8 4 5 | 10 6 6 | 6 10 7 | 10 10 8 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/workspace/ACM/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ACM 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/workspace/ACM/Debug/ACM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/ICPC/2017 ICPC/workspace/ACM/Debug/ACM -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/workspace/ACM/Debug/C2.d: -------------------------------------------------------------------------------- 1 | C2.o: ../C2.cpp 2 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/workspace/ACM/Debug/C2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/ICPC/2017 ICPC/workspace/ACM/Debug/C2.o -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/workspace/ACM/Debug/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := 8 | 9 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/workspace/ACM/Debug/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | C_UPPER_SRCS := 6 | CXX_SRCS := 7 | C++_SRCS := 8 | OBJ_SRCS := 9 | CC_SRCS := 10 | ASM_SRCS := 11 | CPP_SRCS := 12 | C_SRCS := 13 | O_SRCS := 14 | S_UPPER_SRCS := 15 | CC_DEPS := 16 | C++_DEPS := 17 | EXECUTABLES := 18 | C_UPPER_DEPS := 19 | CXX_DEPS := 20 | OBJS := 21 | CPP_DEPS := 22 | C_DEPS := 23 | 24 | # Every subdirectory with source files must be described here 25 | SUBDIRS := \ 26 | . \ 27 | 28 | -------------------------------------------------------------------------------- /Solutions/ICPC/2017 ICPC/workspace/ACM/Debug/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | CPP_SRCS += \ 7 | ../C2.cpp 8 | 9 | OBJS += \ 10 | ./C2.o 11 | 12 | CPP_DEPS += \ 13 | ./C2.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | %.o: ../%.cpp 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: GCC C++ Compiler' 20 | g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /Solutions/Live Archive/2478 - Island Hopping.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/Live Archive/2478 - Island Hopping.cpp -------------------------------------------------------------------------------- /Solutions/Live Archive/3917 Tiling a Grid With Dominoes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100 3 | using namespace std; 4 | int memo[MAX]; 5 | inline int dp(int i){ 6 | int & ret = memo[i]; 7 | if (ret != -1) return ret; 8 | int ans = 4 * dp(i - 2) + dp(i - 1); 9 | for (int j = 3; j <= i; ++j) 10 | ans += 2*dp(i - j); 11 | for (int j = 4; j <= i; j += 2) 12 | ans += dp(i - j); 13 | // return ret = 4 * dp(i - 2) + 2*dp(i - 3) + dp(i - 1); 14 | // for (int j = 4; j <= i; ++j) ans += dp(i - j); 15 | return ret = ans; 16 | } 17 | inline void init(void){ 18 | memset(memo, 0xFF, sizeof memo); 19 | memo[0] = 1; 20 | memo[1] = 1; 21 | memo[2] = 5; 22 | memo[3] = 11; 23 | } 24 | int main(void){ 25 | init(); 26 | int TC; scanf("%d", &TC); 27 | for (int x, tc = 1; tc <= TC; ++tc){ 28 | scanf("%d", &x); 29 | printf("%d %d\n", tc, dp(x)); 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Solutions/Live Archive/4764 Bing it.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100100 3 | using namespace std; 4 | int last[MAX]; 5 | int memo[MAX]; 6 | int arr[MAX]; 7 | inline void init(void){ 8 | memset(last, 0xFF, sizeof last); 9 | } 10 | int n; 11 | inline void read(void){ 12 | for (int i = 0; i < n; ++i) 13 | scanf("%d", arr + i); 14 | } 15 | inline void solve(void){ 16 | memo[0] = 0; 17 | last[arr[0]] = 0; 18 | for (int i = 1; i < n; ++i){ 19 | int x = arr[i]; 20 | if (last[x] == -1) memo[i] = memo[i-1]; 21 | else memo[i] = max(memo[i-1], memo[last[x]] + (x == 999 ? 3 : 1)); 22 | last[x] = i; 23 | } 24 | } 25 | int main(void){ 26 | while(scanf("%d", &n), n){ 27 | read(); 28 | init(); 29 | solve(); 30 | printf("%d\n", memo[n-1]); 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Solutions/Live Archive/5789 Army Buddies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100010 3 | using namespace std; 4 | int nxt[MAX], p[MAX]; 5 | int s, b; 6 | inline void init(void){ 7 | for (int i = 1; i <= s; ++i) 8 | { 9 | nxt[i] = i + 1; 10 | p[i] = i - 1; 11 | } 12 | nxt[s] = 0; 13 | p[1] = 0; 14 | } 15 | int main(void){ 16 | while(scanf("%d%d", &s, &b), s || b){ 17 | init(); 18 | for(int x, y; b--;){ 19 | scanf("%d%d", &x, &y); 20 | int a = p[x]; 21 | int b = nxt[y]; 22 | nxt[a] = b; 23 | p[b] = a; 24 | if (a) printf("%d ", a); 25 | else printf("* "); 26 | if (b) printf("%d\n", b); 27 | else printf("*\n"); 28 | } 29 | printf("-\n"); 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Solutions/Live Archive/7150 Amalgamated Artichokes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define eps 1 3 | using namespace std; 4 | double p, a, b, c, d; 5 | int n; 6 | inline double price(const double k) 7 | { 8 | return p * (sin(a * k + b) + cos(c * k + d) + 2.0); 9 | } 10 | int main() 11 | { 12 | while(~scanf("%lf%lf%lf%lf%lf%d", &p, &a, &b, &c, &d, &n)) 13 | { 14 | double ans = 0.0; 15 | double min_so_far = 1e9; 16 | for (int i = n; i >= 1; --i) 17 | { 18 | const double cur_value = price(i); 19 | ans = max(ans, cur_value - min_so_far); 20 | min_so_far = min(min_so_far, cur_value); 21 | } 22 | 23 | printf("%.8lf\n", ans); 24 | } 25 | return 0; 26 | } 27 | /* 28 | 42 1 23 4 8 10 29 | 100 7 615 998 801 3 30 | 100 432 406 867 60 1000 31 | */ 32 | 33 | -------------------------------------------------------------------------------- /Solutions/Live Archive/7556 Jump on Buildings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 5010 3 | using namespace std; 4 | int n, h[MAX], r[MAX], l[MAX]; 5 | inline void read(void){ 6 | for (int i = 0; i < n; ++i) 7 | scanf("%d", h + i); 8 | } 9 | inline void solve(void){ 10 | r[0] = l[0] = 0; 11 | for (int i = 1; i < n; ++i){ 12 | r[i] = l[i] = 0; 13 | for (int j = i - 1; j >= 0; --j) 14 | if (h[i] > h[j]) 15 | l[i] = max(l[i], 1 + r[j]); 16 | else if (h[j] > h[i]) 17 | r[j] = max(r[j], 1 + l[i]); 18 | } 19 | } 20 | inline void print(void){ 21 | for (int i = 0; i < n; ++i) 22 | printf("%d ", max(l[i], r[i])); 23 | printf("\n"); 24 | } 25 | int main(void){ 26 | while(~scanf("%d", &n)){ 27 | read(); 28 | solve(); 29 | print(); 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Solutions/Live Archive/7560 Chota Mouse and his Birthday Cake.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 10100 3 | using namespace std; 4 | int p[MAX]; 5 | int f, h, w; 6 | int main(void){ 7 | int TC; scanf("%d", &TC); 8 | while(TC--){ 9 | scanf("%d%d%d", &f, &h, &w); 10 | for (int i = 0, x; i < f; ++i) 11 | scanf("%d", p + i); 12 | p[f++] = 0; p[f++] = w; 13 | sort(p, p + f); 14 | int ans = w; 15 | for (int i = 1; i < f; ++i) 16 | ans = min(ans, p[i] - p[i - 1]); 17 | printf("%d\n", ans * h); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/SPOJ/3 TRT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 2005 3 | using namespace std; 4 | int memo[MAX][MAX]; 5 | int arr[MAX]; 6 | int a, n; 7 | inline void init(void){ 8 | memset(memo, 0xFF, sizeof memo); 9 | } 10 | inline int dp(const int i, const int j){ 11 | if (j < i) return 0; 12 | int & ret = memo[i][j]; 13 | if (ret != -1) return ret; 14 | // len(i, j) == n -> pow = 1 15 | return ret = max((n - (j - i))*arr[i] + dp(i + 1, j), (n - (j - i))*arr[j] + dp(i, j - 1)); 16 | } 17 | inline void read(void){ 18 | scanf("%d", &n); 19 | for (int i = 0; i < n; ++i) 20 | scanf("%d", arr + i); 21 | } 22 | int main(void){ 23 | read(); 24 | init(); 25 | printf("%d\n", dp(0, n - 1)); 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Solutions/SPOJ/NOCHANGE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 100005 4 | 5 | using namespace std; 6 | int k[6]; int n; // coins and number of coins 7 | int memo[MAX]; // 1, 0, -1 8 | bool dp(int x){ 9 | if (x < 0) return false; 10 | if (x == 0) return true; 11 | if (memo[x] != char(0xFF)) return memo[x]; 12 | int y; 13 | int m = n; 14 | while (m){ 15 | y = x; 16 | for (int i = 0; i < m; i++) y -= k[i]; 17 | if (dp(y)) return memo[x] = true; 18 | m--; 19 | } 20 | return memo[x] = false; 21 | } 22 | 23 | int main(){ 24 | freopen("Input.txt", "r", stdin); 25 | freopen("Output.txt", "w", stdout); 26 | memset(memo, 0xFF, sizeof memo); // initialize all to -1 27 | int x; 28 | scanf("%d%d", &x, &n); 29 | for (int i = 0; i < n; i++) scanf("%d", &k[i]); 30 | if (dp(x)) printf("YES"); 31 | else printf("NO"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Solutions/SPOJ/NOVICE43.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int memo[13][13][13]; 7 | // this function returns number of strings of length l, using numbers <= i, not exceeding n 8 | int dp(int i, int n, int l){ // n is the maximum number I can use, i is the largest number I can use, l is the length 9 | if (memo[i][n][l] != -1) return memo[i][n][l]; 10 | if (l == 1) return memo[i][n][l] = i; 11 | if (i == n) return memo[i][n][l] = i + dp(i, n, l-1); 12 | return memo[i][n][l] = (i-1)*dp(i, n, l-1) + dp(i+1, n, l-1); 13 | } 14 | int main(){ 15 | freopen("Input.txt", "r", stdin); 16 | freopen("Output.txt", "w", stdout); 17 | int n; 18 | int TC; 19 | memset(memo, 0xFF, sizeof memo); 20 | scanf("%d", &TC); 21 | while (TC--){ 22 | scanf("%d", &n); 23 | printf("%d\n", dp(1, n, n)); 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Solutions/TJU/2862. CityStar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 100005 5 | using namespace std; 6 | int n; 7 | int a[MAX]; 8 | int ans_v, ans_i, ans_j; 9 | void solve(void){ 10 | sort(a, a + n); 11 | ans_v = INT_MAX; 12 | for (int i = 0, j = 4; j < n; ++i, ++j) 13 | if (ans_v > a[j] - a[i]) 14 | ans_v = a[ans_j = j] - a[ans_i = i]; 15 | } 16 | int main(void){ 17 | // freopen("Input.txt", "r", stdin); 18 | int TC; 19 | scanf("%d", &TC); 20 | for (int tc = 1; tc <= TC; ++tc){ 21 | printf("Scenario #%d:\n", tc); 22 | scanf("%d", &n); 23 | for (int i = 0; i < n; ++i) 24 | scanf("%d", a + i); 25 | solve(); 26 | printf("%d:", ans_v + 1); 27 | for (int i = ans_i; i <= ans_j; ++i) 28 | printf(" %d", a[i]); 29 | printf("\n\n"); 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Solutions/TJU/2867. Picking Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 10005 5 | using namespace std; 6 | short lis[MAX]; // minimum ending time of i games 7 | void init(void){ 8 | memset(lis, 0x3F, sizeof lis); 9 | } 10 | typedef pair ss; 11 | int n; 12 | ss a[MAX]; 13 | int main(void){ 14 | // freopen("Input.txt", "r", stdin); 15 | while(scanf("%d", &n), n){ 16 | init(); 17 | for (int i = 0, x, y; i < n; ++i) 18 | scanf("%d%d", &x, &y), a[i] = ss(x, x + y - 1); 19 | sort(a, a + n); 20 | int ans = 0; 21 | for (int i = 0; i < n; ++i) 22 | { 23 | int ind = lower_bound(lis, lis + n, a[i].first) - lis; 24 | lis[ind] = min(a[i].second, lis[ind]); 25 | ans = max(ans, ind); 26 | } 27 | printf("%d\n", ans + 1); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/00147.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int coin[10] = { 2000, 1000, 400, 200, 100, 40, 20, 10, 4, 2}; 6 | 7 | long long memo[10][6005]; 8 | long long dp(int i, int s){ 9 | if (s < 0) return 0; 10 | if (i == 10) return 1; 11 | if (memo[i][s] != -1) return memo[i][s]; 12 | return memo[i][s] = dp(i, s - coin[i]) + dp(i+1, s); 13 | } 14 | int main(){ 15 | double n; 16 | memset(memo, 0xFF, sizeof memo); 17 | while (scanf("%lf", &n), n != 0.0) printf("%6.2lf%17lld\n", n, dp(0, int(n*20.0 + 1e-5))); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Solutions/UVA/00264 Count on Cantor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAX 10000005 6 | using namespace std; 7 | typedef long long int ll; 8 | typedef pair ii; 9 | ii memo[MAX]; 10 | void init(void) { 11 | int counter = 1; 12 | int start = 1; 13 | for (int i = 1; counter < MAX; ++i) { 14 | if (start) 15 | for (int j = i; j >= 1 && counter < MAX; --j) 16 | memo[counter++] = ii(j, i - j + 1); 17 | else 18 | for (int j = 1; j <= i && counter < MAX; ++j) 19 | memo[counter++] = ii(j, i - j + 1); 20 | start ^= 1; 21 | } 22 | } 23 | int main(void) { 24 | // freopen("Input.txt", "r", stdin); 25 | init(); 26 | int x; 27 | while(scanf("%d", &x) != EOF) 28 | printf("TERM %d IS %d/%d\n", x, memo[x].first, memo[x].second); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Solutions/UVA/00325 Identifying Legal Pascal Real Constants.java: -------------------------------------------------------------------------------- 1 | import java.math.*; 2 | import java.util.*; 3 | import java.lang.*; 4 | public class Main 5 | { 6 | public static final String SignedInteger = "(\\+|-)?\\d+"; 7 | public static final String Fraction = SignedInteger + "\\.\\d+"; 8 | public static final String Exponential = "((" + SignedInteger + ")|(" + Fraction + "))(e|E)" + SignedInteger; 9 | 10 | public static void main(String[] args) 11 | { 12 | Scanner sc = new Scanner(System.in); 13 | while(true) 14 | { 15 | String s = sc.nextLine(); 16 | if (s.equals("*")) 17 | { 18 | break; 19 | } 20 | s = s.trim(); 21 | 22 | System.out.println(s + (s.matches(Fraction) || s.matches(Exponential) ? " is legal." : " is illegal.")); 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/00357 Let Me Count The Ways.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 30100 3 | using namespace std; 4 | typedef long long int ll; 5 | const ll val[4] = {50, 25, 10, 5}; 6 | ll memo[MAX][5]; 7 | inline ll dp(const int i, const int j) 8 | { 9 | ll & ret = memo[i][j]; 10 | if (ret != -1LL) 11 | { 12 | return ret; 13 | } 14 | if (i == 0 || j == 4) 15 | { 16 | return ret = 1; 17 | } 18 | return ret = dp(i, j + 1) + (i < val[j] ? 0 : dp(i - val[j], j)); 19 | } 20 | 21 | inline void test(void) 22 | { 23 | for (int i = 0; i <= 30000; i += 100) 24 | printf("%lld\n", dp(i, 0)); 25 | } 26 | 27 | int main() { 28 | memset(memo, -1, sizeof memo); 29 | // test(); 30 | for (int x; ~scanf("%d", &x);) 31 | { 32 | ll ans = dp(x, 0); 33 | if (ans == 1) 34 | printf("There is only 1 way to produce %d cents change.\n", x); 35 | else printf("There are %lld ways to produce %d cents change.\n", ans, x); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Solutions/UVA/00374 Big Mod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | ll raise(ll x, ll p, ll MOD){ // return x^p 5 | ll ans = 1; 6 | for (; p; x = (x * x)%MOD, p >>= 1) 7 | if (p & 1LL) ans = (ans*x)%MOD; 8 | return ans; 9 | } 10 | int main(void){ 11 | ll x, p, MOD; 12 | while(scanf("%lld%lld%lld", &x, &p, &MOD) != EOF) 13 | printf("%lld\n", raise(x, p, MOD)); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Solutions/UVA/00382.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define eps 1e-9 6 | using namespace std; 7 | int main(){ 8 | // freopen("Input.txt", "r", stdin); 9 | long long n; 10 | long long sum; 11 | printf("PERFECTION OUTPUT\n"); 12 | while(scanf("%lld", &n), n){ 13 | // N = 1 IS A SPECIAL CASE 14 | sum = 1; 15 | for (int i = 2; i <= (n >> 1); ++i) 16 | if (!(n%i)) 17 | sum += i; 18 | if(n == 1) sum = 0; 19 | printf("%5d %s\n", n, (sum == n)?"PERFECT":(sum < n)?"DEFICIENT":"ABUNDANT"); 20 | } 21 | printf("END OF OUTPUT\n"); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Solutions/UVA/00414.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | freopen("Input.txt", "r", stdin); 10 | freopen("Output.txt", "w", stdout); 11 | int x; 12 | string s; 13 | int space[12]; 14 | int sum; 15 | while (scanf("%d", &x), x){ 16 | sum = 0; 17 | for (int i = 0; i < x; i++){ 18 | cin >> s; 19 | if (space[i] = 25 - s.length()) { cin >> s; space[i] -= s.length(); }; 20 | sum += space[i]; 21 | } 22 | sort(space, space + x); 23 | sum -= (x*space[0]); 24 | printf("%d\n", sum); 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Solutions/UVA/00441.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | //freopen("Input.txt", "r", stdin); 8 | //freopen("Output.txt", "w", stdout); 9 | 10 | int k; 11 | int i; 12 | int x[12]; 13 | bool first = true; 14 | int counter; 15 | while (true){ 16 | scanf("%d", &k); 17 | if (k == 0) return 0; 18 | if (!first) printf("\n"); else first = false; 19 | int p[12] = { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 }; 20 | for (i = 0; i < k; i++) scanf("%d", &x[i]); 21 | do{ 22 | counter = 0; 23 | for (i = 0; i < k && counter < 5; i++) 24 | if (p[i]) printf("%d ", x[i]), counter++; 25 | while (!p[i]) i++; 26 | printf("%d\n", x[i]); 27 | } while (prev_permutation(p, p + k)); 28 | } 29 | } -------------------------------------------------------------------------------- /Solutions/UVA/00443 humble numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 2000000000LL 5 | using namespace std; 6 | typedef long long int ll; 7 | vector ans; 8 | void init(void){ 9 | ans.reserve(5843); 10 | for (ll i = 1; i <= MAX; i *= 2LL) 11 | for (ll j = i; j <= MAX; j *= 3LL) 12 | for (ll k = j; k <= MAX; k *= 5LL) 13 | for (ll l = k; l <= MAX; l *= 7LL) 14 | ans.push_back((int)l); 15 | sort(ans.begin(), ans.end()); 16 | } 17 | int main(void){ 18 | // freopen("Input.txt", "r", stdin); 19 | init(); 20 | int x; 21 | while(scanf("%d", &x), x) 22 | printf("The %d%s humble number is %d.\n", x, x % 100 >= 11 && x % 100 <= 13 ? "th" : x % 10 == 1 ? "st" : x % 10 == 2? "nd" : x % 10 == 3 ? "rd" : "th", ans[x - 1]); 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Solutions/UVA/00458 The Decoder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void) 4 | { 5 | string line; 6 | while(getline(cin, line)) 7 | { 8 | for (int i = 0; i < line.length(); ++i) 9 | line[i] = line[i] - 7; 10 | cout << line << '\n'; 11 | line = ""; 12 | } 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Solutions/UVA/00488 Triangle Wave.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void) { 4 | int TC; 5 | scanf("%d", &TC); 6 | for(int tc = 1; tc <= TC; ++tc) { 7 | int a, f; 8 | scanf("%d%d", &a, &f); 9 | for (int i = 1; i <= f; ++i) { 10 | for (int j = 1; j <= a; ++j) { 11 | for (int k = 0; k < j; ++k) 12 | printf("%d", j); 13 | printf("\n"); 14 | } 15 | for (int j = a - 1; j > 0; --j) { 16 | for (int k = 0; k < j; ++k) 17 | printf("%d", j); 18 | printf("\n"); 19 | } 20 | if (i != f || tc < TC) 21 | printf("\n"); 22 | } 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/00494 Kindergarten Counting Game.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/UVA/00494 Kindergarten Counting Game.cpp -------------------------------------------------------------------------------- /Solutions/UVA/00498 Polly the Polynomial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | vector v; 5 | inline ll horner(ll x){ 6 | ll ans = 0; 7 | for (int i = 0; i < v.size(); ++i) 8 | ans = ans*x + v[i]; 9 | return ans; 10 | } 11 | int main(void){ 12 | for(string s; getline(cin, s);){ 13 | v.clear(); 14 | for (char * c = strtok(const_cast (s.c_str()), " \n"); c; c = strtok(0, " \n")) 15 | v.push_back(atoi(c)); 16 | getline(cin, s); 17 | char * c = strtok(const_cast (s.c_str()), " \n"); 18 | printf("%lld", horner(atoi(c))); 19 | for (c = strtok(0, " \n"); c; c = strtok(0, " \n")) 20 | printf(" %lld", horner(atoi(c))); 21 | printf("\n"); 22 | } 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Solutions/UVA/00594 One Little, Three Endians.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int reverse(int x){ 5 | int temp = 0; 6 | for (int i = 0; i < 4; ++i){ 7 | temp <<= 8; 8 | temp |= x & 0xFF; 9 | x >>= 8; 10 | } 11 | return temp; 12 | } 13 | int main(void){ 14 | // freopen("Input.txt", "r", stdin); 15 | int x; 16 | while(scanf("%d", &x) != EOF) 17 | printf("%d converts to %d\n", x, reverse(x)); 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/UVA/00616.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int n; 6 | bool can(int m){ 7 | int num = n; 8 | for (int a = 0; a < m; ++a){ // temp men will wake up 9 | if (num % m != 1) return false; 10 | num -= (num/m + 1); 11 | } 12 | return !(num%m); 13 | } 14 | int main(){ 15 | // freopen("Input.txt", "r", stdin); 16 | while(scanf("%d", &n), n >= 0){ 17 | printf("%d coconuts, ", n); 18 | bool flag = false; 19 | for (int m = sqrt(n)+1; m >= 2; --m) 20 | if (can(m)) { 21 | printf("%d people and 1 monkey\n", m); 22 | flag = true; 23 | break; 24 | } 25 | if (!flag) printf("no solution\n"); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/UVA/00623 500!.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | import java.math.*; 4 | import java.lang.Integer; 5 | public class Main { 6 | static final int max = 1010; 7 | static BigInteger memo[] = new BigInteger[max]; 8 | static void init(){ 9 | memo[0] = BigInteger.ONE; 10 | for (int i = 1; i <= 1000; ++i) 11 | memo[i] = memo[i-1].multiply(BigInteger.valueOf(i)); 12 | } 13 | static int counter[] = new int[10]; 14 | public static void main(String[] args) { 15 | init(); 16 | Scanner sc = new Scanner(System.in); 17 | while(sc.hasNext()){ 18 | int x = sc.nextInt(); 19 | System.out.print(x + "!\n" + memo[x] + "\n"); 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Solutions/UVA/00674.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int coin[4] = { 50, 25, 10, 5}; // the fifth coin is 1 6 | int memo[4][7490]; 7 | int change(int i, int v){ // starting at coin i, with value v 8 | if (i == 4 || v == 0) return 1; 9 | if (memo[i][v] != -1) return memo[i][v]; 10 | if (coin[i] > v) return memo[i][v] = change(i+1, v); 11 | return memo[i][v] = change(i, v-coin[i]) + change(i + 1, v); 12 | // the number of ways of representing v - coin[i] using coin i, added to the number of ways of representing v ignoring coin[i] 13 | } 14 | 15 | int main(){ 16 | freopen("Input.txt", "r", stdin); 17 | freopen("Output.txt", "w", stdout); 18 | int v; 19 | memset(memo, 0xFF, sizeof memo); 20 | while (scanf("%d", &v) != EOF) 21 | printf("%d\n", change(0, v)); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Solutions/UVA/00686 Goldbach’s Conjecture (II).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX (1 << 15) 3 | using namespace std; 4 | int memo[MAX] = { 0 }; 5 | vector primes; 6 | inline void sieve(void) 7 | { 8 | primes.reserve(MAX / log(MAX)); 9 | bitset bs; 10 | bs.set(); 11 | for (int i = 2; i <= MAX; ++i) 12 | { 13 | if (bs[i]) 14 | { 15 | primes.push_back(i); 16 | for (int j = i * i; j <= MAX; j += i) 17 | { 18 | bs[j] = 0; 19 | } 20 | } 21 | } 22 | } 23 | inline void init_memo(void) 24 | { 25 | for (int i = 0; i < primes.size(); ++i) 26 | { 27 | const int p = primes[i]; 28 | for (int j = i; j < primes.size(); ++j) 29 | { 30 | const int sum = p + primes[j]; 31 | if (sum >= MAX) 32 | { 33 | break; 34 | } 35 | ++memo[sum]; 36 | } 37 | } 38 | } 39 | int main(void) 40 | { 41 | sieve(); 42 | init_memo(); 43 | for (int x; scanf("%d", &x), x; printf("%d\n", memo[x])); 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Solutions/UVA/00696 How Many Knights.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int m, n; 4 | int main(void) 5 | { 6 | #ifndef ONLINE_JUDGE 7 | freopen("input.in", "r", stdin); 8 | #endif 9 | while(scanf("%d%d", &m, &n), m || n) 10 | { 11 | const int area = n * m; 12 | int ans = (area >> 1) + (area & 1); 13 | int h = min(n, m), w = max(n, m); 14 | // n <= m 15 | if (h == 1) 16 | ans = w; 17 | if (h == 2) 18 | ans = max(ans, ((w / 4) * 2 + min(w & 3, 2)) * 2); 19 | printf("%d knights may be placed on a %d row %d column board.\n", ans, m, n); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Solutions/UVA/00927.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | freopen("Input.txt", "r", stdin); 8 | freopen("Output.txt", "w", stdout); 9 | 10 | int TC, degree, c[21], d, k; 11 | long sum, power; 12 | int i; 13 | scanf("%d", &TC); 14 | while (TC--){ 15 | scanf("%d", °ree); 16 | for (i = 0; i <= degree; i++) 17 | scanf("%d", &c[i]); 18 | scanf("%d%d", &d, &k); 19 | i = 1; 20 | while ((i*(i + 1)*d) / 2 < k) 21 | i++; 22 | sum = 0; 23 | for (int l = 0; l <= degree; l++){ 24 | power = 1; 25 | for (int j = 0; j < l; j++) 26 | power *= i; 27 | sum += long(c[l])*power; 28 | } 29 | cout << sum << '\n'; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solutions/UVA/00988.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | vectorcounter; 5 | vectordead; 6 | int n; 7 | int main(void){ 8 | // freopen("Input.txt", "r", stdin); 9 | bool first = true; 10 | while(scanf("%d", &n) != EOF){ 11 | if (!first) printf("\n"); 12 | first = false; 13 | counter.assign(n, 0); 14 | dead.assign(n, false); 15 | counter[0] = 1; 16 | for (int i = 0; i < n; ++i){ 17 | int cur_count = counter[i]; 18 | int k, x; scanf("%d", &k); 19 | if (!k)dead[i] = true; 20 | else while(k--) 21 | scanf("%d", &x), counter[x] += cur_count; 22 | } 23 | int ans = 0; 24 | for (int i = 0; i < n; ++i) 25 | if (dead[i]) 26 | ans += counter[i]; 27 | printf("%d\n", ans); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/00991 Sate Salutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 13 3 | using namespace std; 4 | typedef long long int ll; 5 | ll memo[MAX]; 6 | void init(void){ 7 | memo[0] = 1LL; 8 | for (int i = 1; i <= 10; ++i) 9 | memo[i] = (2*(2*i - 1)*memo[i - 1])/(i + 1); 10 | } 11 | int main(void){ 12 | // freopen("Input.txt", "r", stdin); 13 | init(); 14 | int n; 15 | bool first = true; 16 | while(scanf("%d", &n) != EOF){ 17 | if(!first) printf("\n"); 18 | first = false; 19 | printf("%d\n", memo[n]); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/01121 Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n, s; 4 | int arr[100100]; 5 | inline int solve(void){ 6 | bool flag = false; 7 | int i = 0, j = 0, cur = 0; 8 | int ans = 1000000; 9 | while(j < n || cur >= s) 10 | { 11 | while(j < n && cur < s) 12 | cur += arr[j++]; 13 | if (cur >= s){ 14 | flag = true; 15 | ans = min(ans, j - i); 16 | cur -= arr[i++]; 17 | } 18 | } 19 | return flag ? ans : 0; 20 | } 21 | int main(void){ 22 | while(~scanf("%d%d", &n, &s)){ 23 | for (int i = 0; i < n; ++i) 24 | scanf("%d", arr + i); 25 | printf("%d\n", solve()); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Solutions/UVA/01203 Argus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | struct inst{ 6 | int t; // time of next arrival 7 | int i; // ID 8 | int p; // period 9 | }; 10 | inline bool operator < (const inst & a, const inst & b){ 11 | return (a.t != b.t) ? a.t < b.t : a.i < b.i; 12 | } 13 | char s[20]; 14 | int main(){ 15 | // freopen("Input.txt", "r", stdin); 16 | priority_queueq; 17 | while(scanf("%s", s), strcmp(s, "#")){ 18 | int x, y; scanf("%d%d", &x, &y); 19 | q.push(inst{-y, -x, -y}); 20 | } 21 | int k; 22 | scanf("%d", &k); 23 | while(k--){ 24 | inst temp = q.top(); 25 | q.pop(); 26 | printf("%d\n", -temp.i); 27 | q.push(inst{temp.t + temp.p, temp.i, temp.p}); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/01225.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int num[10]; 5 | inline void solve(int i){ 6 | while(i){ 7 | ++num[i%10]; 8 | i/=10; 9 | } 10 | } 11 | int main(){ 12 | // freopen("Input.txt", "r", stdin); 13 | int TC; 14 | scanf("%d", &TC); 15 | while(TC--){ 16 | int n; 17 | scanf("%d", &n); 18 | memset(num ,0, sizeof num); 19 | for (int i = 1; i <= n; ++i) solve(i); 20 | printf("%d", num[0]); 21 | for (int i = 1; i < 10; ++i) printf(" %d", num[i]); 22 | printf("\n"); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Solutions/UVA/01230 MODEX.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | import java.math.*; 4 | import java.lang.Integer; 5 | public class Main { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int TC = sc.nextInt(); 9 | while(TC-- > 0){ 10 | BigInteger x = sc.nextBigInteger(); 11 | BigInteger y = sc.nextBigInteger(); 12 | BigInteger n = sc.nextBigInteger(); 13 | System.out.println(x.modPow(y, n)); 14 | } 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Solutions/UVA/10007 Count the Trees.java: -------------------------------------------------------------------------------- 1 | import java.math.*; 2 | import java.util.*; 3 | import java.lang.*; 4 | public class Main { 5 | static BigInteger fact(int i){ 6 | return (i == 1) ? BigInteger.ONE 7 | : BigInteger.valueOf(i).multiply(fact(i-1)); 8 | } 9 | static BigInteger cat(int i){ 10 | return (i == 0) ? BigInteger.ONE : 11 | (BigInteger.valueOf(2*i*(2*i - 1)).multiply(cat(i - 1))).divide(BigInteger.valueOf(i*(i + 1))); 12 | } 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | while(true){ 16 | int n = sc.nextInt(); 17 | if (n == 0) break; 18 | System.out.println(cat(n).multiply(fact(n))); 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/10015 Joseph's Cousin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | vectorprimes; 5 | inline void sieve(void) 6 | { 7 | const ll upper_bound = 50000; 8 | bitset bs; 9 | bs.set(); 10 | bs[0] = bs[1] = 0; 11 | for (ll i = 2; i <= upper_bound; ++i) 12 | if (bs[i]) 13 | { 14 | primes.push_back(i); 15 | for (ll j = i * i; j <= upper_bound; j += i) 16 | bs[j] = 0; 17 | } 18 | } 19 | inline ll jo(const ll x, const int i) 20 | { 21 | return (x == 1LL) ? 0LL : (jo(x - 1, i + 1) + primes[i]) % x; 22 | } 23 | int main() { 24 | sieve(); 25 | for (ll x; scanf("%lld", &x), x; ) 26 | { 27 | printf("%lld\n", jo(x, 0) + 1LL); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solutions/UVA/10056 What is the Probability.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define eps 1e-5 3 | using namespace std; 4 | int main(void) 5 | { 6 | int n, i; double p; 7 | int TC; 8 | scanf("%d", &TC); 9 | while(TC--) 10 | { 11 | scanf("%d%lf%d", &n, &p, &i); 12 | if (p == 1.0) 13 | { 14 | printf("%.4lf\n", i == 1 ? 1.0 : 0.0); 15 | continue; 16 | } 17 | if (p == 0.0) 18 | { 19 | printf("%.4lf\n", 0.0); 20 | continue; 21 | } 22 | double q = 1.0 - p; 23 | double sum = 1.0; 24 | for (int i = 1;; ++i) 25 | { 26 | const double cur = pow(q, i * n); 27 | sum += cur; 28 | if (cur < eps) 29 | { 30 | break; 31 | } 32 | } 33 | sum *= pow(q, i - 1); 34 | printf("%.4lf\n", sum * p); 35 | } 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Solutions/UVA/10079 Pizza Cutting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | int main(void) 5 | { 6 | for (int n; scanf("%d", &n), n >= 0; ) 7 | { 8 | printf("%lld\n", (ll) (n * (n + 1LL) >> 1LL) + 1LL); 9 | } 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Solutions/UVA/10081 Tight Words.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | long double memo[105][10]; 4 | int n, k; 5 | inline void init_memo(void){ 6 | for (int i = 0; i < 105; ++i) 7 | for (int j = 0; j < 10; ++j) 8 | memo[i][j] = (i) ? -1.0 : 1.0; 9 | } 10 | inline long double dp(const int i, const int j){ 11 | long double & ret = memo[i][j]; 12 | if (ret != -1.0) return ret; 13 | long double ans = 0.0; 14 | for (int t = 0; t < k; ++t) 15 | if (abs(j - t) <= 1) 16 | ans += dp(i - 1, t); 17 | return ret = (long double) ans / k; 18 | } 19 | int main(void){ 20 | while(~scanf("%d%d", &k, &n)){ 21 | ++k; 22 | init_memo(); 23 | long double ans = 0.0; 24 | for (int i = 0; i < k; ++i) 25 | ans += dp(n-1, i); 26 | ans /= (long double) k; 27 | printf("%.5llf\n", (long double)ans * 100.0); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/10104 Euclid Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | ll x, y, d; 5 | void euclid(ll a, ll b){ 6 | if (b){ 7 | euclid(b, a %b); 8 | ll x1 = y; 9 | ll y1 = x - (a/b)*y; 10 | x = x1; y = y1; 11 | } 12 | else { 13 | d = a; 14 | x = 1; 15 | y = 0; 16 | } 17 | } 18 | int main(void){ 19 | for (int a, b;~scanf("%d%d", &a, &b);) 20 | { 21 | euclid(a, b); 22 | printf("%d %d %d\n", x, y, d); 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/10161 Ant on a Chessboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 45000 3 | using namespace std; 4 | typedef long long int ll; 5 | int a[N]; 6 | inline void init(void){ 7 | for (int i = 1; i < N; ++i) 8 | a[i] = i*i; 9 | } 10 | int main() { 11 | init(); 12 | for (int n; scanf("%d", &n), n;){ 13 | int i = lower_bound(a + 1, a + N, n) - a; 14 | int x, y; 15 | if (a[i] == n) { 16 | x = 1; y = i; 17 | } 18 | else 19 | { 20 | x = i; 21 | y = n - a[i - 1]; 22 | if (y > i){ 23 | x -= (y - i); 24 | y = i; 25 | } 26 | } 27 | if (!(i & 1)) swap(x, y); 28 | printf("%d %d\n", x, y); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Solutions/UVA/10165 Stone Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void){ 4 | for (int n; scanf("%d", &n), n; ){ 5 | int ans = 0; 6 | for(int x; n--;){ 7 | scanf("%d", &x); 8 | ans ^= x; 9 | } 10 | printf("%s\n", ans ? "Yes" : "No"); 11 | } 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Solutions/UVA/10176 Ocean Deep! - Make it shallow!!.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define code 131071 3 | using namespace std; 4 | char s[100000]; 5 | int main(void){ 6 | while(scanf("%s", s) != EOF){ 7 | int len = strlen(s); 8 | while(s[len - 1] != '#') 9 | scanf("%s", s + len), len = strlen(s); 10 | int rem = 0; 11 | for (int pow = 1, i = len - 2; i >= 0; --i){ 12 | if (s[i] == '1') 13 | rem = (rem + pow) % code; 14 | pow = (pow << 1)%code; 15 | } 16 | printf("%s\n", rem ? "NO" : "YES"); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/UVA/10179 Irreducible Basic Functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vectorprimes; 4 | inline void sieve(void){ 5 | const int upper_bound = (1 << 15); 6 | bitsetbs; 7 | bs.set(); 8 | bs[0] = bs[1] = 0; 9 | for (int i = 2; i <= upper_bound; ++i) 10 | if (bs[i]){ 11 | primes.push_back(i); 12 | for (int j = i*i; j <= upper_bound && j > 0; j += i) 13 | bs[j] = 0; 14 | } 15 | } 16 | int phi(int x){ 17 | int ans = x; 18 | for (int i = 0, p = 2; p*p <= x; p = primes[++i]) 19 | if (x % p == 0){ 20 | ans -= ans /p; 21 | while(x % p == 0) x /= p; 22 | } 23 | if (x != 1) ans -= ans /x; 24 | return ans; 25 | } 26 | int main(void){ 27 | sieve(); 28 | for (int x; scanf("%d", &x), x; printf("%d\n", phi(x))); 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/10192 Vacation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 150 3 | using namespace std; 4 | char a[150], b[150]; 5 | int n, m; 6 | int memo[150][150]; 7 | inline int dp(int i, int j){ 8 | int & ret = memo[i][j]; 9 | if (ret != -1) return ret; 10 | if (i == n || j == m) return ret = 0; 11 | if (a[i] == b[j]) // you can visit this city! 12 | return ret = 1 + dp(i + 1, j + 1); 13 | return ret = max(dp(i + 1, j), dp(i, j + 1)); 14 | } 15 | inline void init(void){ 16 | memset(memo, 0xFF, sizeof memo); 17 | } 18 | inline bool read(char * s, int & len){ 19 | fgets(s, INT_MAX, stdin); 20 | len = strlen(s); 21 | s[--len] = 0; 22 | return strcmp(s, "#"); 23 | } 24 | inline bool read(void){ 25 | if (!read(a, n)) return false; 26 | read(b, m); 27 | return true; 28 | } 29 | int main(void){ 30 | for(int tc = 1; read(); ++tc) 31 | init(), 32 | printf("Case #%d: you can visit at most %d cities.\n", tc, dp(0, 0)); 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Solutions/UVA/10219 Find the Ways!.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | typedef double ld; 5 | int n, k; 6 | int solve(void){ 7 | k = std::max(k, n - k); 8 | ld ans = 0.0; 9 | for (int i = k + 1; i <= n; ++i) 10 | ans += log10((ld)i); 11 | for (int i = 2; i <= n - k; ++i) 12 | ans -= log10((ld)i); 13 | return (int) floor(1 + ans); 14 | } 15 | int main(void){ 16 | // freopen("Input.txt", "r", stdin); 17 | while(scanf("%d%d", &n, &k) != EOF) 18 | printf("%d\n", solve()); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/UVA/10252 Common Permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1010 3 | using namespace std; 4 | int counter_a[128], counter_b[128]; 5 | char a[MAX], b[MAX]; 6 | inline void count_freq(char * s, int * x){ 7 | for (;*s != '\n'; ++s) 8 | ++x[*s]; 9 | } 10 | int main(void){ 11 | // freopen("Input.txt", "r", stdin); 12 | while(1) 13 | { 14 | *a = *b = 0; 15 | fgets(a, INT_MAX, stdin); 16 | fgets(b, INT_MAX, stdin); 17 | if (*a == 0 || *b == 0) break; 18 | memset(counter_a, 0, sizeof counter_a); 19 | memset(counter_b, 0, sizeof counter_b); 20 | count_freq(a, counter_a); 21 | count_freq(b, counter_b); 22 | for (char i = 'a'; i <= 'z'; ++i) 23 | { 24 | int x = min(counter_a[i], counter_b[i]); 25 | while(x--) printf("%c", i); 26 | } 27 | printf("\n"); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/10268 498-bis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int x; 4 | vector c; // my coefficients 5 | const char space[7] = " \n\t\v\f\r"; 6 | inline int horner(void){ 7 | int ans = 0; 8 | for (unsigned int i = 0; i < c.size(); ++i) 9 | ans = ans*x + c[i]; 10 | return ans; 11 | } 12 | inline bool read(void){ 13 | c.clear(); 14 | if (scanf("%d", &x) == EOF) return false; 15 | string line; 16 | getline(cin, line); 17 | getline(cin, line); 18 | for (char * s = strtok(const_cast(line.c_str()), space); s; s = strtok(0, space)) 19 | c.push_back(atoi(s)); 20 | c.pop_back(); 21 | const int n = c.size(); 22 | for (int i = 0; i < n; ++i) 23 | c[i] *= (n - i); 24 | return true; 25 | } 26 | int main(void){ 27 | while(read()) 28 | printf("%d\n", horner()); 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/10299 - Relatives.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 40000 3 | using namespace std; 4 | typedef long long ll; 5 | vector primes; 6 | inline void sieve() 7 | { 8 | bitset bs; 9 | bs.set(); bs[0] = bs[1] = 0; 10 | for (int i = 2; i < MAX; ++i) 11 | if (bs[i]) 12 | { 13 | primes.push_back(i); 14 | if (1LL * i * i < MAX) 15 | for (int j = i * i; j < MAX; j += i) 16 | bs[i] = 0; 17 | } 18 | } 19 | inline int euler_phi(int x) 20 | { 21 | int ans = x; 22 | for (int i = 0, p = primes[0]; p * p <= x; p = primes[++i]) 23 | if (x % p == 0) 24 | { 25 | ans -= ans / p; 26 | while (x % p == 0) x /= p; 27 | } 28 | if (x != 1) ans -= ans/x; 29 | return ans; 30 | } 31 | int main() 32 | { 33 | sieve(); 34 | for (int n; scanf("%d", &n), n; ) 35 | { 36 | printf("%d\n", n == 1 ? 0 : euler_phi(n)); 37 | } 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Solutions/UVA/10312 Expression Bracketing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 30 3 | using namespace std; 4 | typedef long long int ll; 5 | ll cat_memo[MAX]; 6 | ll sup_cat_memo[MAX]; 7 | inline void init(void){ 8 | cat_memo[0] = 1; 9 | sup_cat_memo[1] = sup_cat_memo[2] = 1; 10 | for (int i = 1; i < MAX; ++i) 11 | cat_memo[i] = 2*(2*i - 1)*cat_memo[i-1]/(i + 1); 12 | for (int i = 3; i < MAX; ++i) 13 | sup_cat_memo[i] = ((6*i - 9)*sup_cat_memo[i-1] + (3 - i)*sup_cat_memo[i-2])/i; 14 | } 15 | int main(void){ 16 | init(); 17 | for (int n; scanf("%d", &n) != EOF; printf("%lld\n", sup_cat_memo[n] - cat_memo[n - 1])); 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/UVA/10334 Ray Through Glasses.java: -------------------------------------------------------------------------------- 1 | import java.math.*; 2 | import java.util.*; 3 | import java.lang.*; 4 | public class Main { 5 | static final int MAX = 1005; 6 | static BigInteger memo[]; 7 | public static void init(){ 8 | memo = new BigInteger[MAX]; 9 | memo[0] = BigInteger.ONE; 10 | memo[1] = BigInteger.valueOf(2); 11 | for (int i = 2; i < MAX; ++i) 12 | memo[i] = memo[i-1].add(memo[i-2]); 13 | } 14 | public static void main(String[] args) { 15 | init(); 16 | Scanner sc = new Scanner(System.in); 17 | while(sc.hasNext()) 18 | System.out.println(memo[sc.nextInt()]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/UVA/10338 Mischievous Children.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | char s[1 << 5]; 4 | int counter[1 << 5]; 5 | typedef long long int ll; 6 | ll fact[1 << 5]; 7 | inline void init(void){ 8 | fact[0] = 1; 9 | for (int i = 1; i < 32; ++i) 10 | fact[i] = i * fact[i-1]; 11 | } 12 | int main(void){ 13 | // freopen("Input.txt", "r", stdin); 14 | init(); 15 | int TC; scanf("%d", &TC); 16 | for (int tc = 1; tc <= TC; ++tc){ 17 | memset(counter, 0, sizeof counter); 18 | scanf("%s", s); 19 | int n; 20 | for (n = 0; s[n]; ++n) 21 | ++counter[s[n] - 'A']; 22 | ll ans = fact[n]; 23 | for (int i =0; i < 26; ++i) 24 | ans /= fact[counter[i]]; 25 | printf("Data set %d: %lld\n", tc, ans); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/UVA/10346.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 152 5 | using namespace std; 6 | int n, k; 7 | int main(){ 8 | // freopen("Input.txt", "r", stdin); 9 | while(scanf("%d%d", &n ,&k) != EOF){ 10 | int ans = 0; 11 | int rem = 0; 12 | while(n){ 13 | ans += n; 14 | n += rem; 15 | rem = n%k; 16 | n/= k; 17 | } 18 | printf("%d\n", ans); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/10359 Tiling.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.math.*; 3 | 4 | public class Main 5 | { 6 | public static final int MAX = 256; 7 | public static BigInteger memo[] = new BigInteger[MAX]; 8 | public static void init() 9 | { 10 | memo[0] = memo[1] = BigInteger.ONE; 11 | for (int i = 2; i < MAX; ++i) 12 | memo[i] = memo[i - 1].add(memo[i - 2]).add(memo[i - 2]); 13 | } 14 | public static void main(String[] args) 15 | { 16 | init(); 17 | Scanner sc = new Scanner(System.in); 18 | while(sc.hasNext()) 19 | System.out.println(memo[sc.nextInt()]); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/10360.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define N 100 5 | using namespace std; 6 | int main(){ 7 | freopen("Input.txt", "r", stdin); 8 | freopen("Output.txt", "w", stdout); 9 | int TC, d, n, x, y, size, Max; 10 | int kill[N][N]; 11 | 12 | scanf("%d", &TC); 13 | while (TC--){ 14 | memset(kill, 0, sizeof kill); 15 | scanf("%d%d", &d, &n); 16 | while (n--){ 17 | scanf("%d %d %d", &x, &y, &size); 18 | for (int i = max(x - d, 0); i <= min(x + d, N-1); i++) 19 | for (int j = max(y - d, 0); j <= min(y + d, N-1); j++) 20 | kill[i][j] += size; 21 | } 22 | x = 0; y = 0; Max = kill[0][0]; 23 | for (int i = 0; i < N; i++) 24 | for (int j = 0; j < N; j++) 25 | if (kill[i][j] > Max){ 26 | x = i; y = j; Max = kill[x][y]; 27 | } 28 | printf("%d %d %d\n", x, y, Max); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Solutions/UVA/10368 Euclid's Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | inline bool win(int a, int b){ 4 | if (!a || !b) return false; 5 | if (a < b) swap(a, b); 6 | // a should be greater than or equal to b 7 | for (int c = a % b; c < a; c += b) 8 | if (!win(c, b)) return true; 9 | return false; 10 | } 11 | int main(void){ 12 | for (int x, y; scanf("%d%d", &x, &y), x || y; ) 13 | printf("%s wins\n", win(x, y) ? "Stan" : "Ollie"); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Solutions/UVA/10394 Twin Primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 18500000 5 | using namespace std; 6 | typedef pair ii; 7 | typedef long long int ll; 8 | ii ans[100005]; 9 | int counter = 0; 10 | void sieve(void){ 11 | bitsetbs; 12 | bs.set(); 13 | bs[0] = bs[1] = 0; 14 | for (int i = 2; ; ++i) 15 | if (bs[i]) 16 | { 17 | if (bs[i-2]) 18 | { 19 | ans[counter++] = ii(i-2, i); 20 | if (counter > 100000) break; 21 | } 22 | if ((ll) i * i <= MAX) 23 | for (int j = i * i; j <= MAX; j += i) 24 | bs[j] = 0; 25 | } 26 | } 27 | int main(void){ 28 | sieve(); 29 | // freopen("Input.txt", "r", stdin); 30 | ii temp; 31 | for (int x; scanf("%d", &x) != EOF;) 32 | temp = ans[x - 1], printf("(%d, %d)\n", temp.first, temp.second); 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Solutions/UVA/10404 Bachet's Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1000100 3 | using namespace std; 4 | char memo[MAX]; 5 | int m; 6 | int arr[10]; 7 | inline char win(const int i){ 8 | if (i < 0) return 1; 9 | char & ret = memo[i]; 10 | if (ret != -1) return ret; 11 | for (int j = m-1; j >= 0; --j) 12 | if (!win(i - arr[j])) return ret = 1; 13 | return ret = 0; 14 | } 15 | inline void init_memo(void){ 16 | memset(memo, 0xFF, sizeof memo); 17 | memo[0] = 0; 18 | for (int i = 0; i < m; ++i) 19 | memo[arr[i]] = 1; 20 | } 21 | int main(void){ 22 | for(int n; ~scanf("%d%d", &n, &m);){ 23 | for (int i = 0; i < m; ++i) scanf("%d", arr + i); 24 | sort(arr, arr + m); 25 | init_memo(); 26 | printf("%s wins\n", win(n) ? "Stan" : "Ollie"); 27 | } 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Solutions/UVA/10407 Simple Division.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 1005 5 | using namespace std; 6 | inline int gcd(int a, int b){ 7 | return !b ? a : gcd(b, a % b); 8 | } 9 | int counter; 10 | int a[MAX]; 11 | int solve(){ 12 | int ans = a[0] - a[1]; 13 | for (int i = 0; i < counter; ++i) 14 | for (int j = i + 1; j < counter; ++j) 15 | ans = gcd(ans, abs(a[i] - a[j])); 16 | return ans; 17 | } 18 | int main(void){ 19 | // freopen("Input.txt", "r", stdin); 20 | int x; 21 | while(scanf("%d", &x), x){ 22 | a[0] = x; 23 | counter = 1; 24 | while(scanf("%d", &x), x) 25 | a[counter++] = x; 26 | printf("%d\n", solve()); 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/UVA/10489 - Boxes of Chocolates.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n, b; 4 | int main() 5 | { 6 | int TC; scanf("%d", &TC); 7 | while(TC--) 8 | { 9 | scanf("%d%d", &n, &b); 10 | int ans = 0; 11 | for(int k; b--; ) 12 | { 13 | int cur = 1; 14 | scanf("%d", &k); 15 | for (int a; k--; ) 16 | { 17 | scanf("%d", &a); 18 | cur *= a, cur %= n; 19 | } 20 | ans += cur, ans %= n; 21 | } 22 | printf("%d\n", ans); 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/10491 Cows and Cars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n_cows, n_cars, n_show; 4 | double p1(void) { // probability of choosing a cow first then switching to a car 5 | int n_total = n_cows + n_cars; 6 | double q = (double)n_cows / n_total; // choose a cow first 7 | int n_after_open = n_total - 1 - n_show; 8 | double r = (double)n_cars/n_after_open; // choose a car second 9 | return r*q; 10 | } 11 | double p2(void) { // probability of choosing a car first then switching to a car again 12 | int n_total = n_cows + n_cars; 13 | double q = (double)n_cars / n_total; 14 | int n_after_open = n_total - 1 - n_show; 15 | int c_after_open = n_cars - 1; 16 | double r = (double) c_after_open/n_after_open; 17 | return r*q; 18 | } 19 | int main(void) { 20 | while (~scanf("%d%d%d", &n_cows, &n_cars, &n_show)) 21 | printf("%1.5lf\n", p1() + p2()); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Solutions/UVA/10526 Very Easy !!!.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | import java.math.*; 4 | import java.lang.Integer; 5 | public class Main { 6 | static public BigInteger compute(BigInteger a, int n){ 7 | BigInteger ans = BigInteger.ZERO; 8 | BigInteger power = a; 9 | for (int i = 1; i <= n; ++i){ 10 | ans = ans.add(BigInteger.valueOf(i).multiply(power)); 11 | power = power.multiply(a); 12 | } 13 | return ans; 14 | } 15 | public static void main(String[] args) { 16 | Scanner sc = new Scanner(System.in); 17 | while(sc.hasNext()){ 18 | int n = sc.nextInt(), a = sc.nextInt(); 19 | System.out.println(compute(BigInteger.valueOf(a), n)); 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Solutions/UVA/10539 Almost Prime Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | vectorprimes; 5 | inline void sieve(void){ 6 | bitset<1000100> bs; 7 | bs.set(); 8 | for (ll i = 2; i < 1000000LL; ++i) 9 | if (bs[i]){ 10 | primes.push_back(i); 11 | for (ll j = i*i; j < 1000000LL; j += i) 12 | bs[j] = 0; 13 | } 14 | } 15 | int main(void){ 16 | sieve(); 17 | int TC; scanf("%d", &TC); 18 | for(ll a, b; TC--;){ 19 | scanf("%lld%lld", &a, &b); 20 | ll answer = 0LL; 21 | for (ll i = 0, p = 2; p*p <= b; p = primes[++i]) 22 | { 23 | ll x = p*p; 24 | while(x < a) x *= p; 25 | while(x <= b){ 26 | ++answer; 27 | x *= p; 28 | } 29 | } 30 | printf("%lld\n", answer); 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Solutions/UVA/10551 Basic Remains.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | import java.math.*; 4 | import java.lang.Integer; 5 | public class Main { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | while(true){ 9 | int b = sc.nextInt(); 10 | if (b == 0) break; 11 | BigInteger p = sc.nextBigInteger(b); 12 | BigInteger m = sc.nextBigInteger(b); 13 | System.out.println(p.mod(m).toString(b)); 14 | } 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Solutions/UVA/10581.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 85 4 | using namespace std; 5 | char s[8][MAX]; 6 | char ans[MAX]; 7 | int main(){ 8 | // freopen("Input.txt", "r", stdin); 9 | int TC; 10 | scanf("%d", &TC); 11 | char c; 12 | scanf("%c", &c); // ignore \n 13 | while(TC--){ 14 | scanf("%s", s[0]); // ignore first line 15 | for (int i = 0; i < 8; ++i) scanf("%s", s[i]); 16 | int len = strlen(s[0]) - 2; 17 | memset(ans, 0, sizeof ans); 18 | for (int i = 0; i < 8; ++i) 19 | for (int j = 1; j <= len; ++j) 20 | ans[j-1] |= (int(s[i][j] == '\\')) << i; // char j-1 21 | printf("%s\n", ans); 22 | scanf("%s", s[0]); // ignore last line 23 | scanf("%c%c", &c, &c); // ignore 2 \n 24 | } 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Solutions/UVA/10586 Polynomial Remains.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 10010 3 | using namespace std; 4 | int n, k; 5 | int c[MAX]; 6 | inline void read(void){ 7 | for (int i = 0; i <= n; ++i) 8 | scanf("%d", c + i); 9 | } 10 | inline void solve(void){ 11 | while(n >= k){ 12 | // subtract (ax^n + ax^(n-k)) 13 | c[n-k] -= c[n]; 14 | --n; 15 | } 16 | } 17 | inline void print(void){ 18 | n = max(n, 0); 19 | while(n > 0 && !c[n])--n; 20 | for (int i = 0; i <= n; ++i) 21 | printf("%d%c", c[i], " \n"[i == n]); 22 | } 23 | int main(void){ 24 | while(scanf("%d%d", &n, &k), n != -1 || k != -1){ 25 | read(); 26 | solve(); 27 | print(); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/10589.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define eps 1e-9 3 | using namespace std; 4 | typedef long double d; 5 | struct point{ 6 | d x, y; 7 | }; 8 | inline const d dist_sq(const point & a, const point & b){ 9 | return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y); 10 | } 11 | int n, a; 12 | int main(){ 13 | // freopen("Input.txt", "r", stdin); 14 | while (scanf("%d", &n), n){ 15 | scanf("%d", &a); 16 | int a_2 = a*a; 17 | d x, y; 18 | int counter = 0; 19 | int X[4] = { 0, 0, a, a }; 20 | int Y[4] = { 0, a, 0, a }; 21 | for (int i = 0; i < n; ++i){ 22 | scanf("%llf%llf", &x, &y); 23 | bool flag = true; 24 | for (int j = 0; j < 4 && flag; ++j) 25 | if ((x - X[j])*(x - X[j]) + (y - Y[j])*(y - Y[j]) > a_2) flag = false; 26 | counter += flag; 27 | } 28 | // HAD TO WRITE THE PRECISION SPECIFIER ".5" 29 | printf("%.5llf\n", (d)counter*a*a / n); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solutions/UVA/10608.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAX 30005 6 | using namespace std; 7 | int p[MAX]; 8 | int Count[MAX]; 9 | int n, m; 10 | inline int parent(const int i){ 11 | return (p[i] == i)?i:p[i] = parent(p[i]); 12 | } 13 | inline void unite(const int x, const int y){ 14 | p[parent(x)] = parent(y); 15 | } 16 | int main(){ 17 | int TC; 18 | scanf("%d", &TC); 19 | while(TC--){ 20 | scanf("%d%d", &n, &m); 21 | for (int i = 1; i <= n; ++i) p[i] = i; 22 | int x, y; 23 | while(m--) {scanf("%d%d", &x, &y); unite(x, y);} 24 | memset(Count, 0, sizeof Count); 25 | for (int i = 1; i <= n; ++i) ++Count[parent(i)]; 26 | int ans = 0; 27 | for (int i = 1; i <= n; ++i) ans = max(ans, Count[i]); 28 | printf("%d\n", ans); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Solutions/UVA/10633 - Rare Easy Problem.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef unsigned long long ll; 4 | inline bool test(const ll potential_x, const ll final) 5 | { 6 | return (potential_x - (potential_x / 10)) == final; 7 | } 8 | set solve(ll given) 9 | { 10 | set ans; 11 | for (int rem = 0; rem <= 9; ++rem) 12 | { 13 | const ll x_9 = (10LL * given - rem); 14 | if (x_9 % 9 == 0 && test(x_9 / 9, given)) 15 | ans.insert(x_9 / 9); 16 | } 17 | return ans; 18 | } 19 | int main() 20 | { 21 | for (ll given; scanf("%llu", &given), given; ) 22 | { 23 | const auto ans = solve(given); 24 | for (const auto x : ans) 25 | printf("%llu%s", x, x == *ans.rbegin() ? "" : " "); 26 | printf("\n"); 27 | } 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Solutions/UVA/10637 Coprimes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int s, t; 4 | int arr[35]; 5 | inline void rec(int i, int cur){ 6 | if (i == t) 7 | { 8 | if (cur == 0) 9 | for (int j = 0; j < t; ++j) 10 | printf("%d%c", arr[j], " \n"[j == t - 1]); 11 | } 12 | else 13 | for (int j = i ? arr[i-1] : 1; j <= cur; ++j) 14 | { 15 | bool flag = true; 16 | for (int k = 0; k < i && flag; ++k) 17 | if (__gcd(arr[k], j) != 1) flag = false; 18 | if (!flag) continue; 19 | arr[i] = j; 20 | rec(i + 1, cur - j); 21 | } 22 | } 23 | int main() { 24 | int TC; scanf("%d", &TC); 25 | for (int tc = 1; tc <= TC; ++tc){ 26 | scanf("%d%d", &s, &t); 27 | printf("Case %d:\n", tc); 28 | rec(0, s); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Solutions/UVA/10673 Play with Floor and Ceil.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | ll x, y, d; 5 | void euclid(ll a, ll b){ 6 | if (b){ 7 | euclid(b, a %b); 8 | ll x1 = y; 9 | ll y1 = x - (a/b)*y; 10 | x = x1; y = y1; 11 | } 12 | else { 13 | d = a; 14 | x = 1; 15 | y = 0; 16 | } 17 | } 18 | int main(void){ 19 | int TC; scanf("%d", &TC); 20 | for (ll n, k; TC--;){ 21 | scanf("%lld%lld", &n, &k); 22 | euclid(n/k, n/k + bool (n % k)); 23 | x *= n/d; y *= n/d; 24 | printf("%d %d\n", x, y); 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Solutions/UVA/10678 Grazing Cow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | typedef long double ld; 6 | int main(){ 7 | // freopen("Input.txt", "r", stdin); 8 | const ld PI = acos(-1.0); 9 | int d, l, TC; 10 | scanf("%d", &TC); 11 | while (TC--) 12 | scanf("%d%d", &d, &l), printf("%.3llf\n", (ld)PI*(l / 2.0)*sqrt((ld)(l / 2.0)*(l / 2.0) - (d / 2.0)*(d / 2.0))); 13 | } -------------------------------------------------------------------------------- /Solutions/UVA/10684 The jackpot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // algorithm does not work with long long 4 | using namespace std; 5 | int main(){ 6 | freopen("Input.txt", "r", stdin); 7 | freopen("Output.txt", "w", stdout); 8 | int n; 9 | int x[10005]; 10 | int memo[10005]; 11 | int Max; 12 | while (scanf("%d", &n), n){ 13 | for (int i = 0; i < n; i++) scanf("%d", &x[i]); 14 | Max = memo[n - 1] = x[n - 1]; 15 | for (int i = n - 2; i >= 0; i--) 16 | Max = max(Max, memo[i] = x[i] + max(0, memo[i+1])); 17 | if (Max > 0) printf("The maximum winning streak is %d.\n", Max); 18 | else printf("Losing streak.\n"); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Solutions/UVA/10689 Yet another Number System.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 15005 3 | using namespace std; 4 | int a, b, n, m; 5 | int memo[MAX]; 6 | int solve(void){ 7 | int upper_bound; 8 | switch(m){ 9 | case 1:upper_bound = 60;break; 10 | case 2:upper_bound = 300;break; 11 | case 3:upper_bound = 1500;break; 12 | case 4:upper_bound = 15000;break; 13 | } 14 | int ten = 1; 15 | while(m--) ten *= 10; 16 | n %= upper_bound; 17 | memo[0] = a % ten, memo[1] = b% ten; 18 | for (int i = 2; i <= n; ++i) 19 | memo[i] = (memo[i - 1] + memo[i - 2])% ten; 20 | return memo[n]; 21 | } 22 | int main(void){ 23 | // freopen("Input.txt", "r", stdin); 24 | int TC; 25 | scanf("%d", &TC); 26 | while(TC--){ 27 | scanf("%d%d%d%d", &a, &b, &n, &m); 28 | printf("%d\n", solve()); 29 | } 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Solutions/UVA/10699 Count the Factors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1000005 3 | using namespace std; 4 | int memo[MAX] = {0}; 5 | inline void init(void){ 6 | for (int i = 2; i <= 1000000; ++i) 7 | if (!memo[i]) 8 | for (int j = i; j <= 1000000; j += i) 9 | ++memo[j]; 10 | } 11 | int main(void){ 12 | // freopen("Input.txt", "r", stdin); 13 | init(); 14 | for (int x; scanf("%d", &x), x;) 15 | printf("%d : %d\n", x, memo[x]); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Solutions/UVA/10721 Bar Codes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 52 3 | using namespace std; 4 | typedef long long int ll; 5 | int n, m, k; 6 | ll memo[MAX][MAX]; 7 | /* number of barcodes of length i containing exactly j bars 8 | */ 9 | inline ll dp(const int i, const int j) 10 | { 11 | if (i < 0 || j < 0) 12 | { 13 | return 0LL; 14 | } 15 | ll & ret = memo[i][j]; 16 | if (ret != -1LL) 17 | { 18 | return ret; 19 | } 20 | if (i == 0 || j == 0) 21 | { 22 | return ret = 0LL; 23 | } 24 | ret = 0LL; 25 | for (int t = 1; t <= m; ++t) 26 | { 27 | ret += dp(i - t, j - 1); 28 | } 29 | return ret; 30 | } 31 | inline void init_memo(void) 32 | { 33 | memset(memo, -1, sizeof memo); 34 | memo[0][0] = 1LL; 35 | } 36 | int main() { 37 | while(~scanf("%d%d%d", &n, &k, &m)) 38 | { 39 | init_memo(); 40 | printf("%lld\n", dp(n, k)); 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Solutions/UVA/10773.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define eps 1e-9 5 | using namespace std; 6 | int main(){ 7 | // freopen("Input.txt", "r", stdin); 8 | long double d, v, u; 9 | int TC; 10 | scanf("%d", &TC); 11 | for (int tc = 1; tc <= TC; ++tc){ 12 | scanf("%llf%llf%llf", &d, &v, &u); 13 | printf("Case %d: ", tc); 14 | // CORNER CASE: INPUT IS 0? 15 | if ((v > u) || v < eps || u < eps || u == v) printf("can't determine\n"); 16 | else { 17 | // LOOK AT THE EQUATION AND SEE WHEN IT FAILS. E.G. U = V 18 | long double x = d/sqrt(u*u - v*v); 19 | long double y = d/u; 20 | printf("%.3llf\n", (x > y)? x-y:y-x); 21 | } 22 | } 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Solutions/UVA/10774 Repeated Josephus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 30010 3 | using namespace std; 4 | int memo[MAX]; 5 | inline void init(void){ 6 | memo[1] = 0; // index zero wins if you only have one person 7 | for (int i = 2; i < MAX; ++i) 8 | memo[i] = (memo[i-1] + 2) % i; 9 | } 10 | typedef pair ii; 11 | ii solve(int n){ 12 | int x = memo[n] + 1; 13 | if (x == n) return ii(0, n); 14 | ii tmp = solve(x); 15 | return ii(tmp.first + 1, tmp.second); 16 | } 17 | int main(void){ 18 | init(); 19 | int TC; scanf("%d", &TC); 20 | for(int tc = 1, n; tc <= TC; ++tc){ 21 | scanf("%d", &n); 22 | ii ans = solve(n); 23 | printf("Case %d: %d %d\n", tc, ans.first, ans.second); 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/10784 Diagonal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 45000000LL 4 | using namespace std; 5 | typedef long long int ll; 6 | ll memo[MAX]; 7 | void init(void){ 8 | memo[0] = memo[1] = memo[2] = -1; 9 | for (ll i = 3LL; i < MAX; ++i) 10 | memo[i] = ((i - 3LL)*i)/2LL; 11 | } 12 | int main(void){ 13 | // freopen("Input.txt", "r", stdin); 14 | init(); 15 | ll N; 16 | int tc = 0; 17 | while(scanf("%lld", &N), N) 18 | printf("Case %d: %d\n", ++tc, lower_bound(memo, memo + MAX, N) - memo); 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Solutions/UVA/10790 How Many Points of Intersection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | int main(void) 5 | { 6 | for (int a, b, tc = 1; scanf("%d%d", &a, &b), a || b; ++tc) 7 | { 8 | ll ans = 0; 9 | /* 10 | for (int j = 1; j < a; ++j) 11 | for (int i = 1; i < b; ++i) 12 | { 13 | ans += (ll) i * j; 14 | } 15 | */ 16 | ans = (ll) ((a * (a - 1LL)) / 2LL) * ((b * (b - 1LL)) / 2LL); 17 | printf("Case %d: %lld\n", tc, ans); 18 | } 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Solutions/UVA/10814 Simplifying Fractions.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.math.BigInteger; 3 | import java.math.*; 4 | import java.lang.Integer; 5 | import java.io.*; 6 | public class Main { 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int TC = sc.nextInt(); 10 | while(TC-- > 0){ 11 | BigInteger a = sc.nextBigInteger(); 12 | sc.skip(" / "); 13 | BigInteger b = sc.nextBigInteger(); 14 | BigInteger c = a.gcd(b); 15 | a = a.divide(c); 16 | b = b.divide(c); 17 | System.out.println(a.toString() + " / " + b.toString()); 18 | } 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Solutions/UVA/10878.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | string s; 5 | int main(){ 6 | // freopen("Input.txt", "r", stdin); 7 | getline(cin, s); 8 | char c = s[0]; 9 | while(getline(cin, s), s != ""){ 10 | if (s[0] == c) break; 11 | char d = 0; 12 | int counter = 7; 13 | int i = 1; 14 | while(counter >= 0){ 15 | if (s[i] != ' ' && s[i] != 'o') {++i; continue;} 16 | d |= (int(s[i++] == 'o')) << counter--; 17 | } 18 | printf("%c", d); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/10918 Tri Tiling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX (1 << 5) 3 | using namespace std; 4 | typedef long long int ll; 5 | ll memo[MAX]; 6 | inline void preprocess(void) 7 | { 8 | memo[0] = 1LL; 9 | memo[1] = 0LL; 10 | for (int i = 2; i < MAX; ++i) 11 | { 12 | ll & cur = memo[i]; 13 | // you can close at 2 in 3 different ways 14 | // afterwards, you can close at 4 in 2 different ways 15 | cur = 3 * memo[i - 2]; 16 | for (int j = i - 4; j >= 0; j -= 2) 17 | cur += memo[j] << 1LL; 18 | } 19 | } 20 | int main(void) 21 | { 22 | preprocess(); 23 | for (int n; scanf("%d", &n), ~n; ) 24 | printf("%lld\n", memo[n]); 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Solutions/UVA/10922 2 the 9s.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX (1 << 10) 3 | using namespace std; 4 | typedef long long int ll; 5 | char s[MAX]; 6 | inline int deg(ll i) 7 | { 8 | if (!i || i % 9LL) 9 | { 10 | return 0; 11 | } 12 | if (i == 9) 13 | { 14 | return 1; 15 | } 16 | 17 | ll j = 0; 18 | while(i) 19 | { 20 | j += (i % 10LL); 21 | i /= 10LL; 22 | } 23 | return 1 + deg(j); 24 | } 25 | int main(void) 26 | { 27 | while(scanf("%s", s), strcmp(s, "0")) 28 | { 29 | printf("%s ", s); 30 | const int n = strlen(s); 31 | ll ans = 0LL; 32 | for (int i = 0; i < n; ++i) 33 | { 34 | ans += (ll) (s[i] - '0'); 35 | } 36 | 37 | if (ans % 9LL) 38 | { 39 | printf("is not a multiple of 9."); 40 | } 41 | else 42 | { 43 | printf("is a multiple of 9 and has 9-degree %d.", deg(ans)); 44 | } 45 | printf("\n"); 46 | } 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Solutions/UVA/10924 Prime Words.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX (1 << 11) 3 | using namespace std; 4 | bitset bs; 5 | inline void sieve(void) 6 | { 7 | bs.set(); 8 | bs[0] = 0; 9 | for (int i = 2; i <= MAX; ++i) 10 | if (bs[i]) 11 | { 12 | for (int j = i * i; j <= MAX; j += i) 13 | { 14 | bs[j] = 0; 15 | } 16 | } 17 | } 18 | char s[100]; 19 | inline int value(const char c) 20 | { 21 | return (c >= 'a' && c <= 'z') ? (int) c - 'a' + 1 : (int) c - 'A' + 27; 22 | } 23 | int main(void) 24 | { 25 | sieve(); 26 | while(~scanf("%s", s)) 27 | { 28 | int val = 0; 29 | for (int i = 0; s[i]; ++i) 30 | { 31 | val += value(s[i]); 32 | } 33 | printf("%s\n", bs[val] ? "It is a prime word." : "It is not a prime word."); 34 | } 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Solutions/UVA/10929 You can say 11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX (1 << 10) 3 | using namespace std; 4 | char s[MAX]; 5 | inline int calc(void) 6 | { 7 | const int n = strlen(s); 8 | int ans = 0; 9 | for (int i = 0; i < n; ++i) 10 | { 11 | ans = (ans * 10 + s[i] - '0') % 11; 12 | } 13 | return ans; 14 | } 15 | 16 | int main(void) 17 | { 18 | while(scanf("%s", s), strcmp(s, "0")) 19 | printf("%s is %sa multiple of 11.\n", s, calc() == 0 ? "" : "not "); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/10929 You can say 11.java: -------------------------------------------------------------------------------- 1 | import java.math.*; 2 | import java.util.*; 3 | 4 | public class Main 5 | { 6 | public static void main(String[] args) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | while(true) 10 | { 11 | String s = sc.nextLine().trim(); 12 | BigInteger x = new BigInteger(s); 13 | if (x.equals(BigInteger.ZERO)) 14 | { 15 | break; 16 | } 17 | 18 | System.out.println(s + " is " + (x.mod(BigInteger.valueOf(11)).equals(BigInteger.ZERO) ? "" : "not ") + "a multiple of 11."); 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/10931 Parity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define LSOne(s) ((s) & (-s)) 3 | using namespace std; 4 | inline int count_ones(int s){ 5 | int ans = 0; 6 | while(s) 7 | ++ans, s -= LSOne(s); 8 | return ans; 9 | } 10 | inline void print(int s){ 11 | int i = 31; 12 | while(!(s & (1 << i))) --i; 13 | for (; i >= 0; --i) 14 | printf("%c", '0' + bool(s & (1 << i))); 15 | } 16 | int main(void){ 17 | for (int x; scanf("%d", &x), x; ){ 18 | printf("The parity of "); 19 | print(x); 20 | printf(" is %d (mod 2).\n", count_ones(x)); 21 | } 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Solutions/UVA/10940 Throwing cards away II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | inline int f(int n){ 4 | if (n == 1) return 0; 5 | bool odd = n & 1; 6 | int x = f((n >> 1) + odd); 7 | if (odd) 8 | return x ? (x << 1) - 1 : n - 1; 9 | return (x << 1) + 1; 10 | } 11 | int main() { 12 | for (int n; scanf("%d", &n), n; printf("%d\n", f(n) + 1)); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Solutions/UVA/10990 Another New Function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 2000000 3 | using namespace std; 4 | typedef long long int ll; 5 | ll acc[MAX]; 6 | int dep_phi[MAX + 10]; 7 | int phi[MAX + 10]; 8 | inline void init(void){ 9 | for (int i = 1; i <= MAX; ++i) 10 | phi[i] = i; 11 | for (int i = 2; i <= MAX; ++i) 12 | if (phi[i] == i) 13 | for (int j = i; j <= MAX; j += i) 14 | phi[j] = (phi[j]/i)*(i-1); 15 | dep_phi[1] = 0; 16 | for (int i = 2; i <= MAX; ++i) 17 | dep_phi[i] = 1 + dep_phi[phi[i]]; 18 | acc[1] = 0; 19 | for (int i = 2 ;i <= MAX; ++i) 20 | acc[i] = dep_phi[i] + acc[i - 1]; 21 | } 22 | int main(void){ 23 | init(); 24 | int TC; scanf("%d", &TC); 25 | for (int x, y; TC--;){ 26 | scanf("%d%d", &x, &y); 27 | printf("%lld\n", acc[y] - acc[x - 1]); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/11042 Complex, difficult and complicated.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define N (1 << 10) 3 | #define MAX (1LL << 30LL) 4 | using namespace std; 5 | typedef long long int ll; 6 | typedef complex cll; 7 | int main(void) 8 | { 9 | int TC; scanf("%d", &TC); 10 | for(int x, y, n; TC--; ) 11 | { 12 | scanf("%d%d", &x, &y); 13 | cll z = cll(x, y); 14 | cll t = z; 15 | n = 1; 16 | while(n < N && t.imag() != 0 && t.imag() <= MAX && t.real() <= MAX) 17 | { 18 | t *= z; 19 | ++n; 20 | } 21 | if (n < N && t.imag() == 0 && t.real() <= MAX) 22 | printf("%d\n", n); 23 | else 24 | printf("TOO COMPLICATED\n"); 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Solutions/UVA/11069 A Graph Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 82 4 | using namespace std; 5 | int memo[MAX]; 6 | int n; 7 | int dp(int i){ 8 | int & ans = memo[i]; 9 | if (ans != -1) return ans; 10 | return ans = dp(i + 2) + dp(i + 3); 11 | } 12 | void init(void){ 13 | memset(memo, 0xFF, sizeof memo); 14 | memo[n + 1] = 0; 15 | memo[n] = 1; 16 | memo[n-1] = 1; 17 | if (n >= 2) memo[n-2] = 1; 18 | if (n >= 3) memo[n-3] = 2; 19 | } 20 | int main(void){ 21 | // freopen("Input.txt", "r", stdin); 22 | while(scanf("%d", &n) != EOF) 23 | init(), printf("%d\n", dp(1) + dp(2)); 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/11084 Anagram Division.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | char s[15]; 5 | int n, d; 6 | inline bool valid(void) 7 | { 8 | long long int cur = 0LL; 9 | for (int i = 0; i < n; ++i) 10 | { 11 | cur = (ll)cur * 10LL + s[i]; 12 | } 13 | return (cur % d) == 0; 14 | } 15 | int main() { 16 | int TC; scanf("%d", &TC); 17 | while(TC--) 18 | { 19 | scanf("%s%d", s, &d); 20 | n = strlen(s); 21 | sort(s, s + n); 22 | for (int i = 0; i < n; ++i) 23 | { 24 | s[i] -= '0'; 25 | } 26 | int ans = 0; 27 | do { 28 | ans += valid(); 29 | } while(next_permutation(s, s + n)); 30 | printf("%d\n", ans); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Solutions/UVA/11086 Composite Prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1000000LL 3 | using namespace std; 4 | typedef long long int ll; 5 | vector primes; 6 | inline void sieve(void) 7 | { 8 | bitset bs; 9 | bs.set(); 10 | for (ll i = 2LL; i <= MAX; ++i) 11 | if (bs[i]) 12 | { 13 | primes.push_back(i); 14 | for (ll j = i * i; j <= MAX; j += i) 15 | bs[j] = 0; 16 | } 17 | } 18 | inline bool composite_prime(ll x) 19 | { 20 | int counter = 0; 21 | for (ll i = 0, p = primes[0]; p * p <= x; p = primes[++i]) 22 | { 23 | while(x % p == 0) ++counter, x /= p; 24 | if (counter > 2) return false; 25 | } 26 | if (x != 1) ++counter; 27 | return counter == 2; 28 | } 29 | int main(void) 30 | { 31 | sieve(); 32 | int n; 33 | for(int ans = 0; ~scanf("%d", &n); ans = 0) 34 | { 35 | for(int a; n--;) 36 | scanf("%d", &a), ans += composite_prime(a); 37 | printf("%d\n", ans); 38 | } 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Solutions/UVA/11105 Semi-prime H-numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1000100 3 | using namespace std; 4 | int ans[MAX]; 5 | bool prime[MAX], semi_prime[MAX]; 6 | inline void sieve(void){ 7 | memset(prime, 1, sizeof prime); 8 | for (int i = 5; i*i < MAX; i += 4) 9 | if (prime[i]) 10 | for (int j = i*i; j < MAX; j += (i << 2)) 11 | prime[j] = 0; 12 | memset(semi_prime, 0, sizeof semi_prime); 13 | for (int i = 5; i*i < MAX; i += 4) 14 | if (prime[i]) 15 | for (int j = i; i*j < MAX; j += 4) 16 | if (prime[j]) 17 | semi_prime[i * j] = true; 18 | ans[1] = 0; 19 | for (int i = 0; i < MAX; ++i) 20 | ans[i] = ans[i - 1] + semi_prime[i]; 21 | } 22 | int main(void){ 23 | sieve(); 24 | for (int n; scanf("%d", &n), n; printf("%d %d\n", n, ans[n])); 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Solutions/UVA/11133 Eigensequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 46 3 | using namespace std; 4 | int memo[MAX][MAX]; 5 | inline bool valid(const int i, const int k) 6 | { 7 | const int diff = k - i; 8 | for (int x = i + 1; x < k; ++x) 9 | { 10 | if (x % diff == 0) 11 | { 12 | return false; 13 | } 14 | } 15 | return k % diff == 0; 16 | } 17 | inline int dp(const int i, const int j) 18 | { 19 | int & ret = memo[i][j]; 20 | if (ret != -1) 21 | { 22 | return ret; 23 | } 24 | if (i == j) 25 | { 26 | return ret = 1; 27 | } 28 | ret = 0; 29 | for (int k = i + 1; k <= j; ++k) 30 | { 31 | if (valid(i, k)) 32 | { 33 | ret += dp(k, j); 34 | } 35 | } 36 | return ret; 37 | } 38 | int main(void) 39 | { 40 | memset(memo, -1, sizeof memo); 41 | for (int x, y; scanf("%d%d", &x, &y), x || y; printf("%d %d %d\n", x, y, dp(x, y))); 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Solutions/UVA/11172 Relational Operator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void) 4 | { 5 | int TC; scanf("%d", &TC); 6 | for (int a, b; TC--;) 7 | { 8 | scanf("%d%d", &a, &b); 9 | printf("%c\n", a == b ? '=' : a < b ? '<' : '>'); 10 | } 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Solutions/UVA/11173 Grey Codes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | int main(void) 5 | { 6 | #ifndef ONLINE_JUDGE 7 | freopen("input.in", "r", stdin); 8 | #endif 9 | int TC; scanf("%d", &TC); 10 | for(ll n, k; TC--; ) 11 | { 12 | scanf("%lld%lld", &n, &k); 13 | // printf("%d\n", k ^ (k >> 1)); 14 | // this can be the answer 15 | ll ans = 0LL; 16 | for (ll i = n - 1; i >= 0LL; --i) 17 | { 18 | const ll period = 1LL << (i + 2LL); 19 | const ll cur = (k % period) / (period >> 2); 20 | if (cur == 1LL || cur == 2LL) 21 | ans |= (1LL << i); 22 | } 23 | printf("%lld\n", ans); 24 | } 25 | return 0; 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Solutions/UVA/11176 Winning Streak_2.cpp: -------------------------------------------------------------------------------- 1 | // bottom-up DP solution 2 | #include 3 | #define MAX 502 4 | using namespace std; 5 | int n; 6 | double p, q; 7 | double memo[MAX][MAX][MAX]; 8 | int main(void) 9 | { 10 | while(scanf("%d%lf", &n, &p), n) 11 | { 12 | q = 1.0 - p; 13 | for (int j = 0; j <= n; ++j) 14 | { 15 | for (int k = 0; k <= n; ++k) 16 | { 17 | memo[n][j][k] = (double) max(j, k); 18 | } 19 | } 20 | for (int i = n - 1; i >= 0; --i) 21 | { 22 | for (int j = 0; j <= i + 1; ++j) 23 | { 24 | for (int k = 0; k <= j; ++k) 25 | { 26 | memo[i][j][k] = p * memo[i + 1][max(j, k + 1)][k + 1] + q * memo[i + 1][j][0]; 27 | } 28 | } 29 | } 30 | printf("%lf\n", memo[0][0][0]); 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Solutions/UVA/11203 Can you decide it for ME.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool valid(char * s){ 4 | int iE = -1, iM = -1; 5 | int i; 6 | for (i = 0; s[i]; ++i) 7 | switch(s[i]){ 8 | case '?':break; 9 | case 'M':{ 10 | if (iM != -1) return false; 11 | iM = i; 12 | break; 13 | } 14 | case 'E':{ 15 | if (iE != -1) return false; 16 | iE = i; 17 | break; 18 | } 19 | default: return false; 20 | } 21 | if (iM == 0) return false; 22 | if (iE == i - 1) return false; 23 | int diff = iE - iM; 24 | if (diff <= 1) return false; 25 | diff -= 2; 26 | // y has diff extra '?' symbols 27 | int j = i - iE - 1 - diff - 1; 28 | return j == iM; 29 | } 30 | char s[60]; 31 | int main(void){ 32 | // freopen("Input.txt", "r", stdin); 33 | int TC; 34 | scanf("%d", &TC); 35 | while(TC--){ 36 | scanf("%s", s); 37 | if (!valid(s)) printf("no-"); 38 | printf("theorem\n"); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Solutions/UVA/11204 Musical instruments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX (1 << 6) 3 | using namespace std; 4 | typedef long long int ll; 5 | int fav[MAX]; 6 | int n, m; 7 | inline void init(void) 8 | { 9 | memset(fav, 0, sizeof fav); 10 | } 11 | int main(void) 12 | { 13 | int TC; scanf("%d", &TC); 14 | while(TC--) 15 | { 16 | init(); 17 | scanf("%d%d", &n, &m); 18 | for (int x, i = 0; i < m; ++i) 19 | { 20 | for (int j = 0; j < n; ++j) 21 | { 22 | scanf("%d", &x); 23 | if (x == 1) 24 | { 25 | ++fav[j]; 26 | } 27 | } 28 | } 29 | 30 | ll ans = 1LL; 31 | for (int i = 0; i < n; ++i) 32 | ans *= (ll) max(1, fav[i]); 33 | printf("%lld\n", ans); 34 | } 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Solutions/UVA/11218 KTV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100 3 | #define INF 1000000000 4 | using namespace std; 5 | int score[MAX], val[MAX], n; 6 | inline void read(void) 7 | { 8 | for (int i = 0, x, y, z; i< n; ++i) 9 | { 10 | scanf("%d%d%d%d", &x, &y, &z, score + i); 11 | --x; --y; --z; 12 | val[i] = (1 << x) | (1 << y) | (1 << z); 13 | } 14 | } 15 | int main() { 16 | for(int tc = 1; scanf("%d", &n), n; ++tc) 17 | { 18 | read(); 19 | int ans = 0; 20 | for (int i = 0; i < n; ++i) 21 | for (int j = i + 1; j < n; ++j) 22 | for (int k = j + 1; k < n; ++k) 23 | if ((val[i] | val[j] | val[k]) == 0x1FF) 24 | ans = max(ans, score[i] + score[j] + score[k]); 25 | printf("Case %d: %d\n", tc, ans ? ans : -1); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Solutions/UVA/11231 Black and white painting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | inline int get_num(int n, int m) 4 | { 5 | return max((n >> 1) - 3, 0) * max((m >> 1) - 3, 0); 6 | } 7 | inline int solve(int n, int m, int c){ 8 | return c ? get_num(n, m) + get_num(n-1, m-1) : get_num(n, m - 1) + get_num(n - 1, m); 9 | } 10 | int main(void){ 11 | for (int n, m, c; scanf("%d%d%d", &n, &m, &c), n || m || c; printf("%d\n", solve(n, m, c))); 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Solutions/UVA/11258 String Partition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 205 3 | using namespace std; 4 | typedef long long int ll; 5 | int len; 6 | char s[MAX]; 7 | ll memo[MAX]; 8 | ll dp(int i){ 9 | ll & ret = memo[i]; 10 | if (ret != -1LL) return ret; 11 | ll ans = 0LL; 12 | for (ll p = 0; i < len; ++i) 13 | { 14 | p = (10LL * p) + s[i]; 15 | if ((ll) p > INT_MAX) break; 16 | ans = max(ans, p + dp(i + 1)); 17 | } 18 | return ret = ans; 19 | } 20 | inline void init(void){ 21 | memset(memo, 0xFF, sizeof memo); 22 | len = strlen(s); 23 | memo[len] = memo[len + 1] = 0LL; 24 | for (int i = 0; i < len; ++i) 25 | s[i] -= '0'; 26 | } 27 | inline void read(void){ 28 | scanf("%s", s); 29 | } 30 | int main(void){ 31 | int TC; scanf("%d", &TC); 32 | while(TC--){ 33 | read(); 34 | init(); 35 | printf("%lld\n", dp(0)); 36 | } 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Solutions/UVA/11287 Pseudoprime Numbers.cpp: -------------------------------------------------------------------------------- 1 | /* package whatever; // don't place package name! */ 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | import java.io.*; 6 | import java.math.*; 7 | 8 | /* Name of the class has to be "Main" only if the class is public. */ 9 | class Main 10 | { 11 | public static void main (String[] args) 12 | { 13 | Scanner sc = new Scanner(System.in); 14 | BigInteger a, p; 15 | while(true){ 16 | p = sc.nextBigInteger(); 17 | a = sc.nextBigInteger(); 18 | if (a.equals(BigInteger.ZERO) && p.equals(BigInteger.ZERO)) 19 | break; 20 | if (!p.isProbablePrime(10) && a.modPow(p, p).equals(a)) 21 | System.out.println("yes"); 22 | else 23 | System.out.println("no"); 24 | } 25 | // your code goes here 26 | } 27 | } -------------------------------------------------------------------------------- /Solutions/UVA/11291 Smeech.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | string line; 4 | int i; 5 | inline double solve() 6 | { 7 | while(isspace(line[i])) ++i; 8 | double ans; 9 | if (line[i] == '(') 10 | { 11 | ++i; 12 | // it's a double! 13 | sscanf(line.c_str() + i, "%lf", &ans); 14 | while(i < line.length() && (isdigit(line[i]) || line[i] == '.')) ++i; 15 | double x = solve(); 16 | double y = solve(); 17 | while(line[i] != ')') ++i; 18 | ++i; 19 | return ans * (x + y) + (1.0 - ans) * (x - y); 20 | } 21 | else 22 | { 23 | sscanf(line.c_str() + i, "%lf", &ans); 24 | while(i < line.length() && (isdigit(line[i]) || line[i] == '-' || line[i] == '+')) 25 | ++i; 26 | return ans; 27 | } 28 | } 29 | int main(void) 30 | { 31 | while(getline(cin, line), strcmp(line.c_str(), "()")) 32 | { 33 | i = 0; 34 | printf("%.2lf\n", solve()); 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Solutions/UVA/11292.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | freopen("Input.txt", "r", stdin); 9 | freopen("Output.txt", "w", stdout); 10 | int n, m; 11 | int dragon[20000], knight[20000]; 12 | int k, d; 13 | long long gold; // knights and dragons counters 14 | while (scanf("%d%d", &n, &m), n && m){ 15 | for (int i = 0; i < n; i++) 16 | scanf("%d", &dragon[i]); 17 | for (int i = 0; i < m; i++) 18 | scanf("%d", &knight[i]); 19 | sort(dragon, dragon + n); 20 | sort(knight, knight + m); 21 | gold = k = d = 0; 22 | while (d < n){ 23 | while (knight[k] < dragon[d] && k < m) k++; 24 | if (k >= m) break; 25 | gold += knight[k]; 26 | d++; k++; 27 | } 28 | if (d < n) printf("Loowater is doomed!\n"); 29 | else cout << gold << '\n'; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Solutions/UVA/11310 Delivery Debacle.java: -------------------------------------------------------------------------------- 1 | import java.math.*; 2 | import java.util.*; 3 | import java.lang.*; 4 | public class Main { 5 | final static int MAX = 42; 6 | static long memo[] = new long[MAX]; 7 | public static void init(){ 8 | memo[0] = 1L; 9 | memo[1] = 1L; 10 | memo[2] = 5L; 11 | for (int i = 3; i < MAX; ++i) 12 | memo[i] = memo[i-1] + (memo[i-2] << 2) + (memo[i-3] << 1); 13 | } 14 | public static void main(String[] args) { 15 | init(); 16 | Scanner sc = new Scanner(System.in); 17 | int TC = sc.nextInt(); 18 | while(TC-- > 0) 19 | System.out.println(memo[sc.nextInt()]); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Solutions/UVA/11311 Exclusively Edible_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | inline bool win(const int a, const int b, const int c, const int d){ 4 | return a ^ b ^ c ^ d; 5 | } 6 | int main(void){ 7 | int TC; scanf("%d", &TC); 8 | for(int m, n, r, c; TC--; printf("%s\n", win(r, c, m - 1 - r, n - 1 - c) ? "Gretel" : "Hansel")) 9 | scanf("%d%d%d%d", &m, &n, &r, &c); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Solutions/UVA/11344 The Huge One.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | int rem[13]; 5 | char s[2010]; 6 | inline void compute_rem(void){ 7 | memset(rem, 0, sizeof rem); 8 | for (char * c = s; *c; ++c) 9 | { 10 | for (int i = 1; i <= 12; ++i) 11 | { 12 | int & v = rem[i]; 13 | v = ((10 * v) + (*c - '0')) % i; 14 | } 15 | } 16 | } 17 | inline bool wonderful(void){ 18 | int n; 19 | bool flag = true; 20 | scanf("%d", &n); 21 | for(int x;n--;){ 22 | scanf("%d", &x); 23 | if (rem[x]) flag = false; 24 | } 25 | return flag; 26 | } 27 | int main(void){ 28 | int TC; scanf("%d", &TC); 29 | while(TC--){ 30 | scanf("%s", s); 31 | compute_rem(); 32 | printf("%s - %s.\n", s, wonderful()? "Wonderful" : "Simple"); 33 | } 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Solutions/UVA/11346 Probability.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(void) 4 | { 5 | int TC; scanf("%d", &TC); 6 | for (double a, b, s; TC--;) 7 | { 8 | scanf("%lf%lf%lf", &a, &b, &s); 9 | if (s == 0.0) 10 | { 11 | printf("100.000000\%\n"); 12 | continue; 13 | } 14 | const double w = s / b; 15 | if (w >= a) 16 | { 17 | printf("0.000000\%\n"); 18 | continue; 19 | } 20 | const double tot = a * b; 21 | const double in = w * b + s * (log(a) - log(w)); 22 | printf("%.6lf\%\n", (1.0 - in / tot) * 100); 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/11351 Last Man Standing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // add one at the end 4 | inline int f(int n, int k){ 5 | if (n == 1) // only one man left 6 | return 0; 7 | return (f(n-1, k) + k) % n; 8 | } 9 | int main(void){ 10 | int TC; scanf("%d", &TC); 11 | for (int tc = 1, n, k; tc <= TC; ++tc) 12 | scanf("%d%d", &n, &k), printf("Case %d: %d\n", tc, f(n, k) + 1); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Solutions/UVA/11371 Number Theory for Newbies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | char s[15]; 5 | char r[15]; 6 | bool reverse_cmp(const char a, const char b){ 7 | return b < a; 8 | } 9 | inline void normalize(void){ 10 | char * c = r; 11 | while(*c == '0') ++c; 12 | if (c != r) swap(*r, *c); 13 | } 14 | ll diff(){ 15 | ll x, y; 16 | sscanf(s, "%lld", &x); // larger 17 | sscanf(r, "%lld", &y); // smaller 18 | return x - y; 19 | } 20 | int main(void){ 21 | while(scanf("%s", s) != EOF){ 22 | int n = strlen(s); 23 | sort(s, s + n, reverse_cmp); 24 | reverse_copy(s, s + n, r);r[n] = 0; 25 | normalize(); 26 | ll k = diff(); 27 | printf("%s - %s = %lld = 9 * %lld\n", s, r, k, k/9); 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/UVA/11401 Triangle Counting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 1000010 3 | using namespace std; 4 | typedef long long int ll; 5 | ll memo[MAX]; 6 | ll sum(ll n){ 7 | return (n*(n + 1)) >> 1; 8 | } 9 | ll solve_odd(ll n){ 10 | return 2LL * sum(n >> 1LL) + ((n >> 1LL) + 1LL); 11 | } 12 | ll solve_even(ll n){ 13 | return 2LL * sum(n >> 1LL); 14 | } 15 | inline ll solve(ll n){ 16 | if (n & 1LL) return solve_odd(n); 17 | return solve_even(n); 18 | } 19 | inline void init(void){ 20 | memo[3] = 0; 21 | for (ll i = 4; i < MAX; ++i) 22 | memo[i] = memo[i - 1] + solve(i - 3); 23 | } 24 | int main(void){ 25 | init(); 26 | for (int n; scanf("%d", &n), n >= 3; printf("%lld\n", memo[n])); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/UVA/11408 Count DePrimes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 5000100 3 | using namespace std; 4 | int memo[MAX] = {0}; 5 | int acc[MAX]; 6 | inline void sieve(void){ 7 | for (int i = 2; i < MAX; ++i) 8 | if (!memo[i]) 9 | for (int j = i << 1; j < MAX; j += i) 10 | memo[j] += i; 11 | acc[1] = 0; 12 | acc[2] = 1; 13 | for (int i = 3; i < MAX; ++i) 14 | acc[i] = acc[i - 1] + (!memo[i] || !memo[memo[i]]); 15 | } 16 | int main(void){ 17 | sieve(); 18 | for (int a, b; scanf("%d", &a), a; ){ 19 | scanf("%d", &b); 20 | printf("%d\n", acc[b] - acc[a-1]); 21 | } 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Solutions/UVA/11413 Fill the Containers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 1005 4 | using namespace std; 5 | int v[MAX]; 6 | int n, m; 7 | bool can(int x){ 8 | int j = 0; 9 | for (int i = 0; i < m && j < n; ++i){ 10 | int temp = x; 11 | while(temp >= v[j] && j < n) temp -= v[j++]; 12 | } 13 | return j >= n; 14 | } 15 | int main(void){ 16 | // freopen("Input.txt", "r", stdin); 17 | while(scanf("%d%d", &n, &m) != EOF){ 18 | for (int i = 0; i < n; ++i) 19 | scanf("%d", v + i); 20 | int lo = 0, hi = 1000000000; 21 | while(lo != hi){ 22 | int mid = min((lo + hi) >> 1, hi - 1); 23 | if (can(mid))hi = mid; 24 | else lo = mid + 1; 25 | } 26 | printf("%d\n", hi); 27 | } 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Solutions/UVA/11415 Count the Factorials.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 3000000LL 3 | using namespace std; 4 | typedef long long int ll; 5 | int arr[MAX + 10]; 6 | inline void sieve(void) 7 | { 8 | memset(arr, 0, sizeof arr); 9 | for (ll i = 2; i <= MAX; ++i) 10 | { 11 | if (arr[i] == 0) 12 | { 13 | for (ll t = i; t <= MAX; t *= i) 14 | { 15 | for (ll j = t; j <= MAX; j += t) 16 | { 17 | ++arr[j]; 18 | } 19 | } 20 | } 21 | } 22 | } 23 | int main(void) 24 | { 25 | sieve(); 26 | for (int i = 1; i < MAX; ++i) 27 | { 28 | arr[i] += arr[i - 1]; 29 | } 30 | int TC; scanf("%d", &TC); 31 | for(int n; TC--; printf("%d\n", upper_bound(arr, arr + MAX, n) - arr)) 32 | { 33 | scanf("%d", &n); 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Solutions/UVA/11428 Cubes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 10000 3 | using namespace std; 4 | typedef long long int ll; 5 | ll cube[MAX]; 6 | inline void init_cube(void) 7 | { 8 | for (ll i = 0LL; i < MAX; ++i) 9 | { 10 | cube[i] = i * i * i; 11 | } 12 | } 13 | int main(void) 14 | { 15 | init_cube(); 16 | for (ll n; scanf("%lld", &n), n;) 17 | { 18 | bool flag = false; 19 | for (ll y = 1LL; y < MAX; ++y) 20 | { 21 | if (binary_search(cube + 1, cube + MAX, cube[y] + n)) 22 | { 23 | ll x = lower_bound(cube + 1, cube + MAX, n + cube[y]) - cube; 24 | printf("%lld %lld\n", x, y); 25 | flag = true; 26 | break; 27 | } 28 | } 29 | if (!flag) 30 | { 31 | printf("No solution\n"); 32 | } 33 | } 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Solutions/UVA/11462 Age Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 105 3 | using namespace std; 4 | int counter[MAX]; 5 | inline void init(void){ 6 | memset(counter, 0, sizeof counter); 7 | } 8 | inline void read(int n){ 9 | for (int x; n--;){ 10 | scanf("%d", &x); 11 | ++counter[x]; 12 | } 13 | } 14 | inline void print(int n){ 15 | for (int i = 0; i < MAX; ++i) 16 | { 17 | int v = counter[i]; 18 | while(v--) 19 | printf("%d%c", i, " \n"[!--n]); 20 | } 21 | } 22 | int main(void){ 23 | for (int n;scanf("%d", &n), n; ){ 24 | init(); 25 | read(n); 26 | print(n); 27 | } 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Solutions/UVA/11538 Chess Queen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef long long int ll; 5 | ll solve(const ll n, const ll m){ 6 | ll rows = m*n*(m - 1); 7 | ll cols = m*n*(n - 1); 8 | ll mn = min(n, m); 9 | ll diags = mn*(mn + 1)*(2LL*mn + 1LL)/3LL - mn*(mn + 1LL) + (n + m - 1 - 2LL*mn)*mn*(mn - 1); 10 | return rows + cols + 2LL*diags; 11 | } 12 | int main(void){ 13 | // freopen("Input.txt", "r", stdin); 14 | int n, m; 15 | while(scanf("%d%d", &n, &m), n || m) 16 | printf("%lld\n", solve(ll(n), ll(m))); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Solutions/UVA/11554 Hapless Hedonism.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX (1 << 20) 3 | using namespace std; 4 | typedef long long int ll; 5 | ll memo[MAX]; 6 | inline ll sum(const ll n) 7 | { 8 | return n * (n + 1LL) / 2LL; 9 | } 10 | inline void init_memo(void) 11 | { 12 | memo[0] = memo[1] = memo[2] = memo[3] = 0LL; 13 | for (int i = 4; i < MAX; ++i) 14 | { 15 | memo[i] = memo[i - 1] + (3 - i + 2LL * sum(i - 2) - 1LL)/4LL; 16 | } 17 | } 18 | int main(void) 19 | { 20 | init_memo(); 21 | int TC; scanf("%d", &TC); 22 | for(ll n; TC--;) 23 | { 24 | scanf("%lld", &n); 25 | printf("%lld\n", memo[n]); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/UVA/11597 Spanning Subtree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int komplete(int n){ 4 | return (n*(n-1)) >> 1; 5 | } 6 | int main(void){ 7 | int n; 8 | for(int tc = 1; scanf("%d", &n), n; ++tc) 9 | printf("Case %d: %d\n", tc, komplete(n)/(n-1)); 10 | return 0; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Solutions/UVA/11628 Another lottery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int arr[10100]; 4 | int n, m; 5 | int main(void) 6 | { 7 | while(scanf("%d%d", &n, &m), n || m) 8 | { 9 | int tot = 0; 10 | for (int i = 0; i < n; ++i) 11 | { 12 | for (int j = 0; j < m; ++j) 13 | { 14 | scanf("%d", arr + i); 15 | } 16 | tot += arr[i]; 17 | } 18 | 19 | for (int i = 0; i < n; ++i) 20 | { 21 | const int d = __gcd(arr[i], tot); 22 | printf("%d / %d\n", arr[i] / d, tot / d); 23 | } 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Solutions/UVA/11646 Athletics Track.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const double eps = 1e-8; 4 | const double pi = acos(-1.0); 5 | double r; 6 | char s[3]; 7 | inline bool can(const double w) 8 | { 9 | const double l = w * r; 10 | const double radius = hypot(l * 0.5, w * 0.5); 11 | 12 | const double alpha = 2.0 * atan2(w * 0.5, l * 0.5); 13 | 14 | return (l + radius * alpha) * 2.0 >= 400.0; 15 | } 16 | 17 | int main(void) 18 | { 19 | double a, b; 20 | for(int tc = 1; ~scanf("%lf%s%lf", &a, s, &b); ++tc) 21 | { 22 | r = a / b; 23 | double lo = 0.0, hi = 1e9; 24 | while(hi - lo > eps) 25 | { 26 | double mid = (lo + hi) * 0.5; 27 | if (can(mid)) 28 | { 29 | hi = mid; 30 | } 31 | else 32 | { 33 | lo = mid; 34 | } 35 | } 36 | 37 | printf("Case %d: %.10lf %.10lf\n", tc, lo * r, lo); 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Solutions/UVA/11718 Fantasy of a Summation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | ll n, k, mod; 5 | ll a; 6 | inline ll pow_mod(ll n, ll k, ll mod){ 7 | ll ans = 1; 8 | for (n = n % mod; k; k >>= 1, n = (n*n) % mod) 9 | if (k & 1LL) ans = (ans * n) % mod; 10 | return ans; 11 | } 12 | int main(void){ 13 | // freopen("Input.txt", "r", stdin); 14 | // freopen("Output.txt", "w", stdout); 15 | int TC; scanf("%d", &TC); 16 | for (int tc = 1; tc <= TC; ++tc){ 17 | // n ^ k 18 | scanf("%d%d%d", &n, &k, &mod); 19 | a = 0LL; 20 | { 21 | ll x; 22 | for (int i = 0; i < n; ++i) 23 | scanf("%lld", &x), a = (a + x) % mod; 24 | } 25 | printf("Case %d: %lld\n", tc, (k*a*pow_mod(n, k - 1, mod)) % mod); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Solutions/UVA/11723.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define eps 1e-9 5 | using namespace std; 6 | int main(){ 7 | // freopen("Input.txt", "r", stdin); 8 | int r, n; 9 | int tc = 0; 10 | while(scanf("%d%d", &r, &n), r || n){ 11 | printf("Case %d: ", ++tc); 12 | int ans; 13 | if (r <= n) ans = 0; 14 | else ans = (r/n) + bool(r%n) - 1; 15 | if (ans > 26) printf("impossible\n"); 16 | else printf("%d\n", ans); 17 | } 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Solutions/UVA/11827 Maximum GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | char line[10000]; 4 | bool line_empty(){ 5 | for (char * s = line; *s; ++s) 6 | if (!isspace(*s)) return false; 7 | return true; 8 | } 9 | int arr[105]; 10 | int main(void){ 11 | // freopen("Input.txt", "r", stdin); 12 | int TC; 13 | for(scanf("%d", &TC); TC--;){ 14 | *line = 0; while(line_empty()) fgets(line, INT_MAX, stdin); 15 | int counter = 0; 16 | for (char * s = strtok(line, " \n");s;s = strtok(0, " \n")) 17 | sscanf(s, "%d", arr + counter++); 18 | int ans = 1; 19 | for (int i = 0; i < counter; ++i) 20 | for (int j = i + 1; j < counter; ++j) 21 | ans = max(ans, __gcd(arr[i], arr[j])); 22 | printf("%d\n", ans); 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/11834 Elevator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | inline bool valid(int l, int c, int r){ 4 | r <<= 1; 5 | return l >= r && c >= r; 6 | } 7 | inline int sqrdst(int dx, int dy){ 8 | return dx*dx + dy*dy; 9 | } 10 | int main(void){ 11 | for (int l, c, r1, r2; scanf("%d%d%d%d", &l, &c, &r1, &r2), l | c | r1 | r2;){ 12 | int sum = r1 + r2; 13 | bool flag =valid(l, c, r1) && valid(l, c, r2) && sqrdst(l - sum, c - sum) >= sum*sum; 14 | printf("%c\n", flag ? 'S' : 'N'); 15 | } 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Solutions/UVA/11875.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define eps 1e-9 6 | using namespace std; 7 | int main(){ 8 | // freopen("Input.txt", "r", stdin); 9 | int TC; 10 | scanf("%d", &TC); 11 | int age[15]; 12 | for (int tc = 1; tc <= TC; ++tc){ 13 | printf("Case %d: ", tc); 14 | int n; 15 | scanf("%d", &n); 16 | for (int i = 0; i < n; ++i) scanf("%d", &age[i]); 17 | sort(age, age + n); 18 | printf("%d\n", age[(n >> 1)]); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/11878 Homework Checker.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef pair ii; 4 | char s[1000]; 5 | inline ii get_int(char * s){ 6 | char tmp[10]; 7 | int len; 8 | int ans = 0; 9 | tmp[0] = s[0]; 10 | for (len = 1;isdigit(s[len]); ++len) 11 | tmp[len] = s[len]; 12 | tmp[len] = 0; 13 | sscanf(tmp, "%d", &ans); 14 | return ii(ans, len); // parsed number & length 15 | } 16 | inline bool correct(void){ 17 | char * c = s; 18 | ii x = get_int(c); c += x.second; 19 | bool addition = (*c == '+'); 20 | ii y = get_int(++c); c += y.second; 21 | if (*c == '?') return false; 22 | ii z = get_int(++c); 23 | if (addition)return (x.first + y.first == z.first); 24 | return (x.first - y.first == z.first); 25 | } 26 | int main(void){ 27 | int ans = 0; 28 | while(scanf("%s", s) != EOF) 29 | ans += correct(); 30 | printf("%d\n", ans); 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Solutions/UVA/11879 Multiple of 17.java: -------------------------------------------------------------------------------- 1 | package main; 2 | import java.util.Scanner; 3 | import java.math.BigInteger; 4 | import java.math.*; 5 | import java.lang.Integer; 6 | public class Main { 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | BigInteger a; 10 | while(true){ 11 | a = sc.nextBigInteger(); 12 | if (a.compareTo(BigInteger.ZERO) == 0) break; 13 | if (a.mod(BigInteger.valueOf(17)) == BigInteger.ZERO) System.out.println(1); 14 | else System.out.println(0); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Solutions/UVA/12004 Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | inline ll sum(ll n){ 5 | return (n*(n + 1LL)) >> 1LL; 6 | } 7 | int main() { 8 | int TC; scanf("%d", &TC); 9 | for (int tc = 1, n; tc <= TC; ++tc) 10 | { 11 | printf("Case %d: ", tc); 12 | scanf("%d", &n); 13 | ll a = sum(n - 1); 14 | if (a & 1LL) printf("%lld/%d\n", a, 2); 15 | else printf("%lld\n", a >> 1LL); 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Solutions/UVA/12028 A Gift from the Setter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100100 3 | #define MOD 1000007LL 4 | using namespace std; 5 | typedef long long int ll; 6 | ll K, C; 7 | int n; 8 | ll a[MAX]; 9 | inline void generate(void) 10 | { 11 | a[0] %= MOD; 12 | for (int i = 1; i < n; ++i) 13 | { 14 | a[i] = (K * a[i - 1] + C) % MOD; 15 | } 16 | } 17 | inline ll solve(void) 18 | { 19 | ll ans = 0; 20 | sort(a, a + n); 21 | ll prev_sum = a[0]; 22 | for (int i = 1; i < n; ++i) 23 | { 24 | ans += a[i] * i - prev_sum; 25 | prev_sum += a[i]; 26 | } 27 | return ans; 28 | } 29 | int main(void) 30 | { 31 | int TC; scanf("%d", &TC); 32 | for (int tc = 1; tc <= TC; ++tc) 33 | { 34 | scanf("%lld%lld%d%lld", &K, &C, &n, a); 35 | generate(); 36 | printf("Case %d: %lld\n", tc, solve()); 37 | } 38 | return 0; 39 | } 40 | /* 41 | 2 42 | 1 1 2 1 43 | 10 10 10 5 44 | */ 45 | 46 | -------------------------------------------------------------------------------- /Solutions/UVA/12114 Bachelor Arithmetic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int b, s; 4 | int main(void) 5 | { 6 | for(int tc = 1; scanf("%d%d", &b, &s), b || s; ++tc) 7 | { 8 | printf("Case %d: :-", tc); 9 | if (b == 1) 10 | { 11 | printf("\\"); 12 | } 13 | else 14 | { 15 | if (s >= b) 16 | { 17 | printf("|"); 18 | } 19 | else 20 | { 21 | printf("("); 22 | } 23 | } 24 | 25 | printf("\n"); 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Solutions/UVA/12155 ASCII Diamond.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n, r1, r2, c1, c2; 4 | int main(void) 5 | { 6 | for (int tc = 1; scanf("%d", &n), n; ++tc) 7 | { 8 | printf("Case %d:\n", tc); 9 | scanf("%d%d%d%d", &r1, &c1, &r2, &c2); 10 | const int len = (n << 1) - 1; 11 | for (int i = r1; i <= r2; ++i) 12 | { 13 | for (int j = c1; j <= c2; ++j) 14 | { 15 | const int dist = (abs(n - 1 - (i % len)) + abs(n - 1 - (j % len))); 16 | printf("%c", dist >= n ? '.' : 'a' + (dist % 26)); 17 | } 18 | printf("\n"); 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/12210 A Match Making Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 10005 5 | using namespace std; 6 | int b, s; 7 | int main(void){ 8 | // freopen("Input.txt", "r", stdin); 9 | int tc = 0; 10 | while(scanf("%d%d", &b, &s), b || s){ 11 | printf("Case %d: ", ++tc); 12 | int ans = INT_MAX; 13 | int x; 14 | for (int i = 0; i < b; ++i) 15 | scanf("%d", &x), ans = min(ans, x); 16 | for (int i = 0; i < s; ++i) 17 | scanf("%d", &x); 18 | if (b <= s) printf("0\n"); 19 | else printf("%d %d\n", b - s, ans); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Solutions/UVA/12293 Box Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int MAX = (1e9 + 1.0); 4 | vectorlose; 5 | inline void preprocess(void) 6 | { 7 | lose.push_back(1); 8 | while(lose.back() < MAX) 9 | { 10 | lose.push_back((lose.back() << 1)+ 1); 11 | } 12 | } 13 | int main(void) 14 | { 15 | preprocess(); 16 | for (int n; scanf("%d", &n), n;) 17 | { 18 | printf("%s\n", binary_search(lose.begin(), lose.end(), n) ? "Bob" : "Alice"); 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Solutions/UVA/12318 Digital Roulette.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lamiiine/Competitive-Programming-1/71e93f8d128c48f5fe2bead843f639c3b6b80b93/Solutions/UVA/12318 Digital Roulette.cpp -------------------------------------------------------------------------------- /Solutions/UVA/12324 Philip J. Fry Problem .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 103 3 | using namespace std; 4 | int t[MAX], b[MAX]; 5 | int memo[MAX][MAX*MAX]; 6 | int n; 7 | inline int dp(const int i, const int j) 8 | { 9 | int & ret = memo[i][j]; 10 | if (ret != -1) 11 | return ret; 12 | if (i == n) 13 | return ret = 0; 14 | return ret = min(t[i] + dp(i + 1, j + b[i]), !j ? INT_MAX : (t[i] >> 1) + dp(i + 1, j + b[i] - 1)); 15 | } 16 | inline void read(void) 17 | { 18 | for (int i = 0; i < n; ++i) 19 | scanf("%d%d", t + i, b + i); 20 | } 21 | int main(void) 22 | { 23 | while(scanf("%d", &n), n) 24 | { 25 | read(); 26 | memset(memo, 0xFF, sizeof memo); 27 | printf("%d\n", dp(0, 0)); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Solutions/UVA/12405 Scarecow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 105 3 | using namespace std; 4 | char s[MAX]; 5 | int n; 6 | int main(void){ 7 | // freopen("Input.txt", "r", stdin); 8 | int TC; 9 | scanf("%d", &TC); 10 | for (int tc = 1; tc <= TC; ++tc){ 11 | int ans = 0; 12 | scanf("%d%s", &n, s); 13 | for (int i = 0; i < n;){ 14 | // you need to cover i 15 | if (s[i] == '#'){++i; continue;} 16 | // cover it with the one after 17 | ++ans; 18 | i += 3; 19 | } 20 | printf("Case %d: %d\n", tc, ans); 21 | } 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Solutions/UVA/12428 Enemy At The Gates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | ll n, m; 5 | inline bool can(const ll k) 6 | { 7 | // k critical roads 8 | const ll nn = n - k; 9 | const ll mm = m - k; 10 | 11 | return ((ll)((ll)nn * (nn - 1LL)) >> 1LL) >= mm; 12 | } 13 | 14 | int main(void) 15 | { 16 | int TC; scanf("%d", &TC); 17 | while(TC--) 18 | { 19 | scanf("%lld%lld", &n, &m); 20 | ll lo = 0LL, hi = (ll) min(m, n - 1); 21 | while(lo < hi) 22 | { 23 | const ll mid = max((lo + hi) >> 1LL, lo + 1LL); 24 | if (can(mid)) 25 | { 26 | lo = mid; 27 | } 28 | else 29 | { 30 | hi = mid - 1LL; 31 | } 32 | } 33 | 34 | printf("%lld\n", lo); 35 | } 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Solutions/UVA/12444 Bits and Pieces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define LSOne(s) (s & (-s)) 3 | using namespace std; 4 | int A, B; 5 | bool solve(const int c, const int d){ 6 | // c is the result of anding 7 | // c is the result of oring 8 | int a = c, b = c; 9 | // For sure, you have all the ones in c and all the zeros in d 10 | // What you are not sure about is the bits are on in d and off in c 11 | if (c & (~d)) return false; 12 | int diff = d &(~c); 13 | while(true){ 14 | int p = LSOne(diff); 15 | if (p == diff) {b += p;break;} 16 | a += p, diff -= p; 17 | } 18 | A = a, B = b; 19 | return true; 20 | } 21 | int main(void){ 22 | // freopen("Input.txt", "r", stdin); 23 | int TC; 24 | scanf("%d", &TC); 25 | for (int x, y; TC--;){ 26 | scanf("%d%d", &x, &y); 27 | if (solve(x, y)) 28 | printf("%d %d\n", A, B); 29 | else printf("-1\n"); 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Solutions/UVA/12455.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int p, x[21]; 7 | bool find(int y, int i){ // start looking at y from index i and backward 8 | if (y == 0) return true; 9 | if (y < 0) return false; 10 | while (x[i] > y && i >= 0) i--; 11 | if (i < 0) return false; 12 | for (int j = i; j >= 0; j--) 13 | if (find(y-x[j], j-1)) return true; 14 | return false; 15 | } 16 | int main(){ 17 | freopen("Input.txt", "r", stdin); 18 | freopen("Output.txt", "w", stdout); 19 | int TC; 20 | scanf("%d", &TC); 21 | int n; 22 | int total; 23 | while (TC--){ 24 | scanf("%d%d", &n, &p); 25 | total = 0; 26 | for (int i = 0; i < p; i++) 27 | scanf("%d", &x[i]), total += x[i]; 28 | sort(x, x + p); 29 | if (find(total - n, p-1)) printf("YES\n"); else printf("NO\n"); 30 | } 31 | } -------------------------------------------------------------------------------- /Solutions/UVA/12461 Airplane_2 (figure out the formula).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | int n; 5 | while(scanf("%d", &n), n) 6 | { 7 | printf("1/2\n"); 8 | } 9 | return 0; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Solutions/UVA/12482 Short Story Competition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 1005 4 | using namespace std; 5 | int a[MAX]; 6 | int n, l, c; 7 | int main(void){ 8 | // freopen("Input.txt", "r", stdin); 9 | char s[75]; 10 | while(scanf("%d%d%d", &n, &l, &c) != EOF){ 11 | for (int i = 0; i < n; ++i) 12 | scanf("%s", s), a[i] = strlen(s); 13 | int ans = 0; 14 | for (int i = 0; i < n;){ 15 | ++ans; // take an extra line! 16 | int temp = c; 17 | while(temp >= a[i])temp -= (a[i++] + 1); 18 | } 19 | printf("%d\n", (ans + l - 1)/l); // number of pages 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Solutions/UVA/536 Tree Recovery.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main 4 | { 5 | static Scanner sc = new Scanner(System.in); 6 | static String pre, in; 7 | static void parse(final int pre_i, final int pre_j, final int in_i, final int in_j) 8 | { 9 | if (pre_i >= pre_j) return; 10 | final int k = in.indexOf(pre.charAt(pre_i), in_i) - in_i; // number of characters in left subtree 11 | parse(pre_i + 1, pre_i + k + 1, in_i, in_i + k); 12 | parse(pre_i + k + 1, pre_j, in_i + k + 1, in_j); 13 | System.out.print(pre.charAt(pre_i)); 14 | } 15 | public static void main(String[] args) 16 | { 17 | while(sc.hasNext()) 18 | { 19 | String[] line = sc.nextLine().split(" "); 20 | pre = line[0]; in = line[1]; 21 | parse(0, pre.length(), 0, in.length()); 22 | System.out.println(); 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Solutions/UVA/562 Dividing coins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 102 3 | using namespace std; 4 | int arr[MAX], acc[MAX], n, tot; 5 | int memo[MAX][MAX*500]; 6 | inline int dp(const int i, const int j) 7 | { 8 | int & ret = memo[i][j]; 9 | if (ret != -1) return ret; 10 | if (i == n) return ret = abs((tot - j) - j); 11 | return ret = min(dp(i + 1, j), dp(i + 1, j + arr[i])); 12 | } 13 | 14 | int main(void) 15 | { 16 | int TC; scanf("%d", &TC); 17 | while(TC--) 18 | { 19 | memset(memo, -1, sizeof memo); 20 | scanf("%d", &n); 21 | for (int i = 0; i < n; ++i) 22 | scanf("%d", arr + i); 23 | tot = 0; 24 | for (int i = 0; i < n; ++i) 25 | tot += arr[i]; 26 | printf("%d\n", dp(0, 0)); 27 | } 28 | } 29 | /* 30 | 2 31 | 3 32 | 2 3 5 33 | 4 34 | 1 2 4 6 35 | */ 36 | 37 | -------------------------------------------------------------------------------- /Solutions/UVA/575 Skew Binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int ll; 4 | char s[1000]; 5 | ll parse(void){ 6 | ll n = strlen(s); 7 | ll ans = 0LL; 8 | for (ll i = 0; i < n; ++i) 9 | ans += (ll)(s[i] - '0')*((1LL << (n - i)) - 1LL); 10 | return ans; 11 | } 12 | int main(void){ 13 | while(scanf("%s", s), strcmp(s, "0")) 14 | printf("%lld\n", parse()); 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Solutions/UVA/673 Parentheses Balance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX (1 << 8) 3 | using namespace std; 4 | char s[MAX]; 5 | char stk[MAX]; 6 | int top; 7 | inline char inverse(char t){ 8 | return t == ')' ? '(' : '['; 9 | } 10 | inline bool correct(void){ 11 | top = 0; 12 | for (char * c = s;; ++c){ 13 | char t = *c; 14 | if (t == '\n') break; 15 | if (t == '(' || t == '[') 16 | stk[top++] = t; 17 | else if (!top || stk[--top] != inverse(t)) return false; 18 | } 19 | return !top; 20 | } 21 | int main(void){ 22 | int TC; fgets(s, INT_MAX, stdin); sscanf(s, "%d", &TC); 23 | while(TC--){ 24 | fgets(s, INT_MAX, stdin); 25 | printf("%s\n", correct() ? "Yes" : "No"); 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/UVA/756 - Biorhythms.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int p, e, i; 5 | inline bool invalid(const int today) 6 | { 7 | return (today - p) % 23 || (today - e) % 28 || (today - i) % 33; 8 | } 9 | int main() 10 | { 11 | for(int TC = 1, d; scanf("%d%d%d%d", &p, &e, &i, &d), ~p || ~e || ~i || ~d; ++TC) 12 | for (int i = d + 1; i - d <= 21252; ++i) 13 | if (!invalid(i)) 14 | { 15 | printf("Case %d: the next triple peak occurs in %d days.\n", TC, i - d); 16 | break; 17 | } 18 | return 0; 19 | } 20 | 21 | --------------------------------------------------------------------------------