├── .gitignore ├── README.md ├── lib ├── DataStructures │ ├── BIT.hpp │ ├── BIT2D.hpp │ ├── DSU.hpp │ ├── MinQueue.hpp │ ├── OrderedSet.hpp │ ├── SegTree.hpp │ ├── SegTree2D.hpp │ ├── SparseTable.hpp │ └── WaveletTree.hpp ├── Geometry │ ├── Point.hpp │ └── PointUtils.hpp ├── Graphs │ ├── Dinic.hpp │ ├── DirectedMST.hpp │ ├── LCA.hpp │ ├── MCMF.hpp │ └── TwoSat.hpp ├── Math │ ├── FFT.hpp │ ├── FWHT.hpp │ ├── Karatsuba.hpp │ ├── MillerRabin.hpp │ ├── Mobius │ └── Mobius.hpp ├── Strings │ ├── Hash.hpp │ ├── KMP.hpp │ ├── SuffixAutomaton.hpp │ └── Z.hpp ├── Utils │ ├── CoordCompress.hpp │ ├── Debug.hpp │ ├── Mint.hpp │ └── int128.hpp ├── misc │ ├── environment │ │ ├── README.md │ │ ├── bash.sh │ │ ├── clion_template.cpp │ │ ├── clion_template_single.cpp │ │ ├── old_bash.sh │ │ ├── old_clion_template.cpp │ │ ├── prep.sh │ │ ├── template.cpp │ │ └── template2.cpp │ └── java │ │ ├── InputReader.java │ │ └── OutputWriter.java └── todo │ ├── DP-Tricks │ └── ConvexHullTrick.cpp │ ├── Data-Structures │ ├── 0.in │ ├── BIT2D.cpp │ ├── Makefile │ ├── MergeSortTree │ ├── MergeSortTree.cpp │ ├── SegTree.cpp │ ├── SegTree2D.hpp │ ├── SegTreeBeats.cpp │ ├── SegTreePersistent.cpp │ ├── SegTreeSparse.cpp │ ├── Treap │ ├── Treap.cpp │ └── gen.py │ ├── Geometry │ ├── Circle.cpp │ └── Point3D.hpp │ ├── Graphs │ ├── Graph.cpp │ ├── Tree.cpp │ └── scc.cpp │ ├── Math │ ├── Factor │ ├── Factor.cpp │ └── Polynomial.cpp │ ├── Strings │ ├── 0.in │ ├── SuffixAutomaton │ ├── SuffixAutomaton.cpp │ └── Trie.cpp │ └── misc │ └── frac.cpp └── problems ├── gyms ├── ACPC 2018 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── F.cpp │ ├── I.cpp │ ├── K.cpp │ └── L.cpp ├── Arabella 2018 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ └── K.cpp ├── BAPC 2017 │ ├── A.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── I.cpp │ ├── K.cpp │ ├── L.cpp │ └── M.cpp ├── JUST 4.0 2017 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ └── K.cpp ├── Jakarta 2018 │ ├── A.cpp │ ├── D.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ └── L.cpp ├── Latin American Regional 2020 │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F (WA).cpp │ ├── H.cpp │ ├── I (WA).cpp │ ├── J.cpp │ ├── K.cpp │ ├── L.cpp │ ├── N.cpp │ └── statements.pdf ├── Latin American Subregional 2019 │ ├── A.cpp │ ├── B.cpp │ ├── D.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ ├── L.cpp │ └── M.cpp ├── Latin American Subregional 2020 │ ├── A.cpp │ ├── B.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── L.cpp │ ├── M.cpp │ └── N.cpp ├── MFP 2023 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ └── J.cpp ├── NCPC 2015 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ └── G.cpp ├── NCPC 2016 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ └── J.cpp ├── NCPC 2018 │ ├── B.cpp │ ├── C.cpp │ ├── E.cpp │ ├── H.cpp │ ├── I.java │ ├── J.cpp │ └── K.cpp ├── NEERC 2017 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── E.cpp ├── Pacific Northwest 2019-2020 Div. 1 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── I.cpp │ ├── L.cpp │ └── M.cpp ├── SWERC 2019 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ └── K.cpp ├── Samara Intercollegiate 2015 │ ├── A.cpp │ ├── B.cpp │ ├── E.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ ├── K.cpp │ └── M.cpp ├── Swiss Subregional 2017 │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ └── K.cpp ├── Tsukuba 2015 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── I.cpp │ └── J.cpp ├── Tsukuba 2017 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ └── I.cpp ├── left.txt └── list.txt └── solutions ├── AtCoder ├── ABC137 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ └── F.cpp ├── ABC148 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.py │ └── F.cpp ├── ABC155 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ └── F.cpp ├── ABC168 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp ├── ABC169 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ └── F.cpp ├── ABC173 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── E2.cpp │ └── F.cpp └── DP │ ├── A │ ├── A.cpp │ ├── A.dSYM │ └── Contents │ │ ├── Info.plist │ │ └── Resources │ │ └── DWARF │ │ └── A │ ├── B │ ├── B.cpp │ ├── B.dSYM │ └── Contents │ │ ├── Info.plist │ │ └── Resources │ │ └── DWARF │ │ └── B │ ├── C │ ├── C.cpp │ └── C.dSYM │ └── Contents │ ├── Info.plist │ └── Resources │ └── DWARF │ └── C ├── Brazilian Summer School ├── Summer School 2018 │ └── Flow │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── C.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ ├── F.cpp │ │ ├── G.cpp │ │ ├── H.cpp │ │ ├── I.cpp │ │ ├── J.cpp │ │ └── K.cpp ├── Summer School 2019 │ ├── Dia 1 │ │ ├── A.py │ │ ├── B.cpp │ │ ├── C.cpp │ │ ├── D.py │ │ └── M.cpp │ ├── Dia 2 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ ├── F.cpp │ │ ├── G.cpp │ │ └── H.cpp │ ├── Dia 3 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── C.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ ├── F.cpp │ │ ├── G.cpp │ │ └── H.cpp │ ├── Dia 4 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── F.cpp │ │ └── I.cpp │ ├── Dia 5 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── H.cpp │ │ └── I.cpp │ ├── Dia 6 │ │ ├── D.cpp │ │ ├── E.cpp │ │ ├── G.cpp │ │ ├── H.cpp │ │ ├── I.cpp │ │ └── J.cpp │ ├── Dia 7 │ │ ├── A.cpp │ │ ├── C.cpp │ │ ├── E.cpp │ │ ├── F.cpp │ │ ├── G.cpp │ │ ├── I.cpp │ │ └── K.cpp │ ├── Dia 8 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── C.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ ├── F.cpp │ │ ├── G.cpp │ │ ├── H.cpp │ │ ├── I.cpp │ │ └── J.cpp │ ├── Dia 9 │ │ ├── C.cpp │ │ ├── G.cpp │ │ ├── H.cpp │ │ └── I.cpp │ └── J.cpp └── Summer School 2020 │ ├── 01-ICL 2015 │ ├── B.cpp │ ├── E │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J │ ├── J.cpp │ └── L.cpp │ ├── 02-Flow │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ ├── J.cpp │ └── in │ ├── 03-Russian Team Olympiad │ ├── D │ └── D.cpp │ └── Dinic.cpp ├── CUCEI └── 4o Concurso Club de Algoritmia CUCEI │ ├── B.cpp │ ├── E.cpp │ ├── F.cpp │ ├── J.cpp │ ├── L.cpp │ └── O.cpp ├── Codechef ├── December Challenge 2018 │ ├── CHFIDEAL.cpp │ ├── CHFINTRO.cpp │ ├── INTXOR.cpp │ └── MAXEP.cpp ├── June Challenge 2020 │ ├── CONTAIN.cpp │ ├── COVDSMPL │ ├── COVDSMPL.cpp │ ├── GUESSG │ ├── GUESSG.cpp │ ├── Makefile │ ├── T2.cpp │ ├── TTUPLE │ ├── TTUPLE.cpp │ ├── brute │ ├── brute.cpp │ ├── gen │ ├── gen.cpp │ ├── gen.py │ ├── go.sh │ ├── in │ ├── in2 │ ├── o1 │ └── o2 └── Snackdown 2018 │ ├── Online Qualifier │ ├── CHEFPRMS.cpp │ ├── QABC.cpp │ ├── QUALPREL.cpp │ └── SPEAD2.cpp │ ├── Online Round 1A │ ├── ARRGRAPH.cpp │ ├── AVGMAT.cpp │ ├── CARDMGK.cpp │ ├── CHEFADD.cpp │ ├── PERIODIC.cpp │ └── TYPING.cpp │ └── Online Round 1B │ ├── CHFAR.cpp │ └── SNCKYEAR.cpp ├── GEMA ├── GEMA Flows │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp ├── GEMA Long Challenge │ ├── GEMA Long Challenge 1 │ │ ├── C.cpp │ │ ├── G.cpp │ │ ├── G.in │ │ ├── J.cpp │ │ └── K.cpp │ ├── GEMA Long Challenge 2 │ │ ├── A.cpp │ │ ├── E.cpp │ │ └── H.cpp │ ├── GEMA Long Challenge 3 │ │ ├── H.cpp │ │ └── I.cpp │ ├── GEMA Long Challenge 4 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ └── J.cpp │ ├── GEMA Long Challenge 5 │ │ ├── A.cpp │ │ ├── B.py │ │ ├── C.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ ├── F.cpp │ │ ├── G.cpp │ │ ├── H.cpp │ │ ├── I.cpp │ │ ├── J.cpp │ │ └── K.cpp │ └── GEMA Long Challenge 6 │ │ ├── F.cpp │ │ ├── I │ │ ├── I.cpp │ │ ├── in │ │ └── in2 ├── GEMA Strings │ ├── GEMA Strings 00 │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── GEMA Strings 01 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── C.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ ├── F.cpp │ │ └── G.cpp │ ├── GEMA Strings 02 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── C.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ ├── F.cpp │ │ └── G.cpp │ └── GEMA Strings 03 │ │ ├── B.cpp │ │ ├── D.cpp │ │ └── E.cpp └── GEMA Tree │ └── 1 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D │ ├── D.cpp │ └── in ├── Google ├── Google Codejam 2019 │ ├── 1C │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── B2.cpp │ │ ├── C.cpp │ │ ├── interactive_runner.py │ │ └── testing_tool.py │ ├── A.cpp │ ├── A.py │ ├── B.cpp │ └── C.cpp ├── Google Codejam 2020 │ ├── 1A │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── 1B │ │ ├── A │ │ ├── A.cpp │ │ ├── A2 │ │ ├── A2.cpp │ │ └── in │ ├── 1C │ │ ├── A │ │ ├── A.cpp │ │ ├── B │ │ ├── B.cpp │ │ ├── B2 │ │ ├── B2.cpp │ │ ├── B3.cpp │ │ ├── C │ │ ├── C$FastReader.class │ │ ├── C.class │ │ ├── C.cpp │ │ ├── C.java │ │ ├── C.py │ │ ├── gen │ │ ├── gen.cpp │ │ └── in │ └── Qualification │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── C.cpp │ │ └── E.cpp └── Hash Code 2021 │ ├── HashCode2021.tar.gz │ └── Practice │ ├── .out │ ├── A.cpp │ ├── Makefile │ ├── cases │ ├── a.in │ ├── b.in │ ├── c.in │ ├── d.in │ └── e.in │ └── statements.pdf ├── HackerCup └── 2020 │ ├── Qualification │ ├── A.cpp │ ├── B.cpp │ └── C.cpp │ ├── Round 1 │ ├── A1.cpp │ ├── A2.cpp │ ├── A3.cpp │ └── B.cpp │ └── Round 2 │ ├── A │ ├── A │ ├── A.cpp │ ├── full.in │ ├── full.out │ ├── out │ ├── sample.in │ ├── sample.out │ ├── sol.cpp │ ├── val.in │ └── val.out │ └── B │ ├── B │ ├── B.cpp │ ├── B2 │ ├── B2.cpp │ ├── B3.cpp │ ├── in │ ├── in1 │ ├── sample.in │ ├── sample.out │ ├── stupid │ └── stupid.cpp ├── Hackerearth └── FebEasy │ └── goodsub.cpp ├── Lab de Algoritmos 2 2020 ├── 01 │ ├── A.cpp │ ├── B.java │ ├── C.java │ └── D.cpp ├── 02 │ ├── A.java │ ├── B.java │ └── C.java └── 03 │ ├── B.cpp │ └── B │ ├── 0.in │ ├── 0.out │ ├── 1.in │ ├── 2.in │ ├── out │ ├── sol │ ├── sol.cpp │ ├── sol2 │ ├── sol2.cpp │ └── test.sh ├── Lab de Algoritmos Avancados 2019 ├── 10 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── 11 │ ├── 1.cpp │ └── 2.cpp ├── 12 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── 13 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── 01 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ └── 4.cpp ├── 02 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ └── 4.cpp ├── 03 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ └── 4.cpp ├── 04 │ ├── 1.cpp │ └── 2.cpp ├── 05 │ └── 1.cpp ├── 06 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── 07 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── 08 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp └── 09 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ └── 4.cpp ├── MAPS └── MAPS20 │ ├── B.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ └── J.py ├── Neps ├── Contest I │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── E.cpp └── Liga de Programação 2020 │ ├── Etapa 1 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ └── F.cpp │ ├── Etapa 2 │ ├── A │ ├── A.cpp │ ├── B │ ├── B.cpp │ ├── C │ ├── C.cpp │ ├── C2.cpp │ ├── D │ ├── D.cpp │ ├── D2 │ ├── E │ ├── E.cpp │ ├── E.in1 │ ├── E.in2 │ ├── E.in3 │ ├── F │ ├── F.cpp │ ├── gen.py │ ├── in │ ├── in2 │ ├── o │ └── ou │ └── Etapa 4 │ ├── A │ ├── A.cpp │ ├── B │ ├── B.cpp │ ├── C │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F │ ├── F.cpp │ ├── G.cpp │ └── in ├── Stanford ProCo 2015 ├── N1.cpp ├── N2.cpp ├── N3.cpp ├── N4.cpp ├── N5.cpp └── N8.cpp ├── Training Camp Argentina └── 6 │ ├── B.py │ ├── D.java │ ├── G.cpp │ ├── G.java │ ├── G.py │ └── I.cpp ├── UFSCAR └── Desafio de Programadores 2019 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ └── L.cpp ├── USACO ├── Bronze 2020 │ ├── A.cpp │ ├── B.cpp │ └── C.cpp ├── Gold 2015 │ ├── in │ ├── in2 │ ├── out2 │ ├── palpath.java │ ├── trapped │ └── trapped.cpp ├── Gold 2020 │ ├── A.cpp │ ├── B.cpp │ ├── C │ ├── C.cpp │ ├── C2 │ ├── C2.cpp │ └── in └── Silver 2020 │ ├── A.cpp │ ├── B.cpp │ └── C.cpp ├── USP-ICMC ├── USP-ICMC 2016 │ └── F.cpp ├── USP-ICMC 2017 │ ├── A.cpp │ └── J.cpp ├── USP-ICMC 2018 │ ├── D.cpp │ ├── F.cpp │ ├── I.cpp │ └── L.cpp └── USP-ICMC 2019 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── F.cpp │ └── J.cpp ├── USP-IME ├── 2017 USP Try-outs │ └── F.cpp └── 2019 USP Try-outs │ ├── A.cpp │ ├── C.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ └── K.cpp ├── UVA ├── 10196.cpp ├── 10699.cpp ├── 10911.cpp ├── 11022.cpp ├── 1172.cpp ├── 12003.cpp ├── 1230.cpp ├── 374.cpp ├── 455.cpp ├── 524.cpp ├── 6525.cpp ├── 974.cpp └── in ├── Uri ├── Contest de Natal 2019 │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ ├── E.cpp │ ├── F.cpp │ ├── G.cpp │ ├── H.cpp │ ├── I.cpp │ └── J.cpp ├── Maratona do Norte │ ├── B.cpp │ ├── C.cpp │ ├── H.cpp │ ├── K.cpp │ └── L.cpp └── SAP 2020 │ ├── A │ ├── A.cpp │ ├── B │ ├── B.cpp │ ├── B2 │ ├── B2.cpp │ ├── C │ ├── C.cpp │ ├── D │ ├── D.cpp │ ├── G │ ├── G.cpp │ ├── H │ ├── H.cpp │ ├── J │ ├── J.cpp │ ├── J2 │ ├── J2.cpp │ ├── K │ ├── K.cpp │ ├── L │ ├── L.cpp │ ├── in │ └── in2 └── VTEX └── VTEX Code Cup 2019 ├── A.cpp ├── B.cpp ├── C.cpp ├── F.cpp └── G.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # CLion and JHelper information 2 | .DS_Store 3 | .idea 4 | cmake-build-debug 5 | CMakeLists.txt 6 | output 7 | tasks 8 | testrunner 9 | run.template 10 | submission.template 11 | task.template 12 | 13 | # Another files 14 | problems/output 15 | problems/contest -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # competitive-programming 2 | 3 | Codes and more for competitive programming! 4 | -------------------------------------------------------------------------------- /lib/DataStructures/BIT.hpp: -------------------------------------------------------------------------------- 1 | /** Binary Indexed Tree, aka Fenwick Tree */ 2 | template 3 | struct BIT { 4 | std::vector bt; 5 | int n; 6 | 7 | BIT(int n) : n{n} { 8 | bt.resize(n + 2); 9 | } 10 | 11 | inline void init(int sz) { 12 | n = sz; 13 | bt.assign(n + 2, 0); 14 | } 15 | 16 | inline void update(int i, T k) { 17 | for (i++; i <= n + 1; i += i&-i) 18 | bt[i] += k; 19 | } 20 | 21 | inline T query(int i) { 22 | T ret{}; 23 | for (i++; i; i -= i&-i) 24 | ret += bt[i]; 25 | return ret; 26 | } 27 | 28 | inline T query(int l, int r) { 29 | if (l > r) return 0; 30 | return query(r) - query(l-1); 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /lib/DataStructures/BIT2D.hpp: -------------------------------------------------------------------------------- 1 | template 2 | struct Bit2D { 3 | int n, m; 4 | vector> b; 5 | 6 | Bit2D(int n_, int m_) : 7 | n(n_+2), m(m_+2), b(n, vector(m)) 8 | {} 9 | 10 | // adds delta to position (x, y) 11 | auto add(int x, int y, T delta) -> void { 12 | x++, y++; 13 | for (int i = x; i < n; i += i&-i) { 14 | for (int j = y; j < n; j += j&-j) { 15 | b[i][j] += delta; 16 | } 17 | } 18 | } 19 | 20 | // returns sum of rectangle [0, x] x [0, y] 21 | auto get(int x, int y) -> T { 22 | x++, y++; 23 | T ans = 0; 24 | for (int i = x; i; i -= i&-i) { 25 | for (int j = y; j; j -= j&-j) { 26 | ans += b[i][j]; 27 | } 28 | } 29 | return ans; 30 | } 31 | 32 | // returns sum of rectagle defined by [x1, x2] x [y1, y2] 33 | // generalizable to N-dimensional BIT using inclusion-exclusion principle 34 | auto get_rectangle(int x1, int x2, int y1, int y2) -> T { 35 | return get(x2, y2) - get(x1-1, y2) - get(x2, y1-1) + get(x1-1, y1-1); 36 | } 37 | }; -------------------------------------------------------------------------------- /lib/DataStructures/MinQueue.hpp: -------------------------------------------------------------------------------- 1 | template> 2 | struct MinQueue { 3 | constexpr static auto cmp = Cmp{}; 4 | 5 | deque> q; 6 | int l = 0, r = 0; 7 | 8 | auto push(T const& val) -> void { 9 | while (!q.empty() and !cmp(q.back().first,val)) { 10 | q.pop_back(); 11 | } 12 | q.emplace_back(val, r++); 13 | } 14 | 15 | auto pop() -> void { 16 | if (!q.empty() and q.front().second == l) { 17 | q.pop_front(); 18 | } 19 | l++; 20 | } 21 | 22 | auto get() -> T { 23 | return q.front().first; 24 | } 25 | 26 | auto empty() -> bool { 27 | return q.empty(); 28 | } 29 | 30 | auto size() -> int { 31 | return r-l; 32 | } 33 | }; -------------------------------------------------------------------------------- /lib/DataStructures/OrderedSet.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | template using ord_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 5 | /** Instructions: 6 | * If you want to use it like a set: ord_set st; 7 | * If you want to use it like a map: ord_set mp; 8 | * ====== Functions ====== 9 | * insert, erase, lower_bound, upper_bound, find, just like normal set 10 | * s.order_of_key(k); -----> qtt of strictly smaller than k, O(logN) 11 | * s.find_by_order(k); ----> iterator to k-th element counting from 0, in O(logN) 12 | */ -------------------------------------------------------------------------------- /lib/DataStructures/SparseTable.hpp: -------------------------------------------------------------------------------- 1 | template 2 | struct SparseTable { 3 | int n; 4 | std::vector> mat; 5 | 6 | // Function to apply range query 7 | inline static T f(T x, T y) { 8 | return min(x, y); 9 | } 10 | 11 | SparseTable(std::vector const& a) { 12 | n = a.size(); 13 | const int max_log = 32 - __builtin_clz(n); 14 | mat.resize(max_log); 15 | mat[0] = a; 16 | for (int j = 1; j < max_log; j++) { 17 | mat[j].resize(n - (1 << j) + 1); 18 | for (int i = 0; i <= n - (1 << j); i++) { 19 | mat[j][i] = f(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]); 20 | } 21 | } 22 | } 23 | 24 | inline T query(int from, int to) const { 25 | assert(0 <= from && from <= to && to <= n - 1); 26 | int lg = 32 - __builtin_clz(to - from + 1) - 1; 27 | return f(mat[lg][from], mat[lg][to - (1 << lg) + 1]); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /lib/Math/Mobius: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/Math/Mobius -------------------------------------------------------------------------------- /lib/Math/Mobius.hpp: -------------------------------------------------------------------------------- 1 | namespace Mobius { 2 | /** Möbius function 3 | - mu(n) = 1 if n is square-free w/ even prime factors 4 | - mu(n) = -1 if n is square-free w/ odd prime factors 5 | - mu(n) = 0 if has squared prime factor 6 | (n is square-free if each prime appears only once) 7 | */ 8 | vector init_mu(const int MAXN) { 9 | vector mu(MAXN); 10 | mu[1] = 1; 11 | for (int i = 1; i < MAXN; i++) 12 | for (int j = i + i; j < MAXN; j += i) 13 | mu[j] -= mu[i]; 14 | return mu; 15 | } 16 | } -------------------------------------------------------------------------------- /lib/Strings/KMP.hpp: -------------------------------------------------------------------------------- 1 | template 2 | std::vector prefixKMP(T const& v) { 3 | int n = v.size(); 4 | std::vector pi(n); 5 | for (int i = 1; i < n; i++) { 6 | pi[i] = pi[i-1]; 7 | while(pi[i] > 0 && v[pi[i]] != v[i]) 8 | pi[i] = pi[pi[i]-1]; 9 | if (v[i] == v[pi[i]]) pi[i]++; 10 | } 11 | return pi; 12 | } 13 | -------------------------------------------------------------------------------- /lib/Strings/SuffixAutomaton.hpp: -------------------------------------------------------------------------------- 1 | /** Suffix automaton structure. O(N) to build */ 2 | struct SuffixAutomaton { 3 | struct State { 4 | int len, link; 5 | map next; 6 | }; 7 | 8 | vector st; 9 | int last; 10 | 11 | SuffixAutomaton(string const& s) { 12 | int n = s.size(); 13 | st.reserve(2 * n); 14 | 15 | st.push_back(State{0, -1}); 16 | last = 0; 17 | 18 | for (int i = 0; i < n; i++) { // extend current char 19 | st.push_back(State{i + 1, 0}); 20 | int r = (int) st.size() - 1; 21 | 22 | int p = last; 23 | while(p >= 0 && st[p].next.find(s[i]) == st[p].next.end()) { 24 | st[p].next[s[i]] = r; 25 | p = st[p].link; 26 | } 27 | 28 | if (p != -1) { 29 | int q = st[p].next[s[i]]; 30 | if (st[p].len + 1 == st[q].len) { 31 | st[r].link = q; 32 | } else { // split and add q' 33 | st.push_back(State{st[p].len + 1, st[q].link, st[q].next}); 34 | int qq = (int) st.size() - 1; 35 | 36 | st[q].link = st[r].link = qq; 37 | while(p >= 0 && st[p].next[s[i]] == q) { 38 | st[p].next[s[i]] = qq; 39 | p = st[p].link; 40 | } 41 | } 42 | } 43 | 44 | last = r; 45 | } 46 | } 47 | }; -------------------------------------------------------------------------------- /lib/Strings/Z.hpp: -------------------------------------------------------------------------------- 1 | template 2 | std::vector buildZ(T const& v) { 3 | int n = v.size(); 4 | std::vector z(n); 5 | for (int i = 1, l = 0, r = 0; i < n; i++) { 6 | if (i <= r) z[i] = std::min(r - i + 1, z[i - l]); 7 | while(i + z[i] < n && v[z[i]+i] == v[z[i]]) z[i]++; 8 | if (z[i] + i - 1 > r) l = i, r = i + z[i] - 1; 9 | } 10 | return z; 11 | } 12 | -------------------------------------------------------------------------------- /lib/Utils/CoordCompress.hpp: -------------------------------------------------------------------------------- 1 | namespace CoordCompress { 2 | template inline void compress(vector& v) { 3 | sort(v.begin(), v.end()); 4 | v.erase(unique(v.begin(), v.end()), v.end()); 5 | } 6 | 7 | template inline size_t get_id(vector& v, T const&& val) { 8 | return lower_bound(v.begin(), v.end(), val) - v.begin(); 9 | } 10 | } -------------------------------------------------------------------------------- /lib/Utils/Debug.hpp: -------------------------------------------------------------------------------- 1 | string to_string(string s) { 2 | return '"' + s + '"'; 3 | } 4 | 5 | string to_string(const char* s) { 6 | return to_string((string) s); 7 | } 8 | 9 | string to_string(bool b) { 10 | return (b ? "true" : "false"); 11 | } 12 | 13 | template 14 | string to_string(pair p) { 15 | return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; 16 | } 17 | 18 | template 19 | string to_string(A v) { 20 | bool first = true; 21 | string res = "{"; 22 | for (const auto &x : v) { 23 | if (!first) { 24 | res += ", "; 25 | } 26 | first = false; 27 | res += to_string(x); 28 | } 29 | res += "}"; 30 | return res; 31 | } 32 | 33 | void debug_out() { cerr << endl; } 34 | 35 | template 36 | void debug_out(Head H, Tail... T) { 37 | cerr << " " << to_string(H); 38 | debug_out(T...); 39 | } 40 | 41 | #ifdef LOCAL_PC 42 | #define DBG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) 43 | #else 44 | #define DBG(...) 42 45 | #endif -------------------------------------------------------------------------------- /lib/Utils/Mint.hpp: -------------------------------------------------------------------------------- 1 | template 2 | struct Mint { 3 | int v; 4 | Mint() : v{} {} 5 | Mint(int v) : v(v) { go(); } 6 | inline void go() { if (abs(v) >= MOD) v %= MOD; if (v < 0) v += MOD; } 7 | 8 | inline static Mint fpow(Mint x, long long y) { // x ^ y 9 | Mint r = 1; 10 | for (; y > 0; y /= 2, x *= x) if (y%2 != 0) r *= x; 11 | return r; 12 | } 13 | Mint inv() const { return fpow(*this, MOD - 2); } 14 | Mint operator+(Mint const& m) const { return v + m.v >= MOD ? v + m.v - MOD : v + m.v; } 15 | Mint operator-(Mint const& m) const { return v - m.v < 0 ? v - m.v + MOD : v - m.v; } 16 | Mint operator*(Mint const& m) const { return (long long) v * m.v % MOD; } 17 | Mint operator/(Mint const& m) const { return (*this) * m.inv(); } 18 | Mint operator^(long long m) const { return fpow(*this, m); } 19 | Mint& operator+=(Mint const& m) { return (*this) = (*this) + m; } 20 | Mint& operator-=(Mint const& m) { return (*this) = (*this) - m; } 21 | Mint& operator*=(Mint const& m) { return (*this) = (*this) * m; } 22 | Mint& operator/=(Mint const& m) { return (*this) = (*this) / m; } 23 | }; -------------------------------------------------------------------------------- /lib/Utils/int128.hpp: -------------------------------------------------------------------------------- 1 | string to_string(__int128 x) { 2 | if (x == 0) return "0"; 3 | string s; bool sig = x < 0; x = x < 0 ? -x : x; 4 | while(x > 0) s += x % 10 + '0', x /= 10; 5 | if (sig) s += '-'; 6 | reverse(s.begin(), s.end()); 7 | return s; 8 | } 9 | 10 | ostream& operator<<(ostream& out, __int128 x) { 11 | return out << to_string(x); 12 | } 13 | 14 | istream& operator>>(istream& in, __int128& x) { 15 | char c; bool neg = false; while(isspace(c = in.get())); 16 | if(!isdigit(c)) neg = (c == '-'), x = 0; 17 | else x = c - '0'; 18 | while(isdigit(c = in.get())) x = (x << 3) + (x << 1) - '0' + c; 19 | x = neg ? -x : x; return in; 20 | } 21 | 22 | using int128 = __int128; -------------------------------------------------------------------------------- /lib/misc/environment/prep.sh: -------------------------------------------------------------------------------- 1 | if [ $# -eq 0 ]; then 2 | MAX_PROB=15 3 | else 4 | MAX_PROB=$(printf '%d' "'$1") 5 | MAX_PROB=$(( $MAX_PROB - 64 )) 6 | fi 7 | 8 | 9 | (mkdir "tasks") 2> /dev/null 10 | (mkdir "output") 2> /dev/null 11 | 12 | echo "Creating "$MAX_PROB" problems." 13 | for counter in $(seq 1 $MAX_PROB); do 14 | i=$(( $counter + 64 )); 15 | c=$(printf \\$(printf '%03o' $i)); 16 | c="tasks/"$c 17 | (mkdir $c) 2> /dev/null 18 | (cp "template.cpp" $c"/sol.cpp") 19 | (cp "bash.sh" $c"/test.sh") 20 | (chmod +x $c"/test.sh") 21 | echo "Created folder "$c 22 | done -------------------------------------------------------------------------------- /lib/todo/Data-Structures/BIT2D.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/todo/Data-Structures/BIT2D.cpp -------------------------------------------------------------------------------- /lib/todo/Data-Structures/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | python3 gen.py > 0.in 3 | \time ./MergeSortTree < 0.in 4 | make -------------------------------------------------------------------------------- /lib/todo/Data-Structures/MergeSortTree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/todo/Data-Structures/MergeSortTree -------------------------------------------------------------------------------- /lib/todo/Data-Structures/SegTreeBeats.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/todo/Data-Structures/SegTreeBeats.cpp -------------------------------------------------------------------------------- /lib/todo/Data-Structures/SegTreePersistent.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/todo/Data-Structures/SegTreePersistent.cpp -------------------------------------------------------------------------------- /lib/todo/Data-Structures/SegTreeSparse.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/todo/Data-Structures/SegTreeSparse.cpp -------------------------------------------------------------------------------- /lib/todo/Data-Structures/Treap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/todo/Data-Structures/Treap -------------------------------------------------------------------------------- /lib/todo/Data-Structures/gen.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | n = int(1e5) 4 | m = int(5e3) 5 | print(n, m) 6 | 7 | for i in range(n): 8 | print(randint(int(-1e9), int(1e9)), end=' ') 9 | print() 10 | 11 | for i in range(n): 12 | # l = randint(1, n) 13 | # r = randint(l, n) 14 | # k = randint(1, r - l + 1) 15 | l, r, k = 1, n, n 16 | print(l, r, k) -------------------------------------------------------------------------------- /lib/todo/Math/Factor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/todo/Math/Factor -------------------------------------------------------------------------------- /lib/todo/Math/Polynomial.cpp: -------------------------------------------------------------------------------- 1 | template 2 | struct Poly { 3 | vector coef; 4 | 5 | 6 | 7 | 8 | }; -------------------------------------------------------------------------------- /lib/todo/Strings/0.in: -------------------------------------------------------------------------------- 1 | abghABCDE 2 | 2 3 | abAB 4 | ab 5 | -------------------------------------------------------------------------------- /lib/todo/Strings/SuffixAutomaton: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/lib/todo/Strings/SuffixAutomaton -------------------------------------------------------------------------------- /lib/todo/Strings/Trie.cpp: -------------------------------------------------------------------------------- 1 | struct Trie { 2 | int cnt, end_here; 3 | map mp; 4 | 5 | Trie() : cnt(), end_here() {} 6 | 7 | template void add(T const& s, int i = 0) { 8 | if (i == (int) s.size()) end_here++; 9 | else { 10 | cnt++; 11 | if (children[s[i]] == NULL) children[s[i]] = new Trie(); 12 | children[s[i]]->add(s, i + 1); 13 | } 14 | } 15 | 16 | template void remove(T const& s, int i = 0) { 17 | if (i == (int) s.size()) end_here--; 18 | else { 19 | assert(children[s[i]] != NULL); 20 | cnt--; 21 | children[s[i]]->remove(s, i + 1); 22 | } 23 | } 24 | 25 | template int count_equal(T const& s, int i = 0) { 26 | if (i == (int) s.size()) return end_here; 27 | if (children[s[i]] == NULL) return 0; 28 | return children[s[i]]->count_equal(s, i + 1); 29 | } 30 | 31 | template int count_pref_equal(T const& s, int i = 0) { 32 | if (i == (int) s.size()) return end_here; 33 | if (children[s[i]] == NULL) return end_here; 34 | return end_here + children[s[i]]->count_pref_equal(s, i + 1); 35 | } 36 | }; -------------------------------------------------------------------------------- /problems/gyms/ACPC 2018/L.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | 8 | const int N = 1000007, inf = 0x3f3f3f3f; 9 | 10 | using namespace std; 11 | typedef long long ll; 12 | typedef pair ii; 13 | 14 | int main() { 15 | freopen("looking.in", "r", stdin); 16 | ios::sync_with_stdio(0); cin.tie(0); 17 | 18 | int t; cin >> t; 19 | 20 | while(t--) { 21 | int n, k; cin >> n >> k; 22 | int ans = 0; 23 | for(int i = 0; i < n; ++i) { 24 | int a; cin >> a; 25 | ans |= a; 26 | } 27 | cout << ans << '\n'; 28 | } 29 | } -------------------------------------------------------------------------------- /problems/gyms/Arabella 2018/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | 8 | const int N = 100007, inf = 0x3f3f3f3f; 9 | 10 | using namespace std; 11 | typedef long long ll; 12 | typedef pair ii; 13 | 14 | double f(double a, double b) { 15 | return sqrt(a * a + b * b); 16 | } 17 | 18 | int main() { 19 | ios::sync_with_stdio(0); cin.tie(0); 20 | int t; cin >> t; 21 | 22 | while(t--) { 23 | double a, b, c, x; cin >> a >> b >> c >> x; 24 | 25 | x /= 100; 26 | 27 | cout << fixed << setprecision(10) << f(a, b + 2 * c) + f(a, b) * x + f(a - x * a, 2 * c + (b - x * b)) << '\n'; 28 | } 29 | } -------------------------------------------------------------------------------- /problems/gyms/Arabella 2018/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | 8 | const int N = 100007, inf = 0x3f3f3f3f; 9 | 10 | using namespace std; 11 | typedef long long ll; 12 | typedef pair ii; 13 | 14 | int f[100]; 15 | int main() { 16 | ios::sync_with_stdio(0); cin.tie(0); 17 | 18 | int t; cin >> t; 19 | 20 | while(t--) { 21 | int ones = 0; 22 | int len = 0; 23 | for(int i = 0; i < 26; ++i) { 24 | cin >> f[i]; 25 | if(f[i] > 1) len += 2; 26 | ones += f[i] == 1; 27 | } 28 | 29 | 30 | if(ones) len++; 31 | if(len == 0) { 32 | cout << 0 << ' ' << 0 << '\n'; 33 | continue; 34 | } 35 | int tot = 0x3f3f3f3f; 36 | 37 | for(int i = 0; i < 26; ++i) { 38 | if(f[i] > 1) tot = min(tot, f[i]); 39 | } 40 | 41 | 42 | if(len%2 == 0) cout << len << ' ' << tot/2 << '\n'; 43 | else cout << len << ' ' << min(tot/2, ones) << '\n'; 44 | 45 | 46 | } 47 | } -------------------------------------------------------------------------------- /problems/gyms/Arabella 2018/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | using ull = uint64_t; 6 | 7 | ull solve(ull x) { 8 | return x+1 - (1ull << __builtin_popcountll(x)); 9 | } 10 | 11 | int main() { 12 | ios::sync_with_stdio(false), cin.tie(nullptr); 13 | 14 | int t; 15 | cin >> t; 16 | 17 | while (t--) { 18 | ull x; 19 | cin >> x; 20 | 21 | cout << solve(x) << "\n"; 22 | } 23 | } -------------------------------------------------------------------------------- /problems/gyms/BAPC 2017/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | using ll = long long; 6 | using ii = pair; 7 | 8 | seed_seq seq { 9 | (uint64_t) chrono::duration_cast( 10 | chrono::high_resolution_clock::now(). 11 | time_since_epoch()).count(), 12 | (uint64_t) __builtin_ia32_rdtsc(), 13 | (uint64_t) random_device{}(), 14 | (uint64_t) 17 15 | }; 16 | 17 | mt19937 rd{seq}; 18 | 19 | double const pi = acos(-1); 20 | 21 | double n, m; 22 | double r; 23 | 24 | double dist(double m1, double m2, double r2) { 25 | return r2*pi*abs(m2/m - m1/m); 26 | } 27 | 28 | int main() { 29 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 30 | 31 | cin >> m >> n >> r; 32 | 33 | double a, r1, b, r2; 34 | cin >> a >> r1 >> b >> r2; 35 | 36 | double ans = 1e18; 37 | for (double i = 0; i <= n; i++) { 38 | double d = abs(r*(r1-i)/n); 39 | d += abs(r*(r2-i)/n); 40 | 41 | d += dist(a, b, r*i/n); 42 | 43 | ans = min(ans, d); 44 | } 45 | 46 | cout << setprecision(9) << fixed; 47 | cout << ans << "\n"; 48 | } 49 | -------------------------------------------------------------------------------- /problems/gyms/JUST 4.0 2017/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define mk make_pair 8 | #define pb push_back 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | typedef pair pll; 13 | 14 | const int MAXN = 2e5 + 5; 15 | int v[MAXN]; 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(NULL); 18 | 19 | int t; cin >> t; 20 | 21 | while(t--) { 22 | int n; cin >> n; 23 | 24 | for(int i = 0; i < n; ++i) { 25 | cin >> v[i]; 26 | } 27 | 28 | ll ans = 0; 29 | for(int b = 0; b < 22; b -= -1) { 30 | ll x = (1ll << b); 31 | for(int l = 0, r; l < n; l = r) { 32 | r = l; 33 | while(r < n and ((v[l] >> b)&1) == ((v[r] >> b)&1)) r++; 34 | if((v[l] >> b)&1) { 35 | ll tot = r - l; 36 | tot = tot * (tot + 1)/2; 37 | ans += tot * x; 38 | } 39 | } 40 | } 41 | 42 | cout << ans << '\n'; 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /problems/gyms/JUST 4.0 2017/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define mk make_pair 8 | #define pb push_back 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | typedef pair pll; 13 | 14 | const int N = 2e5 + 5; 15 | 16 | int v[N]; 17 | int main() { 18 | ios::sync_with_stdio(false); cin.tie(NULL); 19 | 20 | int t; cin >> t; 21 | 22 | while(t--) { 23 | int n, m; cin >> n >> m; 24 | 25 | for(int i = 0;i < n; ++i) cin >> v[i]; 26 | 27 | int last = -1; 28 | for(int i = 0; i < n; ++i) { 29 | int & x = v[i]; 30 | if(x == -1 and last != -1) { 31 | x = (last + 1)%m; 32 | } 33 | last = x; 34 | } 35 | 36 | last = -1; 37 | for(int i = n-1; i >= 0; --i) { 38 | int & x = v[i]; 39 | if(x == -1 and last != -1) { 40 | x = (last - 1 + m)%m; 41 | } 42 | last = x; 43 | } 44 | 45 | for(int i = 0; i < n; ++i) cout << v[i] << ' '; 46 | cout << '\n'; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /problems/gyms/JUST 4.0 2017/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define mk make_pair 8 | #define pb push_back 9 | 10 | #define MOD 1000000007 11 | 12 | typedef long long ll; 13 | typedef pair pii; 14 | typedef pair pll; 15 | 16 | const int MAXN = 2e5 + 5; 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | 21 | int t; 22 | cin >> t; 23 | 24 | for(int w=0; w> n; 29 | 30 | for(int i=0; i> x; 33 | 34 | m *= (x+1); 35 | m = m%MOD; 36 | } 37 | 38 | cout << (m-1 + MOD)%MOD << "\n"; 39 | } 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /problems/gyms/Jakarta 2018/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | #define pb push_back 7 | #define eb emplace_back 8 | #define mk make_pair 9 | 10 | typedef pair pii; 11 | typedef long long ll; 12 | 13 | const int MAXN = 1e5 + 5; 14 | const int INF = 0x3f3f3f3f; 15 | 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(NULL); 18 | 19 | string s; 20 | cin >> s; 21 | int n = s.size(); 22 | 23 | int c1 = count(s.begin(), s.end(), '1'); 24 | int c0 = n - c1; 25 | 26 | string ans; 27 | if (c0 > c1) { 28 | ans = string(n, '1'); 29 | } 30 | else if (c0 < c1) { 31 | ans = string(n, '0'); 32 | } 33 | else { 34 | int b = int(s.front() == '0') + int(s.back() == '0'); 35 | 36 | if (b == 0) { 37 | // s[0] = 1 e s[n-1] = 1 38 | ans = string(n, '1'); 39 | ans[0] = '0'; 40 | } 41 | else if (b == 2) { 42 | ans = string(n, '0'); 43 | ans[0] = '1'; 44 | } 45 | else { 46 | ans = string(n, '0'); 47 | if (s[0] == '1') ans[n-1] = '1'; 48 | else ans[0] = '1'; 49 | } 50 | } 51 | 52 | cout << ans << "\n"; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /problems/gyms/Jakarta 2018/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | #define pb push_back 7 | #define eb emplace_back 8 | 9 | typedef pair pii; 10 | 11 | const int MAXN = 505; 12 | 13 | int adj[MAXN][MAXN], deg[MAXN]; 14 | 15 | int main() { 16 | int n, m; scanf("%d%d", &n, &m); 17 | 18 | for (int i = 1; i <= m; i++) { 19 | int u, v; scanf("%d%d", &u, &v); 20 | adj[u][v] = adj[v][u] = 1; 21 | deg[u]++, deg[v]++; 22 | } 23 | 24 | int ans = 1e5; 25 | for (int k = 1000; k >= 0; k--) { 26 | bool used = false; 27 | for (int u = 1; u <= n; u++) { 28 | for (int v = u+1; v <= n; v++) { 29 | if (!adj[u][v] && deg[u] + deg[v] >= k) { 30 | deg[u]++, deg[v]++; 31 | adj[u][v] = adj[v][u] = 1; 32 | used = true; 33 | ans = k; 34 | } 35 | } 36 | } 37 | 38 | k += used; 39 | } 40 | 41 | printf("%d\n", ans); 42 | } -------------------------------------------------------------------------------- /problems/gyms/Jakarta 2018/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | #define pb push_back 7 | #define eb emplace_back 8 | #define mk make_pair 9 | 10 | typedef pair pii; 11 | typedef long long ll; 12 | 13 | const int MAXN = 1e5 + 5; 14 | const int INF = 0x3f3f3f3f; 15 | 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(NULL); 18 | 19 | int n; cin >> n; 20 | string s; cin >> s;n--; 21 | bool ans = s[0] == 'T'; 22 | while(n--) { 23 | string x; cin >> x; 24 | if (x[0] == 'L') ans = !ans; 25 | } 26 | 27 | cout << (ans ? "TRUTH" : "LIE") << endl; 28 | 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /problems/gyms/Latin American Regional 2020/L.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define deb(x) (cout << #x << ": " << x << endl) 8 | 9 | using namespace std; 10 | typedef long long ll; 11 | typedef pair ii; 12 | 13 | const int N = 2e5 + 7, inf = 0x3f3f3f3f; 14 | int evt[N], cnt[N]; 15 | 16 | int main() { 17 | ios::sync_with_stdio(0); cin.tie(0); 18 | int n, x; cin >> n >> x; 19 | 20 | for(int i = 0; i < n; i++) { 21 | int s, d; cin >> s >> d; 22 | evt[s]++; 23 | evt[s + d + 1]--; 24 | } 25 | 26 | int cur = 0; 27 | for(int i = 0; i < N; i++) { 28 | cur += evt[i]; 29 | evt[i] = cur; 30 | cnt[i%x] += cur; 31 | } 32 | 33 | // for(int i = 0; i < x; i++) { 34 | // cout << cnt[i] << ' '; 35 | // } 36 | // cout << '\n'; 37 | 38 | ii ans = ii(inf, inf); 39 | for(int i = 0; i <= 8 * 60; i++) { 40 | int tot = cnt[i%x]; 41 | 42 | ans = min(ans, ii(tot, i)); 43 | cnt[i%x] -= evt[i]; 44 | } 45 | 46 | cout << ans.se << ' ' << ans.fi << '\n'; 47 | } -------------------------------------------------------------------------------- /problems/gyms/Latin American Regional 2020/N.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define deb(x) (cout << #x << ": " << x << endl) 8 | 9 | using namespace std; 10 | typedef long long ll; 11 | typedef pair ii; 12 | 13 | int main() { 14 | ios::sync_with_stdio(0); cin.tie(0); 15 | 16 | int n; scanf("%d ", &n); 17 | 18 | int tot = 0; 19 | int ans = 0; 20 | int aux; 21 | scanf("$%d.%d ", &aux, &tot); 22 | for(int i = 0; i < n; i++) { 23 | int x, y; 24 | scanf("$%d.%2d ", &x, &y); 25 | // deb(x); 26 | // deb(y); 27 | 28 | tot += y; 29 | tot %= 100; 30 | if(tot%100) { 31 | ans++; 32 | } 33 | } 34 | 35 | cout << ans << '\n'; 36 | 37 | 38 | } -------------------------------------------------------------------------------- /problems/gyms/Latin American Regional 2020/statements.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/gyms/Latin American Regional 2020/statements.pdf -------------------------------------------------------------------------------- /problems/gyms/Latin American Subregional 2019/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define fi first 7 | #define se second 8 | #define mp make_pair 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | int main() 17 | { 18 | ios::sync_with_stdio(false); cin.tie(NULL); 19 | int n;; 20 | cin >> n; 21 | vector > v; 22 | for(int i=0;i> a; 26 | v.pb(make_pair(a,-i)); 27 | } 28 | sort(v.rbegin(),v.rend()); 29 | if(v[0].se == 0) 30 | cout << "S" << endl; 31 | else 32 | cout << "N" << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /problems/gyms/Latin American Subregional 2019/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define fi first 7 | #define se second 8 | #define mp make_pair 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(NULL); 18 | 19 | int v, n; 20 | cin >> v >> n; 21 | 22 | int x = v*n; 23 | 24 | for (int i = 1; i <= 9; i ++) { 25 | cout << (x*i+9)/10 << " \n"[i==9]; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /problems/gyms/Latin American Subregional 2019/L.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n; scanf("%lld", &n); 6 | printf("%lld\n", 1ll << __builtin_popcountll(n)); 7 | } -------------------------------------------------------------------------------- /problems/gyms/Latin American Subregional 2019/M.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define fi first 7 | #define se second 8 | #define mp make_pair 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | int n, c, t; 17 | vector a; 18 | 19 | bool check(long long k) { 20 | long long cur = 0, qtt = 0; 21 | for (int i : a) { 22 | if ((i + t - 1) / t > k) return false; 23 | if ((cur + i + t - 1) / t <= k) cur += i; 24 | else { 25 | cur = i; 26 | qtt++; 27 | } 28 | } 29 | 30 | qtt += cur != 0; 31 | return qtt <= c; 32 | } 33 | 34 | int main() { 35 | ios::sync_with_stdio(false); cin.tie(NULL); 36 | 37 | cin >> n >> c >> t; 38 | a.resize(n); 39 | for (int& i : a) cin >> i; 40 | 41 | long long lo = 1, hi = 1e14, mi; 42 | while(lo < hi) { 43 | mi = (lo + hi) / 2; 44 | 45 | if (check(mi)) hi = mi; 46 | else lo = mi + 1; 47 | } 48 | 49 | cout << hi << '\n'; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /problems/gyms/Latin American Subregional 2020/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define deb(x) (cout << #x << ": " << x << endl) 4 | #define pb push_back 5 | #define fi first 6 | #define se second 7 | #define eb emplace_back 8 | 9 | using namespace std; 10 | typedef long long ll; 11 | typedef pair ii; 12 | 13 | const int N = 107; 14 | void no() { 15 | cout << "N" << endl; 16 | exit(0); 17 | } 18 | int mat[N][N]; 19 | 20 | int main() { 21 | ios::sync_with_stdio(0); cin.tie(0); 22 | 23 | int n; cin >> n; 24 | 25 | for(int i = 1; i <= n; ++i) { 26 | int d, l, r, c; cin >> d >> l >> r >> c; 27 | 28 | if(d == 0) { 29 | if(c + l - 1 > 10) no(); 30 | for(int k = c; k < c + l; ++k) { 31 | if(mat[r][k] > 0) { 32 | no(); 33 | } 34 | mat[r][k] = i; 35 | } 36 | } else { 37 | if(r + l - 1 > 10) no(); 38 | 39 | for(int k = r; k < r + l; ++k) { 40 | if(mat[k][c] > 0) no(); 41 | mat[k][c] = i; 42 | } 43 | } 44 | } 45 | 46 | cout << "Y" << endl; 47 | } -------------------------------------------------------------------------------- /problems/gyms/NCPC 2015/B.cpp: -------------------------------------------------------------------------------- 1 | // FAIL TO LOAD CODE 2 | // codeforces submission 76043324 -------------------------------------------------------------------------------- /problems/gyms/NCPC 2016/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ff first 4 | #define ss second 5 | 6 | #define pb push_back 7 | #define eb emplace_back 8 | #define all(x) begin(x), end(x) 9 | #define rall(x) rbegin(x), rend(x) 10 | #define clr(x, c) memset((x), (c), sizeof((x))) 11 | 12 | using namespace std; 13 | 14 | using ll = long long; 15 | using ii = pair; 16 | 17 | int const inf = 0x3f3f3f3f; 18 | ll const linf = 0x3f3f3f3f3f3f3f3f; 19 | 20 | int const mod = 1e9 + 7; 21 | 22 | int main() { 23 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 24 | 25 | double n, p; 26 | cin >> n >> p; 27 | 28 | 29 | double c = p / (n+1); 30 | double ans = c; 31 | 32 | for (double x = 2; x <= 3e6; x++) { 33 | c *= x; 34 | c /= x-1; 35 | 36 | c *= n-p+x; 37 | c /= n+x; 38 | 39 | ans = max(ans, c); 40 | } 41 | 42 | cout << setprecision(9) << ans << '\n'; 43 | } 44 | -------------------------------------------------------------------------------- /problems/gyms/NCPC 2016/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ff first 4 | #define ss second 5 | 6 | #define pb push_back 7 | #define eb emplace_back 8 | #define all(x) begin(x), end(x) 9 | #define rall(x) rbegin(x), rend(x) 10 | #define clr(x, c) memset((x), (c), sizeof((x))) 11 | 12 | using namespace std; 13 | 14 | using ll = long long; 15 | using ii = pair; 16 | 17 | int const inf = 0x3f3f3f3f; 18 | ll const linf = 0x3f3f3f3f3f3f3f3f; 19 | 20 | int const mod = 360; 21 | 22 | 23 | int main() { 24 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 25 | 26 | int n1, n2; 27 | cin >> n1 >> n2; 28 | 29 | if ((mod + n1 - n2) % mod < (mod - n1 + n2) % mod){ 30 | cout << -(mod + n1 - n2) % mod <<'\n'; 31 | } 32 | else cout << (mod - n1 + n2) % mod << '\n'; 33 | } 34 | -------------------------------------------------------------------------------- /problems/gyms/NCPC 2018/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | using ll = long long; 6 | using ii = pair; 7 | 8 | int main() { 9 | ios::sync_with_stdio(false), cin.tie(nullptr); 10 | 11 | int n; cin >> n; 12 | 13 | bool flag = true; 14 | for (int i = 1; i <= n; i++) { 15 | string s; cin >> s; 16 | if (s == "mumble") continue; 17 | int x = stoi(s); 18 | if (x != i) flag = false; 19 | } 20 | 21 | if (flag) cout << "makes sense\n"; 22 | else cout << "something is fishy\n"; 23 | } -------------------------------------------------------------------------------- /problems/gyms/NCPC 2018/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | using ll = long long; 6 | using ii = pair; 7 | 8 | bool a[366]; 9 | 10 | int main() { 11 | ios::sync_with_stdio(false), cin.tie(nullptr); 12 | 13 | int n; cin >> n; 14 | for (int i = 1; i <= n; i++) { 15 | int x; cin >> x; 16 | a[x] = 1; 17 | } 18 | 19 | int ans = 0; 20 | vector cur; 21 | for (int i = 1; i <= 365; i++) { 22 | if (a[i]) cur.push_back(i); 23 | 24 | int sum = 0; 25 | for (int x : cur) sum += i - x + 1; 26 | if (sum >= 20) { // precisa limpar 27 | ans++; 28 | cur.clear(); 29 | } 30 | } 31 | 32 | if (cur.size()) ans++; 33 | 34 | cout << ans << '\n'; 35 | } -------------------------------------------------------------------------------- /problems/gyms/NCPC 2018/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | using ll = long long; 6 | using ii = pair; 7 | 8 | const ll W = 10080; 9 | 10 | vector ans[100005]; 11 | char str[W]; 12 | 13 | int main() { 14 | ll l, m; scanf("%lld%lld", &l, &m); 15 | for (int i = 0; i < m; i++) { 16 | ll p, c, t, r; 17 | scanf(" %[^,],%lld,%lld,%lld,%lld", str, &p, &c, &t, &r); 18 | 19 | bool ok = true; 20 | 21 | long long k = lcm(W, t + r); 22 | 23 | ll q1 = k * W / (t + r); 24 | ll q2 = ((k * W) % (t + r)) / t; 25 | ok &= ((q1 + q2) * t * c) >= (k * l); 26 | 27 | if (ok) ans[p].push_back(string(str)); 28 | } 29 | 30 | for (int i = 0; i < W; i++) { 31 | if (ans[i].size()) { 32 | for (string& s : ans[i]) printf("%s\n", s.c_str()); 33 | return 0; 34 | } 35 | } 36 | 37 | printf("no such mower\n"); 38 | } -------------------------------------------------------------------------------- /problems/gyms/NCPC 2018/K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | const int MAXN = 2500 + 5; 7 | const int MOD = 1e9 + 7; 8 | 9 | inline int add(int x, int y) { 10 | x += y; 11 | if (x >= MOD) x -= MOD; 12 | return x; 13 | } 14 | 15 | inline int mul(int x, int y) { 16 | return ll(x) * y % MOD; 17 | } 18 | 19 | int memo[MAXN][MAXN]; 20 | int main() { 21 | int n, k; scanf("%d%d", &n, &k); 22 | for (int i = 1; i < n; i++) scanf("%*d"); 23 | 24 | memo[0][0] = 1; 25 | for (int u = 1; u <= n; u++) { 26 | for (int c = 1; c <= k; c++) { 27 | int op1 = mul(c - 1, memo[u - 1][c]); 28 | int op2 = mul(c, memo[u - 1][c - 1]); 29 | memo[u][c] = add(op1, op2); 30 | } 31 | } 32 | 33 | printf("%d\n", memo[n][k]); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /problems/gyms/Pacific Northwest 2019-2020 Div. 1/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAX 100005 5 | #define pb push_back 6 | 7 | int n, m; 8 | vector adj[MAX]; 9 | 10 | int dist[MAX]; 11 | 12 | void bfs(int s){ 13 | memset(dist, -1, sizeof(dist)); 14 | queue q; 15 | q.push(s); 16 | dist[s] = 0; 17 | while(!q.empty()){ 18 | int u = q.front(); 19 | q.pop(); 20 | for(int v : adj[u]){ 21 | if(dist[v] == -1){ 22 | dist[v] = dist[u]+1; 23 | q.push(v); 24 | } 25 | } 26 | } 27 | } 28 | 29 | int main(){ 30 | 31 | cin>>n>>m; 32 | for(int i=0; i>a>>b; 34 | adj[a].pb(b); 35 | adj[b].pb(a); 36 | } 37 | bfs(1); 38 | cout< 2 | using namespace std; 3 | 4 | #define MAX 1005 5 | #define pii pair 6 | #define pb push_back 7 | #define ff first 8 | #define ss second 9 | 10 | int r, c, ans, vertices; 11 | bool vis[MAX][MAX]; 12 | vector adj[MAX][MAX]; 13 | 14 | void dfs(int i, int j){ 15 | vis[i][j] = true; 16 | vertices++; 17 | for(pii v : adj[i][j]) 18 | if(!vis[v.ff][v.ss]) 19 | dfs(v.ff, v.ss); 20 | } 21 | 22 | int main(){ 23 | memset(vis, false, sizeof vis); 24 | 25 | cin>>r>>c; 26 | for(int i=0; i>m; 29 | if(m=='\\'){ 30 | adj[i][j].pb({i+1, j+1}); 31 | adj[i+1][j+1].pb({i, j}); 32 | } 33 | if(m=='/'){ 34 | adj[i][j+1].pb({i+1, j}); 35 | adj[i+1][j].pb({i, j+1}); 36 | } 37 | if(m!='.') ans++; 38 | } 39 | } 40 | 41 | for(int i=0; i<=r; i++) 42 | for(int j=0; j<=c; j++) 43 | if(!vis[i][j]){ 44 | vertices = 0; 45 | dfs(i, j); 46 | ans-=vertices-1; 47 | } 48 | 49 | cout< 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | 8 | const int N = 1000007, inf = 0x3f3f3f3f; 9 | 10 | using namespace std; 11 | typedef long long ll; 12 | typedef pair ii; 13 | 14 | int main() { 15 | ios::sync_with_stdio(0); cin.tie(0); 16 | 17 | map cnt; 18 | int n; cin >> n; 19 | 20 | for(int i = 0; i < n; ++i) { 21 | string s; cin >> s; 22 | 23 | cnt[s]++; 24 | } 25 | 26 | for(auto a : cnt) { 27 | int f = a.second; 28 | if(f > n - f) { 29 | cout << a.first << endl; 30 | return 0; 31 | } 32 | } 33 | 34 | cout << "NONE" << endl; 35 | } -------------------------------------------------------------------------------- /problems/gyms/SWERC 2019/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | 8 | const int N = 1000007, inf = 0x3f3f3f3f; 9 | 10 | using namespace std; 11 | typedef long long ll; 12 | typedef pair ii; 13 | 14 | int vis[N]; 15 | 16 | int main() { 17 | ios::sync_with_stdio(0); cin.tie(0); 18 | 19 | int n; cin >> n; 20 | 21 | for(int i = 0; i < n; ++i) { 22 | string s; cin >> s; 23 | 24 | if(s[0] == '-') continue; 25 | if(s.size() > 7) continue; 26 | int x; 27 | sscanf(s.c_str(), "%d", &x); 28 | if(x >= N) continue; 29 | vis[x] = 1; 30 | } 31 | 32 | for(int i = 0; i < N; ++i) { 33 | if(vis[i] == 0) { 34 | cout << i << endl; 35 | return 0; 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /problems/gyms/SWERC 2019/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #ifdef LOCAL_PC 5 | #define debug(args...) cout.flush(); fflush(stdout); fprintf(stderr, args) 6 | #else 7 | #define debug(args...) ; /* */ 8 | #endif 9 | #define pb push_back 10 | #define eb emplace_back 11 | #define mk make_pair 12 | #define fi first 13 | #define se second 14 | typedef long long ll; 15 | typedef pair pii; 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | template inline void rd(num &x) { 19 | char c; while(isspace(c = getchar())); 20 | bool neg = false; 21 | if(!isdigit(c)) neg = (c == '-'), x = 0; 22 | else x = c - '0'; 23 | while(isdigit(c = getchar())) 24 | x = (x << 3) + (x << 1) + c - '0'; 25 | if (neg) x = -x; 26 | } 27 | template inline void rd(T& x, Args&... args) { rd(x); rd(args...); } 28 | 29 | const int MAXN = 2e5 + 5, INF = 0x3f3f3f3f; 30 | 31 | int main() { 32 | ll n1, n2, n3; rd(n1, n2, n3); 33 | printf("%lld\n", (n1 + 1) * (n2 + 1) / (n3 + 1) - 1); 34 | } -------------------------------------------------------------------------------- /problems/gyms/Samara Intercollegiate 2015/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define mk make_pair 8 | 9 | using namespace std; 10 | typedef long long ll; 11 | typedef pair ii; 12 | 13 | ll dist(ll x1, ll y1, ll x2, ll y2) { 14 | return abs(x1 - x2) + abs(y1 - y2); 15 | } 16 | 17 | int main() { 18 | ios::sync_with_stdio(0); cin.tie(0); 19 | ll p, n, x; 20 | cin >> p >> n >> x; 21 | 22 | ll ans = dist(0ll, 0ll, x/p, x%p); 23 | ans = min(ans, (n + 1 + p - 1)/p + dist(n/p, n%p, x/p, x%p)); 24 | ans = min(ans, (p + x - 1)/p + abs(p * ((p + x - 1)/p) - x)); 25 | 26 | ans = min(ans, (n + 1 + p - 1)/p + (n - x)/p + abs(n - p * ((n - x)/p) - x)); 27 | ans = min(ans, (n + 1 + p - 1)/p + (n - x)/p + 1 + abs(max(0ll, n - p * (1 + (n - x)/p)) - x)); 28 | 29 | cout << ans << endl; 30 | } -------------------------------------------------------------------------------- /problems/gyms/Samara Intercollegiate 2015/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define mk make_pair 8 | 9 | using namespace std; 10 | typedef long long ll; 11 | typedef pair ii; 12 | 13 | const int N = 100007; 14 | 15 | set cashes; 16 | vector purchases[N]; 17 | 18 | int main() { 19 | ios::sync_with_stdio(0); cin.tie(0); 20 | int n, k; cin >> n >> k; 21 | 22 | 23 | for(int i = 1; i <= k; ++i) { 24 | cashes.insert(ii(0, i)); 25 | } 26 | 27 | for(int i = 1; i <= n; ++i) { 28 | int x; cin >> x; 29 | 30 | int j = cashes.begin()->se; 31 | cout << j << ' '; 32 | int m = purchases[j].size()? purchases[j].back() : x; 33 | m = (m + x); 34 | purchases[j].pb(x); 35 | cashes.erase(cashes.begin()); 36 | int k = purchases[j].size(); 37 | cashes.insert(ii(k * m, j)); 38 | } 39 | cout << endl; 40 | 41 | 42 | } -------------------------------------------------------------------------------- /problems/gyms/Samara Intercollegiate 2015/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define mk make_pair 8 | 9 | using namespace std; 10 | typedef long long ll; 11 | typedef pair ii; 12 | 13 | int main() { 14 | ios::sync_with_stdio(0); cin.tie(0); 15 | 16 | int n; cin >> n; 17 | 18 | stack st; 19 | for (int i = 1; i <= 2*n; i++) { 20 | string x; cin >> x; 21 | int u; cin >> u; 22 | 23 | if (st.size() && x == "in") { 24 | printf("%d %d\n", st.top(), u); 25 | st.push(u); 26 | } else if (x == "in") { 27 | st.push(u); 28 | } else { 29 | st.pop(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /problems/gyms/Samara Intercollegiate 2015/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define mk make_pair 8 | 9 | using namespace std; 10 | typedef long long ll; 11 | typedef pair ii; 12 | 13 | int main() { 14 | ios::sync_with_stdio(0); cin.tie(0); 15 | int n; cin >> n; 16 | ll m = 0; 17 | for(int i = 0; i < n; ++i) { 18 | int a; cin >> a; 19 | m = max(m, (ll) a); 20 | } 21 | for(int i = 0; i < n; ++i) { 22 | int b; cin >> b; 23 | } 24 | 25 | ll s = 0; 26 | int k; cin >> k; 27 | for(int i = 0; i < k; ++i) { 28 | int x; cin >> x; 29 | s += x; 30 | } 31 | 32 | cout << s + m * k << endl; 33 | } -------------------------------------------------------------------------------- /problems/gyms/Samara Intercollegiate 2015/M.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | #define mk make_pair 8 | 9 | using namespace std; 10 | typedef long long ll; 11 | typedef pair ii; 12 | 13 | int main() { 14 | ios::sync_with_stdio(0); cin.tie(0); 15 | int n, q; cin >> n >> q; 16 | 17 | for(int i = 0; i < q; ++i) { 18 | int l, k; cin >> l >> k; 19 | 20 | if(n < l) cout << 0 << '\n'; 21 | else cout << 2 * ((n - l) / (l + k)) + 1 + ((n - l)%(l + k) >= k) << ' '; 22 | } 23 | cout << endl; 24 | } -------------------------------------------------------------------------------- /problems/gyms/Swiss Subregional 2017/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int MAXN = 1e5 + 5; 14 | const int INF = 0x3f3f3f3f; 15 | 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(NULL); 18 | 19 | int t; 20 | cin >> t; 21 | 22 | while (t--) { 23 | ll n; 24 | cin >> n; 25 | 26 | if (n%3 != 0 and n%4 != 0) { 27 | cout << "IMPOSSIBLE\n"; 28 | } 29 | else if (n%3 == 0) { 30 | cout << n/3 << " " << n/3 << " " << n/3 << "\n"; 31 | } 32 | else { 33 | cout << n/2 << " " << n/4 << " " << n/4 << "\n"; 34 | } 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /problems/gyms/Swiss Subregional 2017/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int MAXN = 1e5 + 5; 14 | const int INF = 0x3f3f3f3f; 15 | 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(NULL); 18 | 19 | map mp; 20 | int n; cin >> n; 21 | 22 | int ans = 0; 23 | for (int i = 1; i <= n; i++) { 24 | int x; cin >> x; 25 | ans += mp[x] != 0; 26 | mp[x]++; 27 | } 28 | 29 | cout << ans << endl; 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /problems/gyms/Swiss Subregional 2017/K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int MAXN = 1e5 + 5; 14 | const int INF = 0x3f3f3f3f; 15 | 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(NULL); 18 | 19 | string s; 20 | cin >> s; 21 | 22 | if (s.find("ACM") != s.npos) { 23 | cout << "Fun!\n"; 24 | } 25 | else cout << "boring...\n"; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /problems/gyms/Tsukuba 2015/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | #define se second 6 | #define eb emplace_back 7 | 8 | using namespace std; 9 | 10 | typedef pair ii; 11 | typedef long long ll; 12 | const int N = 1000007, inf = 0x3f3f3f3f; 13 | 14 | double r[N]; 15 | double x[N], y[N]; 16 | int main() { 17 | ios::sync_with_stdio(0); cin.tie(0); 18 | 19 | int n; cin >> n; 20 | 21 | for(int i = 0; i < n; i++) { 22 | cin >> r[i]; 23 | y[i] = r[i]; 24 | } 25 | 26 | x[0] = r[0]; 27 | double ans = 2 * r[0]; 28 | for(int i = 1; i < n; ++i) { 29 | // adiciona circulo i 30 | x[i] = r[i]; 31 | for(int j = 0; j <= i - 1; ++j) { 32 | x[i] = max(x[i], x[j] + sqrt((r[i] + r[j]) * (r[i] + r[j]) - (y[i] - y[j])*(y[i] - y[j]))); 33 | } 34 | ans = max(ans, x[i] + r[i]); 35 | } 36 | 37 | cout << fixed << setprecision(10) << ans << endl; 38 | } -------------------------------------------------------------------------------- /problems/gyms/Tsukuba 2017/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | 6 | using namespace std; 7 | typedef long long ll; 8 | 9 | const int N = 100007; 10 | int l, k; 11 | 12 | ll memo[N][2]; 13 | 14 | ll solve(int w, int black) { 15 | if(w == 0) return black; 16 | ll & st = memo[w][black]; 17 | 18 | if(st == -1) { 19 | if(black) { 20 | st = 1; 21 | if(w) st += solve(w-1, 0); 22 | } else { 23 | st = solve(w-1, 1) + (k <= w ? solve(w - k, 1) : 0); 24 | } 25 | } 26 | return st; 27 | } 28 | 29 | int main() { 30 | memset(memo, -1, sizeof memo); 31 | ios::sync_with_stdio(0); cin.tie(0); 32 | cin >> l >> k; 33 | 34 | ll ans = solve(l - 1, 1) + (k <= l? solve(l - k, 1) : 0); 35 | cout << ans << endl; 36 | } -------------------------------------------------------------------------------- /problems/gyms/Tsukuba 2017/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define pb push_back 4 | #define fi first 5 | 6 | using namespace std; 7 | typedef long long ll; 8 | 9 | const int N = 1000007; 10 | 11 | int h[N]; 12 | 13 | int main() { 14 | ios::sync_with_stdio(0); cin.tie(0); 15 | 16 | int n, t; cin >> n >> t; 17 | 18 | for(int i = 1; i <= n; ++i) { 19 | cin >> h[i]; 20 | } 21 | 22 | ll start = 0; 23 | for(int i = 1; i <= n; ++i) { 24 | ll aux = t - start; 25 | 26 | ll ans; 27 | if(aux <= 0) ans = 1; 28 | else if(i == 0 or h[i] >= h[i-1]) { 29 | ans = aux/h[i] + 1; 30 | } else { 31 | ans = aux/h[i-1] + 1; 32 | 33 | if(aux%h[i-1] >= h[i]) ans++; 34 | } 35 | 36 | cout << ans << '\n'; 37 | start += h[i]; 38 | if(h[i] < h[i-1]) h[i] = h[i-1]; 39 | } 40 | } -------------------------------------------------------------------------------- /problems/gyms/left.txt: -------------------------------------------------------------------------------- 1 | https://codeforces.com/gym/100500 2 | https://codeforces.com/gym/100625 3 | https://codeforces.com/gym/100971 4 | https://codeforces.com/gym/101353 5 | https://codeforces.com/gym/101401 6 | https://codeforces.com/gym/101473 7 | https://codeforces.com/gym/101482 8 | https://codeforces.com/gym/101512 9 | https://codeforces.com/gym/101554 10 | https://codeforces.com/gym/101635 11 | https://codeforces.com/gym/101653 12 | https://codeforces.com/gym/101655 13 | https://codeforces.com/gym/101656 14 | https://codeforces.com/gym/101657 15 | https://codeforces.com/gym/101673 16 | https://codeforces.com/gym/101840 17 | https://codeforces.com/gym/101982 18 | https://codeforces.com/gym/101992 19 | https://codeforces.com/gym/102021 20 | https://codeforces.com/gym/102222 21 | https://codeforces.com/gym/102263 22 | https://codeforces.com/gym/102307 23 | https://codeforces.com/gym/102465 24 | https://codeforces.com/gym/102483 25 | https://codeforces.com/gym/102700 26 | https://codeforces.com/gym/102801 27 | https://codeforces.com/gym/102881 28 | https://codeforces.com/gym/102890 29 | https://codeforces.com/gym/102916 30 | https://codeforces.com/gym/102920 -------------------------------------------------------------------------------- /problems/gyms/list.txt: -------------------------------------------------------------------------------- 1 | - OpenTrains 2 | Asia-Tsukuba 2017 3 | Asia-Tsukuba 2015 4 | Urozero Autumn 2016. NCPC 2016 5 | 6 | -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC137/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | ll a, b; cin >> a >> b; 21 | cout << max({a+b, a-b, a * b}) << '\n'; 22 | 23 | 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC137/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | int k, x; cin >> k >> x; 21 | int kk = k; 22 | 23 | vector ans; 24 | for (int i = x; k && i <= 1000000; i++, k--) 25 | ans.pb(i); 26 | 27 | k = kk-1; 28 | for (int i = x-1; k && i >= -1000000; i--, k--) 29 | ans.pb(i); 30 | 31 | sort(ans.begin(), ans.end()); 32 | for (int i : ans) cout << i << ' '; 33 | cout << '\n'; 34 | 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC137/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | 21 | int n; cin >> n; 22 | vector a(n); 23 | for (auto& x : a) cin >> x; 24 | 25 | map f; 26 | for (auto& x : a) { 27 | sort(x.begin(), x.end()); 28 | f[x]++; 29 | } 30 | 31 | ll ans = 0; 32 | for (auto p : f) { 33 | ll x = p.se; 34 | ans += x * (x-1) / 2; 35 | } 36 | 37 | cout << ans << '\n'; 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC137/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | 21 | int n, m; cin >> n >> m; 22 | vector a(n); 23 | for (auto& p : a) cin >> p.fi >> p.se; 24 | 25 | set days; 26 | for (int i = 1; i <= m; i++) days.emplace(i); 27 | 28 | sort(a.begin(), a.end(), [](pii const& x, pii const& y) { 29 | return x.se == y.se ? x.fi < y.fi: x.se > y.se; 30 | }); 31 | 32 | ll ans = 0; 33 | for (auto p : a) { 34 | if (days.empty()) break; 35 | auto it = days.upper_bound(m - p.fi + 1); 36 | if (it != days.begin()) { 37 | it--; 38 | days.erase(it); 39 | ans += p.se; 40 | } 41 | } 42 | 43 | cout << ans << '\n'; 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC137/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 2500 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | vector edges[MAXN]; 19 | ll dist[MAXN]; 20 | 21 | inline int bellmanford(int n) { 22 | memset(dist, 0x3f, sizeof dist); 23 | 24 | for (int k = 1; k < n; k++) { 25 | bool ok = true; 26 | for (int u = 1; u <= n; u++) { 27 | if (dist[v] > dist[u] + w) { 28 | 29 | } 30 | } 31 | } 32 | 33 | for (int cnt = 1; cnt <= n-1; cnt++) { 34 | bool converged = true; 35 | for (int u = 1; u <= n; u++) { 36 | for (int v : edges[u]) { 37 | if (dist[v] > dist[u] + w) { 38 | dist[v] = dist[u] + w; 39 | converged = false; 40 | } 41 | } 42 | } 43 | if (converged) break; 44 | } 45 | } 46 | 47 | int main() { 48 | // ios::sync_with_stdio(false); cin.tie(NULL); 49 | 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC137/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 2999 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int a[MAXN]; 19 | int main() { 20 | ios::sync_with_stdio(false); cin.tie(NULL); 21 | int n; cin >> n; 22 | for (int i = 0; i < n; i++) { 23 | cin >> a[i]; 24 | } 25 | 26 | 27 | 28 | 29 | for (int i = 0; i < n; i++) { 30 | // a_i == f(i) = b_n-1 * i^n-1 + .... + b_0 31 | 32 | 33 | } 34 | 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC148/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | int a, b; cin >> a >> b; 21 | cout << (a ^ b ^ 3 ^ 2 ^ 1) << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC148/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | int n; cin >> n; 21 | string s, t; cin >> s >> t; 22 | for (int i = 0; i < n; i++) { 23 | cout << s[i] << t[i]; 24 | } 25 | cout << '\n'; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC148/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | ll a, b; cin >> a >> b; 21 | cout << a * b / __gcd(a, b) << '\n'; 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC148/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | int n; cin >> n; 21 | 22 | int ans = 0, ok = 0, cur = 1; 23 | for (int i = 1; i <= n; i++) { 24 | int x; cin >> x; 25 | if (x == cur) { 26 | cur++; 27 | ok = 1; 28 | } else { 29 | ans++; 30 | } 31 | 32 | } 33 | 34 | if (!ok) cout << -1 << '\n'; 35 | else cout << ans << '\n'; 36 | 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC148/E.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/AtCoder/ABC148/E.py -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC155/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | vector a(3); 21 | cin >> a[0] >> a[1] >> a[2]; 22 | sort(a.begin(), a.end()); 23 | 24 | if (a[0] == a[1] && a[1] != a[2]) cout << "Yes\n"; 25 | else if (a[1] == a[2] && a[1] != a[0]) cout << "Yes\n"; 26 | else cout << "No\n"; 27 | 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC155/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | int n; cin >> n; 21 | 22 | bool ans = true; 23 | for (int i = 0; i < n; i++) { 24 | int x; cin >> x; 25 | if (x%2 == 0) ans &= x%3 == 0 or x%5 == 0; 26 | } 27 | 28 | cout << (ans ? "APPROVED" : "DENIED") << '\n'; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/ABC155/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); cin.tie(NULL); 20 | int n; cin >> n; 21 | 22 | map f; 23 | int maxi = 0; 24 | vector ans; 25 | 26 | for (int i = 0; i < n; i++) { 27 | string s; cin >> s; 28 | f[s]++; 29 | 30 | if (f[s] > maxi) { 31 | maxi = f[s]; 32 | ans.clear(); 33 | ans.pb(s); 34 | } else if (f[s] == maxi) { 35 | ans.pb(s); 36 | } 37 | } 38 | 39 | sort(ans.begin(), ans.end()); 40 | for (string& s : ans ) cout << s << '\n'; 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/A: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/AtCoder/DP/A -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/A.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.A 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/A.dSYM/Contents/Resources/DWARF/A: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/AtCoder/DP/A.dSYM/Contents/Resources/DWARF/A -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/B: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/AtCoder/DP/B -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/B.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.B 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/B.dSYM/Contents/Resources/DWARF/B: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/AtCoder/DP/B.dSYM/Contents/Resources/DWARF/B -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/AtCoder/DP/C -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/C.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.C 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /problems/solutions/AtCoder/DP/C.dSYM/Contents/Resources/DWARF/C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/AtCoder/DP/C.dSYM/Contents/Resources/DWARF/C -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 1/A.py: -------------------------------------------------------------------------------- 1 | def gcd(a, b): 2 | return b if a%b == 0 else gcd(b, a%b) 3 | 4 | R = input() 5 | 6 | if('.' in R): 7 | inteiro, decimal = R.split('.') 8 | else: 9 | inteiro = int(R) 10 | decimal = 0 11 | num = int(inteiro) * 100 + int(decimal) 12 | ans = 36000/gcd(36000, num) 13 | 14 | print(int(ans)) 15 | -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 1/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | 7 | typedef long long ll; 8 | typedef pair pii; 9 | 10 | const int INF = 0x3f3f3f3f; 11 | const int MAXN = 100005; 12 | 13 | int n; 14 | set point; 15 | 16 | int main() { 17 | scanf("%d", &n); 18 | 19 | int sum = 0; 20 | point.insert(0); 21 | 22 | for (int i = 1; i <= n; i++) { 23 | int x; 24 | scanf("%d", &x); 25 | sum += x; 26 | point.insert(sum); 27 | } 28 | 29 | 30 | int ans = 0; 31 | for(set::iterator it = point.begin(); it != point.end(); it++) { 32 | if(point.find((*it + sum/2) % sum) != point.end()) { 33 | ans++; 34 | point.erase(it); 35 | point.erase((*it + sum/2) % sum); 36 | } 37 | } 38 | 39 | printf("%c\n", ans >= 2 and sum%2 == 0 ? 'Y' : 'N'); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 1/D.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | n = int(input()) 4 | emails = set() 5 | 6 | for i in range(n): 7 | email = input() 8 | user, provider = email.split('@') 9 | 10 | if('+' in user): 11 | user, dump = email.split('+') 12 | 13 | user = user.replace('.', '') 14 | email = user + '@' + provider 15 | 16 | emails.add(email) 17 | 18 | print(len(emails)) -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 1/M.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | 7 | typedef long long ll; 8 | typedef pair pii; 9 | 10 | const int INF = 0x3f3f3f3f; 11 | const int MAXN = 100005; 12 | 13 | int n; 14 | vector cards; 15 | 16 | int main() { 17 | // ios::sync_with_stdio(false); cin.tie(NULL); 18 | scanf("%d", &n); 19 | cards.resize(n); 20 | 21 | for (int i = 0; i < n; i++) scanf("%d", &cards[i]); 22 | 23 | int ans = 1; 24 | for (int i = 0; i < n-1; i++) { 25 | if (cards[i] < cards[i+1]) ans++; 26 | } 27 | 28 | printf("%d\n", ans); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 2/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | const int MAXN = 5001; 7 | 8 | ll sum[MAXN]; 9 | 10 | bool check(int num) { 11 | char s[10]; sprintf(s, "%d", num); 12 | int len = strlen(s); 13 | sort(s, s + len); 14 | for (int i = 1; i < len; i++) { 15 | if (s[i] == s[i-1]) return false; 16 | } 17 | return true; 18 | } 19 | 20 | void pre() { 21 | for (int i = 1; i < MAXN; i++) { 22 | sum[i] = sum[i-1] + check(i); 23 | } 24 | } 25 | 26 | int main() { 27 | pre(); 28 | 29 | int n, m; 30 | while(~scanf("%d %d", &n, &m)) { 31 | printf("%lld\n", sum[m] - sum[n-1]); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 2/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef pair pii; 6 | 7 | const int MAXN = 200; 8 | 9 | bool memo[200]; 10 | bool vis[15][900][15]; 11 | 12 | void pre() { 13 | int h = 0, m = 0; 14 | // memo[0] = true; 15 | 16 | for (int i = 1; i <= 720; i++) { 17 | m %= 60; 18 | h %= 60; 19 | 20 | int ang = 6 * min(60 - abs(h - m), abs(h - m)); 21 | memo[ang] = true; 22 | 23 | if (i%12 == 0) h++; 24 | m++; 25 | 26 | m %= 60; 27 | h %= 60; 28 | 29 | ang = 6 * min(60 - abs(h - m), abs(h - m)); 30 | memo[ang] = true; 31 | } 32 | } 33 | 34 | 35 | int main() { 36 | pre(); 37 | int a; 38 | while(~scanf("%d", &a)) { 39 | printf("%c\n", memo[a] ? 'Y' : 'N'); 40 | } 41 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 2/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 1005; 5 | typedef long long ll; 6 | 7 | ll col[MAXN][MAXN], n, sum[MAXN][MAXN], best[MAXN][MAXN]; 8 | 9 | int main() { 10 | while(scanf("%lld", &n), n != 0) { 11 | memset(col, 0, sizeof col); 12 | memset(sum, 0, sizeof sum); 13 | memset(best, 0, sizeof best); 14 | 15 | for (int i = 1; i <= n; i++) 16 | for (int j = 1; j <= i; j++) 17 | scanf("%lld", &col[i-j+1][j]); 18 | 19 | for (int i = 1; i <= n; i++) 20 | for (int j = 1; j <= n-i+1; j++) 21 | sum[i][j] = sum[i][j-1] + col[i][j]; 22 | 23 | ll ans = 0; 24 | for (int i = 1; i <= n; i++) { 25 | for (int j = 1; j <= n-i+1; j++) { 26 | ll toSum = best[i-1][j]; 27 | 28 | for (int k = j; k <= n-i+2; k++) { 29 | toSum = max(toSum, best[i-1][k]); 30 | } 31 | 32 | best[i][j] = sum[i][j] + toSum; 33 | ans = max(ans, best[i][j]); 34 | } 35 | } 36 | 37 | printf("%lld\n", ans); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 2/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | while (~scanf("%d%d", &n, &k)) { 7 | priority_queue pq; 8 | long long sum = 0; 9 | 10 | for (int i = 1; i <= n; i++) { 11 | int x; scanf("%d", &x); 12 | sum += x; 13 | 14 | if (i%(k+1) == 0) { 15 | pq.push(-x); 16 | } else if (i > k) { 17 | int topo = -pq.top(); 18 | if (x > topo) { 19 | pq.pop(); 20 | pq.push(-x); 21 | } 22 | } 23 | } 24 | 25 | while(pq.size()) { 26 | sum += pq.top(); 27 | pq.pop(); 28 | } 29 | 30 | printf("%lld\n", sum); 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 3/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | typedef long long ll; 7 | typedef pair pii; 8 | 9 | const int MAXN = 100005; 10 | const int INF = 0x3f3f3f3f; 11 | 12 | ll fast_exp(ll x, ll y, ll MOD) { 13 | x%=MOD; 14 | 15 | ll ans = 1; 16 | while (y > 0) { 17 | if (y & 1LL) ans = (ans * x) % MOD; 18 | y >>= 1LL; 19 | x = (x*x) % MOD; 20 | } 21 | 22 | return ans % MOD; 23 | } 24 | 25 | int main() { 26 | // ios::sync_with_stdio(false); cin.tie(NULL); 27 | 28 | ll b, p, m; 29 | while(~scanf("%lld%lld%lld", &b, &p, &m)) { 30 | printf("%lld\n", fast_exp(b, p, m)); 31 | } 32 | 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 3/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | typedef long long ll; 7 | typedef pair pii; 8 | 9 | const ll INF = 100000000000000LL; 10 | 11 | ll memo[35][35][55]; 12 | 13 | ll solve(int n, int m, int k) { 14 | if (k < 0) return INF; 15 | 16 | ll &ret = memo[n][m][k]; 17 | if (~ret) return ret; 18 | 19 | if (n * m == k || k == 0) return ret = 0; 20 | if (k > n * m) return ret = INF; 21 | 22 | ret = INF; 23 | for (int i = 1; i < n; i++) { 24 | ret = min(ret, m * m + solve(i, m, k)); 25 | ret = min(ret, m * m + solve(n-i, m, k - i * m)); 26 | } 27 | 28 | for (int i = 1; i < m; i++) { 29 | ret = min(ret, n * n + solve(n, i, k)); 30 | ret = min(ret, n * n + solve(n, m-i, k - i * n)); 31 | } 32 | 33 | return ret; 34 | } 35 | 36 | int main() { 37 | memset(memo, -1, sizeof memo); 38 | 39 | int t; scanf("%d", &t); 40 | 41 | while(t--) { 42 | int n, m, k; scanf("%d %d %d", &n, &m, &k); 43 | printf("%lld\n", solve(n, m, k)); 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 4/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | typedef pair pii; 7 | typedef long long ll; 8 | 9 | const int MAXN = 100005; 10 | const int INF = 0x3f3f3f3f; 11 | const double PI = acos(-1.0); 12 | 13 | int memo[MAXN]; 14 | int solve(int n) { 15 | if (n ==) 16 | } 17 | 18 | int main() { 19 | // ios::sync_with_stdio(false); cin.tie(NULL); 20 | int t; scanf("%d", &t); 21 | while(t--) { 22 | int n; scanf("%d", &n); 23 | printf("%d\n", solve(n)); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 4/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | typedef pair pii; 7 | typedef long long ll; 8 | 9 | const int MAXN = 100005; 10 | const int INF = 0x3f3f3f3f; 11 | const double PI = acos(-1.0); 12 | 13 | struct point { 14 | int x, y; 15 | point(); 16 | point(int a, int b) { 17 | x = a; 18 | y = b; 19 | } 20 | }; 21 | 22 | struct line { 23 | point ini, fim; 24 | line(); 25 | line(point a, point b) { 26 | ini = a; 27 | fim = b; 28 | } 29 | }; 30 | 31 | int main() { 32 | // ios::sync_with_stdio(false); cin.tie(NULL); 33 | int n, c; scanf("%d%d", &n, &c); 34 | 35 | 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 5/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define DBG(x) cout << "::" << (#x) << ": " << (x) << endl 5 | const int MAXN = 200005; 6 | int n, a[MAXN]; 7 | 8 | double sum(double x) { 9 | double ans = -1000000000; 10 | 11 | double cur = 0, mi = a[1] - x, ma = a[1] - x; 12 | for (int i = 1; i <= n; i++) { 13 | cur += a[i] - x; 14 | 15 | ans = max(ans, abs(cur)); 16 | ans = max(ans, abs(cur - mi)); 17 | ans = max(ans, abs(cur - ma)); 18 | 19 | mi = min(mi, cur); 20 | ma = max(ma, cur); 21 | } 22 | 23 | return ans; 24 | } 25 | 26 | int main() { 27 | scanf("%d", &n); 28 | for (int i = 1; i <= n; i++) scanf("%d", a+i); 29 | 30 | double lo, hi, mi1, mi2; 31 | int cnt = 500; 32 | 33 | hi = 10000000000; 34 | lo = -1000000000; 35 | while(cnt--) { 36 | mi1 = (2 * lo + hi) / 3; 37 | mi2 = (lo + 2 * hi) / 3; 38 | 39 | if (sum(mi1) > sum(mi2)) { 40 | lo = mi1; 41 | } else { 42 | hi = mi2; 43 | } 44 | } 45 | 46 | printf("%.10lf\n", sum(lo)); 47 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 5/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | #define fi first 7 | #define se second 8 | typedef pair pii; 9 | typedef long long ll; 10 | 11 | const int MAXN = 100005; 12 | const int INF = 0x3f3f3f3f; 13 | const double PI = acos(-1.0); 14 | 15 | int main() { 16 | int n, m; scanf("%d%d", &n, &m); 17 | vector a(n), b(m); 18 | 19 | for (int i = 0; i < n; i++) scanf("%d", &a[i]); 20 | for (int i = 0; i < m; i++) scanf("%d", &b[i]); 21 | 22 | sort(a.begin(), a.end()); 23 | sort(b.rbegin(), b.rend()); 24 | 25 | ll ans = 0; 26 | for (int i = 0; i < min(n, m); i++) 27 | ans += max(b[i] - a[i], 0); 28 | 29 | printf("%lld\n", ans); 30 | } 31 | -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 6/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | 7 | const int MAXN = 500005; 8 | 9 | vector edges[MAXN]; 10 | bool vis[MAXN]; 11 | double ans = 0; 12 | 13 | void dfs(int u, int dist, double prob) { 14 | vis[u] = true; 15 | 16 | int qtt = 0; 17 | for (auto v : edges[u]) qtt += !vis[v]; 18 | 19 | if (qtt == 0) { 20 | ans += prob * dist; 21 | return; 22 | } 23 | 24 | for (auto v : edges[u]) { 25 | if (!vis[v]) dfs(v, dist + 1, prob * (1.0 / qtt)); 26 | } 27 | } 28 | 29 | int main() { 30 | ios::sync_with_stdio(false); cin.tie(NULL); 31 | int n; cin >> n; 32 | 33 | for (int i = 1; i <= n-1; i++) { 34 | int u, v; cin >> u >> v; 35 | edges[u].pb(v); 36 | edges[v].pb(u); 37 | } 38 | 39 | dfs(1, 0, 1.0); 40 | 41 | cout << fixed << setprecision(10) << ans << endl; 42 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 6/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | typedef long long ll; 7 | typedef pair pii; 8 | 9 | const int MAXN = 100005; 10 | const int INF = 0x3f3f3f3f; 11 | const double PI = acos(-1.0); 12 | 13 | int main() { 14 | int n, m; scanf("%d%d", &n, &m); 15 | 16 | int mex = 0x3f3f3f3f; 17 | while(m--) { 18 | int l, r; scanf("%d%d", &l, &r); 19 | mex = min(mex, r - l + 1); 20 | } 21 | 22 | printf("%d\n", mex); 23 | for (int i = 0; i < n; i++) printf("%d ", i % mex); 24 | printf("\n"); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 6/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | typedef long long ll; 7 | typedef pair pii; 8 | 9 | const int MAXN = 100005; 10 | const int INF = 0x3f3f3f3f; 11 | const double PI = acos(-1.0); 12 | 13 | int lcm(int a, int b) { 14 | return a / __gcd(a, b) * b; 15 | } 16 | 17 | int main() { 18 | int n, k; scanf("%d%d", &n, &k); 19 | 20 | int l = 1; 21 | for (int i = 0; i < n; i++) { 22 | int x; scanf("%d", &x); 23 | int g = __gcd(x, k); 24 | l = lcm(l, g); 25 | } 26 | 27 | printf("%s\n", l == k ? "Yes" : "No"); 28 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 7/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | map winner; 7 | map who; 8 | 9 | bool solve(ll n) { 10 | if (winner.count(n)) return winner[n]; 11 | 12 | int count = 0; 13 | for (ll i = 2; i * i <= n; i++) { 14 | if (n%i == 0) { 15 | count++; 16 | 17 | if (solve(i) == false) { 18 | who[n] = i; 19 | return winner[n] = true; 20 | } else if (solve(n/i) == false) { 21 | who[n] = n/i; 22 | return winner[n] = true; 23 | } 24 | } 25 | } 26 | 27 | if (count == 0) { 28 | who[n] = 0; 29 | return winner[n] = true; 30 | } 31 | 32 | return winner[n] = false; 33 | } 34 | 35 | int main() { 36 | ios::sync_with_stdio(false); cin.tie(NULL); 37 | 38 | ll n; cin >> n; 39 | 40 | if (!solve(n)) cout << 2 << endl; 41 | else cout << "1\n" << who[n] << endl; 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 7/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int grundy(int n, int k) { 5 | // se k for par, as torres sempre vao se anular 6 | if (k%2 == 0) { 7 | if (n == 1) return 1; 8 | if (n == 2) return 2; 9 | return n % 2 ^ 1; 10 | } else { //senao, as k-1 torres vao se anular 11 | if (n == 0) return 0; 12 | if (n == 1) return 1; 13 | if (n == 2) return 0; 14 | if (n == 3) return 1; 15 | if (n == 4) return 2; 16 | 17 | if (n%2) return 0; 18 | return grundy(n/2, k) == 1 ? 2 : 1; 19 | } 20 | } 21 | 22 | int main() { 23 | int n, k; scanf("%d%d", &n, &k); 24 | 25 | int sum = 0; 26 | for (int i = 1; i <= n; i++) { 27 | int x; scanf("%d", &x); 28 | sum ^= grundy(x, k); 29 | } 30 | 31 | printf("%s\n", sum == 0 ? "Nicky" : "Kevin"); 32 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 7/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 1000005; 5 | int pos[MAXN]; 6 | 7 | bool isValid(int x) { 8 | return x >= 0; 9 | } 10 | 11 | void pre(int k, int l) { 12 | memset(pos, -1, sizeof pos); 13 | 14 | pos[0] = 0; 15 | for (int i = 1; i <= 1000000; i++) { 16 | if (isValid(i - 1) && pos[i-1] == 0) pos[i] = 1; 17 | else if (isValid(i - k) && pos[i-k] == 0) pos[i] = 1; 18 | else if (isValid(i - l) && pos[i-l] == 0) pos[i] = 1; 19 | else pos[i] = 0; 20 | } 21 | } 22 | 23 | int main() { 24 | int k, l, m; scanf("%d%d%d", &k, &l, &m); 25 | 26 | pre(k, l); 27 | 28 | while(m--) { 29 | int n; scanf("%d", &n); 30 | printf("%c", pos[n] ? 'A' : 'B'); 31 | } 32 | printf("\n"); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 8/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | ll n, m, c0, d0; 7 | ll a[15], b[15], c[15], d[15]; 8 | ll memo[15][1005]; 9 | 10 | ll solve(int i, ll cur) { 11 | if (cur < 0) return -9999999; 12 | 13 | if (i > m) return (cur/c0) * d0; 14 | if (memo[i][cur] != -1) return memo[i][cur]; 15 | 16 | ll ret = solve(i+1, cur); 17 | for (ll k = 1; k <= a[i]/b[i]; k++) 18 | ret = max(ret, k * d[i] + solve(i+1, cur - k * c[i])); 19 | 20 | return memo[i][cur] = ret; 21 | } 22 | 23 | int main() { 24 | memset(memo, -1, sizeof memo); 25 | 26 | scanf("%lld%lld%lld%lld", &n, &m, &c0, &d0); 27 | 28 | for (int i = 1; i <= m; i++) 29 | scanf("%lld%lld%lld%lld", a+i, b+i, c+i, d+i); 30 | 31 | printf("%lld\n", solve(1, n)); 32 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 8/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 100005; 5 | int a[MAXN], freq[MAXN]; 6 | set change; 7 | set need; 8 | 9 | int main() { 10 | int n; scanf("%d", &n); 11 | for (int i = 1; i <= n; i++) { 12 | scanf("%d", a+i); 13 | freq[a[i]]++; 14 | 15 | if (a[i] > n || freq[a[i]] > 1) { 16 | change.insert(i); 17 | } 18 | } 19 | 20 | for (int i = 1; i <= n; i++) { 21 | if (freq[i] == 0) need.insert(i); 22 | } 23 | 24 | for (int i : change) { 25 | int next = *need.begin(); 26 | need.erase(need.begin()); 27 | 28 | a[i] = next; 29 | } 30 | 31 | for (int i = 1; i <= n; i++) printf("%d ", a[i]); 32 | printf("\n"); 33 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 9/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define GREEN 0 5 | #define BLACK 1 6 | 7 | #define int long long 8 | 9 | int32_t main() { 10 | int n, k, g, b; scanf("%lld%lld%lld%lld", &n, &k, &g, &b); 11 | 12 | int A = g, B = b; 13 | char c1 = 'G', c2 = 'B'; 14 | 15 | if (A < B) { 16 | swap(A, B); 17 | swap(c1, c2); 18 | } 19 | 20 | if (A > (B+1)*k) return !printf("NO\n"); 21 | 22 | if (A == B) { 23 | for (int i = 0; i < A; i++) printf("GB"); 24 | return !printf("\n"); 25 | } 26 | 27 | int available = A - (B + 1); 28 | for (int i = 0; i <= B; i++) { 29 | printf("%c", c1); 30 | int cur = min(k-1, available); 31 | available -= cur; 32 | for (int j = 1; j <= cur; j++) printf("%c", c1); 33 | if (i != B) printf("%c", c2); 34 | } 35 | 36 | return !printf("\n"); 37 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 9/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a, b, c; cin >> a >> b >> c; 6 | int ans = 0; 7 | 8 | while (a >= 1 && b >= 2 && c >= 4) { 9 | a -= 1; 10 | b -= 2; 11 | c -= 4; 12 | ans += 7; 13 | } 14 | 15 | cout << ans << endl; 16 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 9/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | template using ord_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 8 | 9 | int main() { 10 | int n, q; scanf("%d%d", &n, &q); 11 | vector a(n); 12 | map > h; 13 | for (int i = 0; i < n; i++) { 14 | scanf("%d", &a[i]); 15 | h[a[i]].insert(i); 16 | } 17 | 18 | int ind, typ, ans, old; 19 | while(q--) { 20 | scanf("%d%d", &ind, &typ); 21 | 22 | old = a[ind]; 23 | h[old].erase(ind); 24 | a[ind] = typ; 25 | 26 | h[typ].insert(ind); 27 | 28 | ans = h[typ].order_of_key(ind); 29 | printf("%d\n", ans); 30 | } 31 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2019/Dia 9/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; scanf("%d", &n); 6 | long long ans = 0, cur = 0; 7 | 8 | for (int i = 1; i <= n; i++) { 9 | int x; scanf("%d", &x); 10 | if (x != cur) ans += abs(cur - x); 11 | cur = x; 12 | } 13 | 14 | printf("%lld\n", ans); 15 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2020/01-ICL 2015/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define fi first 4 | #define se second 5 | 6 | using namespace std; 7 | 8 | const int M = 1000000009; 9 | const int N = 307; 10 | typedef long long ll; 11 | using ii = pair; 12 | 13 | int n, k; 14 | 15 | ll memo[N][N][2]; 16 | 17 | ll solve(int p, int a, int ok) { 18 | if(p == n) return a == 0 and ok == 1; 19 | ll & st = memo[p][a][ok]; 20 | 21 | if(st == -1) { 22 | // abrindo 23 | st = solve(p+1, a, ok); 24 | if(a < k) st += solve(p+1, a+1, ok or (a + 1 == k)); 25 | st %= M; 26 | // fechando 27 | if(a > 0) st += solve(p+1, a-1, ok); 28 | st %= M; 29 | } 30 | return st; 31 | } 32 | 33 | int main() { 34 | ios::sync_with_stdio(false), cin.tie(nullptr); 35 | cin >> n >> k; 36 | 37 | memset(memo, -1, sizeof memo); 38 | cout << solve(0, 0, k == 0) << endl; 39 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2020/01-ICL 2015/E: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Brazilian Summer School/Summer School 2020/01-ICL 2015/E -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2020/01-ICL 2015/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ff first 4 | #define ss second 5 | 6 | using namespace std; 7 | 8 | using ll = long long; 9 | using ii = pair; 10 | 11 | const ll N = 1e6; 12 | 13 | ll sum[N], a[N]; 14 | 15 | inline ll acc(int l, int r) { 16 | return sum[r] - sum[l-1]; 17 | } 18 | 19 | int main() { 20 | ios::sync_with_stdio(false), cin.tie(nullptr); 21 | 22 | int n; cin >> n; 23 | for (int i = 1; i <= n; i++) { 24 | ll h, m, s; cin >> h >> m >> s; 25 | a[i] = s + m * N + h * N * N; 26 | a[i+n] = 12 * N * N + a[i]; 27 | } 28 | sort(a+1, a+n+n+1); 29 | 30 | for (int i = 1; i <= n + n; i++) { 31 | sum[i] = sum[i-1] + a[i]; 32 | } 33 | 34 | ll ans = LLONG_MAX; 35 | for (int i = n+1; i <= n + n; i++) { 36 | ans = min(ans, n * a[i] - acc(i-n+1, i)); 37 | } 38 | 39 | int s = ans % N; 40 | ans /= N; 41 | int m = ans % N; 42 | ans /= N; 43 | int h = ans; 44 | 45 | cout << h << ' ' << m << ' ' << s << '\n'; 46 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2020/01-ICL 2015/J: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Brazilian Summer School/Summer School 2020/01-ICL 2015/J -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2020/01-ICL 2015/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | vector > fac(ll n) { 19 | vector > ans; 20 | for (ll i = 2; i * i <= n; i++) { 21 | if (n%i == 0) { 22 | ans.eb(i, 0); 23 | while(n % i == 0) { 24 | n /= i; 25 | ans.back().se++; 26 | } 27 | } 28 | } 29 | 30 | return ans; 31 | } 32 | 33 | ll nck(ll n, ll k, ll m) { 34 | vector > f = fac(k); 35 | 36 | 37 | 38 | } 39 | 40 | int main() { 41 | ll n, k, m; scanf("%lld%lld%lld", &n, &k, &m); 42 | printf("%lld\n", nck(n, k, m)); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2020/02-Flow/in: -------------------------------------------------------------------------------- 1 | 4 2 | 2 3 | he loves to eat baguettes 4 | il aime manger des baguettes 5 | 4 6 | a b c d e 7 | f g h i j 8 | a b c i j 9 | f g h d e 10 | 4 11 | he drove into a cul de sac 12 | elle a conduit sa voiture 13 | il a conduit dans un cul de sac 14 | il mange pendant que il conduit sa voiture 15 | 6 16 | adieu joie de vivre je ne regrette rien 17 | adieu joie de vivre je ne regrette rien 18 | a b c d e 19 | f g h i j 20 | a b c i j 21 | f g h d e 22 | -------------------------------------------------------------------------------- /problems/solutions/Brazilian Summer School/Summer School 2020/03-Russian Team Olympiad/D: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Brazilian Summer School/Summer School 2020/03-Russian Team Olympiad/D -------------------------------------------------------------------------------- /problems/solutions/Codechef/December Challenge 2018/CHFIDEAL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cout << 1 << endl; 6 | int n; cin >> n; 7 | if (n == 2) cout << 3 << endl; 8 | else cout << 2 << endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/December Challenge 2018/CHFINTRO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, r, x; cin >> n >> r; 6 | while(n--) { 7 | cin >> x; 8 | if (x >= r) cout << "Good boi\n"; 9 | else cout << "Bad boi\n"; 10 | } 11 | return 0; 12 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/December Challenge 2018/INTXOR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 50010; 5 | int a[MAXN]; 6 | 7 | void solve(int n) { 8 | int x, y, z; 9 | printf("1 1 2 3\n"); fflush(stdout); 10 | scanf("%d", &x); 11 | printf("1 1 3 4\n"); fflush(stdout); 12 | scanf("%d", &y); 13 | printf("1 1 2 4\n"); fflush(stdout); 14 | scanf("%d", &z); 15 | 16 | a[1] = x xor y xor z; 17 | for (int i = 2; i <= n; i++) a[i] = a[i-1]+1; 18 | } 19 | 20 | 21 | 22 | int main() { 23 | int t, n; scanf("%d", &t); 24 | while(t--) { 25 | scanf("%d", &n); 26 | 27 | solve(n); 28 | 29 | // for (int i = 1; i <= n; i++) { 30 | // printf("1 %d %d %d\n", i, i, i); fflush(stdout); 31 | // scanf("%d", a+i); 32 | // } 33 | 34 | printf("2 "); 35 | for (int i = 1; i <= n; i++) printf("%d ", a[i]); 36 | printf("\n"); fflush(stdout); 37 | 38 | int x; scanf("%d", &x); 39 | if (x == -1) return 0; 40 | } 41 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/COVDSMPL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Codechef/June Challenge 2020/COVDSMPL -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/GUESSG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Codechef/June Challenge 2020/GUESSG -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | ./gen > in 3 | ./TTUPLE < in 4 | make -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/TTUPLE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Codechef/June Challenge 2020/TTUPLE -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/brute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Codechef/June Challenge 2020/brute -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/gen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Codechef/June Challenge 2020/gen -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/gen.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | T = 10 4 | print(T) 5 | for it in range(T): 6 | for j in range(2): 7 | for i in range(3): 8 | print(random.randint(-10, 10), end=' ') 9 | print() -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/go.sh: -------------------------------------------------------------------------------- 1 | for (( c=1; c<=100000; c++ )); do 2 | ./gen > in 3 | ./TTUPLE < in > o2 4 | diff -w o2 o1 5 | done -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/in: -------------------------------------------------------------------------------- 1 | 1 2 | 207810661 87580121 780124523 3 | 0 352370994 0 4 | -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/in2: -------------------------------------------------------------------------------- 1 | 1 2 | 0 1 2 3 | 13 17 19 4 | -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/o1: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /problems/solutions/Codechef/June Challenge 2020/o2: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Qualifier/CHEFPRMS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAXN 205 4 | 5 | bool isPrime[MAXN]; 6 | vector divs[MAXN]; 7 | 8 | bool isSemiPrime[MAXN]; 9 | map sum; 10 | 11 | int main() { 12 | int t, n = 200; scanf("%d", &t); 13 | 14 | for (int i = 0; i <= n; i++) isPrime[i] = true; 15 | for (int i = 0; i <= n; i++) isSemiPrime[i] = false; 16 | 17 | for (int i = 2; i <= n; i++) { 18 | if (isPrime[i]) { 19 | for (int j = 2*i; j <= n; j += i) { 20 | isPrime[j] = false; 21 | 22 | divs[j].push_back(i); 23 | } 24 | } 25 | } 26 | 27 | for (int i = 1; i <= n; i++) 28 | if (!isPrime[i] && (int)divs[i].size() == 2 && divs[i][0] * divs[i][1] == i) 29 | isSemiPrime[i] = true; 30 | 31 | for (int i = 1; i <= n; i++) { 32 | for (int j = 1; j <= n; j++) { 33 | if (isSemiPrime[i] && isSemiPrime[j]) sum[i+j] = true; 34 | } 35 | } 36 | 37 | 38 | while(t--) { 39 | scanf("%d", &n); 40 | 41 | if (sum[n]) printf("YES\n"); 42 | else printf("NO\n"); 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Qualifier/QABC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 100010; 5 | typedef long long ll; 6 | ll a[MAXN], b[MAXN]; 7 | 8 | int main() { 9 | int t; scanf("%d", &t); 10 | 11 | while (t--) { 12 | int n, k; scanf("%d", &n); 13 | for (int i = 1; i <= n; i++) scanf("%lld", a+i); 14 | for (int i = 1; i <= n; i++) scanf("%lld", b+i); 15 | 16 | bool nie = false; 17 | 18 | for (int i = 1; (!nie) && i <= n-2; i++) { 19 | if (a[i] > b[i]) nie = true; 20 | 21 | if (a[i] < b[i]) { 22 | k = b[i]-a[i]; 23 | a[i] += k; 24 | a[i+1] += 2*k; 25 | a[i+2] += 3*k; 26 | } 27 | } 28 | 29 | for (int i = 1; (!nie) && i <= n; i++) nie = a[i] != b[i]; 30 | 31 | printf("%s\n", nie ? "NIE" : "TAK"); 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Qualifier/QUALPREL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool cmp (int& a, int& b) { 5 | return a > b; 6 | } 7 | 8 | int main() { 9 | int t; scanf("%d", &t); 10 | int n, k, s[100005], ans; 11 | 12 | while(t--) { 13 | ans = 0; 14 | scanf("%d%d", &n, &k); 15 | for (int i = 1; i <= n; i++) scanf("%d", s+i); 16 | 17 | sort(s+1, s+n+1, cmp); 18 | for (int i = 1; i <= n; i++) if (s[i] >= s[k]) ans++; 19 | 20 | printf("%d\n", ans); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Qualifier/SPEAD2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 100005; 5 | typedef long long ll; 6 | 7 | ll a[MAXN], sum[MAXN]; 8 | 9 | int main() { 10 | int t; scanf("%d", &t); 11 | 12 | while (t--) { 13 | int n; scanf("%d", &n); 14 | for (int i = 1; i <= n; i++) scanf("%lld", a+i); 15 | for (int i = 1; i <= n; i++) sum[i] = sum[i-1] + a[i]; 16 | 17 | ll j = 1, ans = 0; 18 | while (j < n) { 19 | j += sum[j]; 20 | ans++; 21 | } 22 | 23 | printf("%lld\n", ans); 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Round 1A/AVGMAT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //x = (a, b) 5 | //y = (c, d) 6 | 7 | //|a-c| + |b-d| 8 | 9 | #define eb emplace_back 10 | const int MAXN = 305; 11 | 12 | int mat[MAXN][MAXN]; 13 | 14 | struct point { 15 | int x, y; 16 | 17 | point() {} 18 | point(int a, int b) { 19 | x = a; 20 | y = b; 21 | } 22 | }; 23 | 24 | int main() { 25 | int t; scanf("%d", &t); 26 | while(t--) { 27 | unordered_map dist; 28 | vector houses; 29 | 30 | int n, m; scanf("%d%d ", &n, &m); 31 | char c; 32 | for (int i = 1; i <= n; i++) 33 | for (int j = 1; j <= m; j++) { 34 | scanf("%c ", &c); 35 | mat[i][j] = c-'0'; 36 | if (mat[i][j]) { 37 | houses.eb(point(i, j)); 38 | } 39 | } 40 | 41 | for (int i = 0; i < houses.size(); i++) { 42 | for (int j = i+1; j < houses.size(); j++) { 43 | int x = abs(houses[i].x - houses[j].x) + abs(houses[i].y - houses[j].y); 44 | dist[x]++; 45 | } 46 | } 47 | 48 | for (int i = 1; i <= m+n-2; i++) 49 | printf("%d ", dist[i]); 50 | printf("\n"); 51 | 52 | } 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Round 1A/CARDMGK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios::sync_with_stdio(false); 6 | int t; cin >> t; 7 | while(t--){ 8 | int ans = 1; 9 | int n; cin >> n; 10 | vector inp(n); 11 | vector aux; 12 | for(int i = 0; i< n; i++){ 13 | cin >> inp[i]; 14 | } 15 | aux = inp; 16 | sort(aux.begin(), aux.end()); 17 | if(aux == inp){ 18 | cout << "YES" << endl; 19 | continue; 20 | } 21 | int i = 0; 22 | for(i = 0; i < n-1; i++){ 23 | if(inp[i] <= inp[i+1]) continue; 24 | else break; 25 | } 26 | for(int j = i+1; j < inp.size() - 1; j++){ 27 | if(inp[j] <= inp[j+1]) continue; 28 | else ans = 0; 29 | } 30 | if(ans and inp[0] >= inp[n-1]) cout << "YES" << endl; 31 | else cout << "NO" << endl; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Round 1A/CHEFADD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | const int MAXN = 100005; 6 | 7 | int ppcnt[MAXN]; 8 | 9 | int main() { 10 | int t; scanf("%d", &t); 11 | 12 | for (int i = 0; i <= 100000; i++) 13 | ppcnt[i] = __builtin_popcount(i); 14 | 15 | int a, b, c, a1, b1, ans; 16 | while(t--) { 17 | ans = 0; 18 | scanf("%d%d%d", &a, &b, &c); 19 | 20 | a1 = ppcnt[a]; 21 | b1 = ppcnt[b]; 22 | 23 | for (int i = 1, j = c-1; i < c; i++, j--) { 24 | if (ppcnt[i] == a1 && ppcnt[j] == b1) 25 | ans++; 26 | } 27 | 28 | printf("%d\n", ans); 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Round 1A/TYPING.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | float go(char c, char l){ 5 | if((c == 'd' or c == 'f') and (l == 'd' or l == 'f')) return 0.4; 6 | if((c == 'j' or c == 'k') and (l == 'j' or l == 'k')) return 0.4; 7 | return 0.2; 8 | } 9 | 10 | int main() { 11 | int t; cin >> t; 12 | while(t--){ 13 | double ans = 0; 14 | int n; cin >> n; 15 | map m; 16 | while(n--){ 17 | string st; 18 | cin >> st; 19 | if(m[st]==0.0){ 20 | double worth = 0.2; 21 | for(int i = 1; i < st.size(); i++){ 22 | worth += go(st[i], st[i-1]); 23 | } 24 | m[st] = worth; 25 | ans+= worth; 26 | } else { 27 | ans+=m[st]/2.0; 28 | } 29 | } 30 | cout << ans*10 << endl; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Round 1B/CHFAR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | int T; scanf("%d", &T); 7 | while(T--) { 8 | int n, k; cin >> n >> k; 9 | int x, qt = 0; 10 | while(n--) { 11 | cin >> x; 12 | if (x > 1) qt++; 13 | } 14 | if (qt <= k) cout << "YES\n"; 15 | else cout << "NO\n"; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /problems/solutions/Codechef/Snackdown 2018/Online Round 1B/SNCKYEAR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int T; scanf("%d", &T); 6 | while(T--) { 7 | int N; scanf("%d", &N); 8 | if (N == 2010 || N == 2015 || N == 2016 || N == 2017 || N == 2019) printf("HOSTED\n"); 9 | else printf("NOT HOSTED\n"); 10 | } 11 | return 0; 12 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 1/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 1e5 + 5; 5 | 6 | int n, k; 7 | 8 | vector edges[MAXN]; 9 | vector ans; 10 | 11 | int state[MAXN]; 12 | 13 | void impossible() { 14 | printf("-1\n"); 15 | exit(0); 16 | } 17 | 18 | void dfs(int u) { 19 | if (state[u] == 2) return; 20 | if (state[u] == 1) impossible(); 21 | 22 | state[u] = 1; 23 | 24 | for (int v : edges[u]) dfs(v); 25 | 26 | ans.push_back(u); 27 | state[u] = 2; 28 | } 29 | 30 | int main() { 31 | scanf("%d%d", &n, &k); 32 | vector need(k); 33 | for (int i = 0; i < k; i++) scanf("%d", &need[i]); 34 | 35 | for (int i = 1; i <= n; i++) { 36 | int t; scanf("%d", &t); 37 | while(t--) { 38 | int j; scanf("%d", &j); 39 | edges[i].push_back(j); 40 | } 41 | } 42 | 43 | for (int i : need) { 44 | dfs(i); 45 | } 46 | 47 | printf("%d\n", (int)ans.size()); 48 | for (int i : ans) printf("%d ", i); 49 | printf("\n"); 50 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 1/G.in: -------------------------------------------------------------------------------- 1 | abacaba 2 | abaaba 3 | x 4 | ab 5 | bc 6 | cd 7 | -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 1/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef pair pii; 5 | 6 | int main () { 7 | int n; scanf("%d", &n); 8 | vector ev(n); 9 | 10 | set numbers; 11 | for (int i = 0; i < n; i++) { 12 | int l, r; scanf("%d%d", &l, &r); 13 | numbers.insert(l); 14 | numbers.insert(r); 15 | ev[i] = {l, r}; 16 | } 17 | 18 | map mp; 19 | 20 | int cnt = 1; 21 | for (int i : numbers) mp[i] = cnt++; 22 | 23 | for (int i = 0; i < n; i++) { 24 | ev[i].first = mp[ev[i].first]; 25 | ev[i].second = mp[ev[i].second]; 26 | } 27 | 28 | sort(ev.begin(), ev.end()); 29 | 30 | int ans = 0, maximo = -1; 31 | for (int t = 1, i = 0; t <= cnt; t++) { 32 | int curMax = -1; 33 | 34 | while (i < n && ev[i].first == t) { 35 | if (ev[i].second < maximo) ans++; 36 | curMax = max(curMax, ev[i].second); 37 | i++; 38 | } 39 | 40 | maximo = max(maximo, curMax); 41 | } 42 | 43 | printf("%d\n", ans); 44 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 2/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 2e6 + 5; 5 | vector ind[MAXN]; 6 | vector vec; 7 | 8 | int find(int x, int l, int r) { 9 | return upper_bound(ind[x].begin(), ind[x].end(), r) - 10 | lower_bound(ind[x].begin(), ind[x].end(), l); 11 | } 12 | 13 | int main() { 14 | ios::sync_with_stdio(false); cin.tie(NULL); 15 | 16 | int q; cin >> q; 17 | 18 | while(q--) { 19 | int op; cin >> op; 20 | 21 | if (op == 1) { 22 | int x; cin >> x; 23 | vec.push_back(x); 24 | ind[x].push_back(vec.size()); 25 | } else if (op == 0) { 26 | if (vec.size() == 0) { 27 | cout << "invalid\n"; 28 | continue; 29 | } 30 | 31 | int x = vec.back(); 32 | ind[x].pop_back(); 33 | vec.pop_back(); 34 | } else { 35 | int x, i, j; cin >> x >> i >> j; 36 | cout << find(x, i, j) << '\n'; 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 3/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | typedef pair pii; 7 | 8 | const int MAXN = 1e5 + 5; 9 | 10 | int main() { 11 | ios::sync_with_stdio(false); cin.tie(NULL); 12 | int n; cin >> n; 13 | map freq; 14 | for (int i = 0; i < n; i++) { 15 | int x; cin >> x; 16 | freq[x]++; 17 | } 18 | 19 | long long ans = 0; 20 | 21 | int cur = freq[0]; 22 | for (int i = cur; i > 1; i--) ans += i - 1; 23 | 24 | for (int i = -10; i < 0; i++) { 25 | for (int j = freq[i]; j > 0; j--) { 26 | ans += freq[-i]; 27 | } 28 | } 29 | 30 | cout << ans << endl; 31 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 4/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | typedef pair pll; 13 | typedef pair pli; 14 | 15 | const int MAXN = 1e5 + 5; 16 | 17 | ll a[MAXN], sum[MAXN]; 18 | pli bnd[MAXN]; 19 | 20 | int main() { 21 | int t; scanf("%d", &t); 22 | while(t--) { 23 | memset(bnd, 0, sizeof(bnd)); 24 | 25 | int n, d; scanf("%d%d", &n, &d); 26 | 27 | for (int i = 1; i <= n; i++) { 28 | scanf("%lld", a+i); 29 | sum[i] = sum[i-1] + a[i]; 30 | } 31 | 32 | int lo = 0, hi = 1, ans = INT_MAX; 33 | bnd[0] = mp(0, 0); 34 | 35 | for (int i = 1; i <= n; i++) { 36 | int ind = lower_bound(bnd+lo, bnd+hi, pli(sum[i] - d, INT_MAX)) - (bnd + 1); 37 | if (ind >= lo) ans = min(ans, i - bnd[ind].se); 38 | while(lo < hi && bnd[hi-1].fi >= sum[i]) hi--; 39 | bnd[hi++] = mp(sum[i], i); 40 | } 41 | 42 | printf("%d\n", ans == INT_MAX ? -1 : ans); 43 | } 44 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 4/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 1e5 + 5; 5 | int a[MAXN], neg[MAXN], pos[MAXN]; 6 | 7 | int main() { 8 | ios::sync_with_stdio(false); cin.tie(NULL); 9 | 10 | ifstream in; 11 | in.open("input.txt", ios::in); 12 | 13 | ofstream out; 14 | out.open("output.txt", ios::out); 15 | 16 | int n; in >> n; 17 | 18 | int zr = 0; 19 | for (int i = 1; i <= n; i++) { 20 | in >> a[i]; 21 | neg[i] = neg[i-1] + (a[i] < 0); 22 | pos[i] = pos[i-1] + (a[i] > 0); 23 | zr += a[i] == 0; 24 | } 25 | 26 | int ans = INT_MAX; 27 | for (int i = 1; i < n; i++) { 28 | int cur = pos[i] + neg[n] - neg[i]; 29 | ans = min(ans, cur); 30 | } 31 | 32 | out << ans + zr << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 4/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; scanf("%d", &t); 6 | while(t--) { 7 | int n; scanf("%d", &n); 8 | vector a(n); 9 | for (int& i : a) scanf("%d", &i); 10 | 11 | vector > ind(n); 12 | for (int i = 0; i < n; i++) { 13 | ind[i].first = a[i]; 14 | ind[i].second = i; 15 | } 16 | 17 | sort(ind.rbegin(), ind.rend()); 18 | 19 | int ans = ind[0].first + ind[1].first; 20 | 21 | int qtt = 0, tot = 0; 22 | for (int i = 0; i < n; i++) { 23 | for (int j = i+1; j < n; j++) { 24 | qtt += a[i] + a[j] == ans; 25 | tot++; 26 | } 27 | } 28 | 29 | printf("%.7lf\n", qtt/(double)tot); 30 | } 31 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 5/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define fi first 7 | #define se second 8 | #define mp make_pair 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int MAXN = 1e5 + 5; 14 | 15 | ll a[MAXN], sum[MAXN]; 16 | 17 | int main() { 18 | ios::sync_with_stdio(false); cin.tie(nullptr); 19 | 20 | int t; cin >> t; 21 | while(t--) { 22 | memset(sum, 0, sizeof sum); 23 | 24 | int n, k; cin >> n >> k; 25 | 26 | for (int i = 1; i <= n; i++) { 27 | cin >> a[i]; 28 | sum[i] = sum[i-1] + a[i]; 29 | } 30 | 31 | ll ans = 0, qtd = 0; 32 | for (int i = 1; i <= n; i++) { 33 | int cur = upper_bound(sum+i, sum+n+1, k+sum[i-1]) - sum - 1; 34 | 35 | if ((cur - i + 1 == qtd && sum[cur] - sum[i-1] < ans) || (cur - i + 1 > qtd)) { 36 | qtd = cur - i + 1; 37 | ans = sum[cur] - sum[i-1]; 38 | } 39 | } 40 | 41 | cout << ans << ' ' << qtd << '\n'; 42 | } 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 5/B.py: -------------------------------------------------------------------------------- 1 | MAXN = int(2e4) 2 | M = int(1e9 + 7) 3 | 4 | fac = [1, 1] 5 | for i in range(2, MAXN): 6 | fac.append((fac[i-1] * i) % M) 7 | 8 | def powerMod(x, y, m): 9 | x %= m 10 | ans = 1 11 | 12 | while (y > 0): 13 | if (y%2 != 0): ans = (ans * x) % m 14 | x = (x * x) % m 15 | y = y // 2 16 | return ans; 17 | 18 | def inv(n): 19 | return powerMod(n, M - 2, M) 20 | 21 | memo = {} 22 | 23 | def nck(n, k): 24 | if k > n: return 0 25 | 26 | if (n * 4384 + k * 90210 in memo): return memo[n * 4384 + k * 90210] 27 | memo[n * 4384 + k * 90210] = (((fac[n] * inv(fac[n-k])) % M) * inv(fac[k])) % M 28 | return memo[n * 4384 + k * 90210] 29 | 30 | T = int(raw_input()) 31 | for cs in range(1, T+1): 32 | N, K = map(int, raw_input().split()) 33 | A = list(map(int, raw_input().split())) 34 | A.sort() 35 | 36 | ans = 0 37 | for i in range(N): 38 | ans = (ans + A[i] * nck(i + 1, K) * K * inv(i + 1)) % M 39 | 40 | print("Case #{}: {}".format(cs, ans)) -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 5/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int MAXN = 1e6 + 5; 14 | 15 | int a[MAXN]; 16 | 17 | int main() { 18 | int n, k; scanf("%d%d", &n, &k); 19 | for (int i = 1; i <= n; i++) scanf("%d", a+i); 20 | sort(a+1, a+n+1); 21 | 22 | int ans = 0; 23 | for (int i = 1; i < MAXN; i++) { 24 | 25 | int l = 1; 26 | for (int j = i; l <= n && j < MAXN; j += i) { 27 | if (a[l] - k <= j && a[l] >= j) { 28 | l = upper_bound(a+1, a+n+1, j + k) - a; 29 | } 30 | } 31 | 32 | if (l > n) ans = i; 33 | } 34 | 35 | printf("%d\n", ans); 36 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 6/I: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 6/I -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 6/in: -------------------------------------------------------------------------------- 1 | 4 4 2 | 1 2 9 3 3 | 3 5 4 8 4 | 4 3 2 7 5 | 5 8 1 6 6 | 4 7 | move 1 8 | move 1 9 | change 1 4 100 10 | move 1 -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Long Challenge/GEMA Long Challenge 6/in2: -------------------------------------------------------------------------------- 1 | 3 4 2 | 10 20 30 40 3 | 50 60 70 80 4 | 90 93 95 99 5 | 3 6 | move 4 7 | change 2 1 100 8 | move 4 -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Strings/GEMA Strings 01/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); cin.tie(NULL); 6 | int T; cin >> T; 7 | while(T--) { 8 | string t, p; cin >> t >> p; 9 | 10 | string s = p + "#" + t; 11 | int n = s.size(), m = p.size(); 12 | vector z(n, 0); 13 | for (int i = 1, l = 0, r = 0; i < n; i++) { 14 | if (i <= r) 15 | z[i] = min(r - i + 1, z[i - l]); 16 | 17 | while(i + z[i] < n && s[z[i]+i] == s[z[i]]) 18 | z[i]++; 19 | 20 | if (z[i] + i - 1 > r) 21 | l = i, r = i + z[i] - 1; 22 | } 23 | 24 | vector ans; 25 | for (int i = m+1; i < n; i++) { 26 | if (z[i] == m) ans.push_back(i - m); 27 | } 28 | 29 | if (ans.empty()) cout << "Not Found\n"; 30 | else { 31 | cout << ans.size() << "\n"; 32 | for (int i : ans) cout << i << " "; 33 | cout << "\n"; 34 | } 35 | 36 | if (T) cout << "\n"; 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Strings/GEMA Strings 01/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); cin.tie(NULL); 6 | 7 | string s; 8 | while(cin >> s, s != ".") { 9 | int n = s.size(); 10 | vector z(n, 0); 11 | 12 | for (int i = 1, l = 0, r = 0; i < n; i++) { 13 | if (i <= r) 14 | z[i] = min(r - i + 1, z[i - l]); 15 | 16 | while(i + z[i] < n && s[z[i]+i] == s[z[i]]) 17 | z[i]++; 18 | 19 | if (z[i] + i - 1 > r) 20 | l = i, r = i + z[i] - 1; 21 | } 22 | 23 | int ans = n; 24 | for (int i = 1; i < ans; i++) { 25 | if (i + z[i] == n && n%i == 0) 26 | ans = i; 27 | } 28 | 29 | cout << n/ans << '\n'; 30 | } 31 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Strings/GEMA Strings 01/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector build_Z(string& s) { 5 | int n = s.size(); 6 | vector z(n, 0); 7 | 8 | for (int i = 1, l = 0, r = 0; i < n; i++) { 9 | if (i <= r) 10 | z[i] = min(r - i + 1, z[i - l]); 11 | 12 | while(i + z[i] < n && s[z[i]+i] == s[z[i]]) 13 | z[i]++; 14 | 15 | if (z[i] + i - 1 > r) 16 | l = i, r = i + z[i] - 1; 17 | } 18 | return z; 19 | } 20 | 21 | int main() { 22 | ios::sync_with_stdio(false); cin.tie(NULL); 23 | 24 | string s; cin >> s; 25 | int n = s.size(); 26 | 27 | vector z = build_Z(s); 28 | 29 | int m = 0, ans = 0; 30 | for (int i = 0; i < n; i++) { 31 | if (z[i] + i == n && z[i] <= m) ans = max(ans, n - i); 32 | m = max(m, z[i]); 33 | } 34 | 35 | if (ans) cout << s.substr(0, ans) << "\n"; 36 | else cout << "Just a legend\n"; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Strings/GEMA Strings 01/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector build_Z(string& s) { 5 | int n = s.size(); 6 | vector z(n, 0); 7 | 8 | for (int i = 1, l = 0, r = 0; i < n; i++) { 9 | if (i <= r) 10 | z[i] = min(r - i + 1, z[i - l]); 11 | 12 | while(i + z[i] < n && s[z[i]+i] == s[z[i]]) 13 | z[i]++; 14 | 15 | if (z[i] + i - 1 > r) 16 | l = i, r = i + z[i] - 1; 17 | } 18 | return z; 19 | } 20 | 21 | int main() { 22 | ios::sync_with_stdio(false); cin.tie(NULL); 23 | 24 | string s; 25 | while(cin >> s) { 26 | int n = s.size(); 27 | 28 | string t = s; 29 | reverse(t.begin(), t.end()); 30 | 31 | string y = t + "#" + s; 32 | vector z = build_Z(y); 33 | 34 | int ans = n; 35 | for (int i = n + 1; i < 2 * n + 1; i++) { 36 | if (z[i] + i - n - 1 == n) 37 | ans = min(ans, n - z[i]); 38 | } 39 | 40 | string S; 41 | for (int i = 0; i < ans; i++) S.push_back(s[i]); 42 | reverse(S.begin(), S.end()); 43 | 44 | S = s + S; 45 | 46 | cout << S << '\n'; 47 | } 48 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Strings/GEMA Strings 01/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector build_Z(string& s) { 5 | int n = s.size(); 6 | vector z(n, 0); 7 | 8 | for (int i = 1, l = 0, r = 0; i < n; i++) { 9 | if (i <= r) 10 | z[i] = min(r - i + 1, z[i - l]); 11 | 12 | while(i + z[i] < n && s[z[i]+i] == s[z[i]]) 13 | z[i]++; 14 | 15 | if (z[i] + i - 1 > r) 16 | l = i, r = i + z[i] - 1; 17 | } 18 | return z; 19 | } 20 | 21 | int main() { 22 | ios::sync_with_stdio(false); cin.tie(NULL); 23 | 24 | string s; cin >> s; 25 | int m = s.size(); 26 | 27 | s = s + "#" + s; 28 | int n = s.size(); 29 | 30 | vector z = build_Z(s); 31 | 32 | vector freq(m+1, 0), ans; 33 | for (int i = m+1; i < n; i++) { 34 | if (z[i] + i == n) ans.push_back(z[i]); 35 | 36 | freq[z[i]]++; 37 | } 38 | 39 | reverse(ans.begin(), ans.end()); 40 | 41 | cout << ans.size() << '\n'; 42 | 43 | int sum = n - m - 1; 44 | for (int i = 0, l = 0; i <= m; i++) { 45 | if (l < (int)ans.size() && ans[l] == i) { 46 | cout << ans[l] << " " << sum << "\n"; 47 | l++; 48 | } 49 | 50 | sum -= freq[i]; 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Strings/GEMA Strings 02/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define eb emplace_back 5 | #define pb push_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef pair pii; 11 | typedef long long ll; 12 | 13 | const int MAXN = 1e5 + 5; 14 | 15 | vector prefixFunction(string& s) { 16 | int n = (int)s.length(); 17 | vector pi(n); 18 | for (int i = 1; i < n; i++) { 19 | int j = pi[i-1]; 20 | while (j > 0 && s[i] != s[j]) j = pi[j-1]; 21 | if (s[i] == s[j]) j++; 22 | pi[i] = j; 23 | } 24 | 25 | return pi; 26 | } 27 | 28 | int main() { 29 | ios::sync_with_stdio(false); cin.tie(NULL); 30 | 31 | int t; cin >> t; 32 | while(t--) { 33 | string s; cin >> s; 34 | ll k; cin >> k; 35 | ll n = s.size(); 36 | 37 | vector pi = prefixFunction(s); 38 | 39 | ll j = pi[n-1]; 40 | cout << n + (n - j) * (k - 1) << '\n'; 41 | } 42 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Strings/GEMA Strings 02/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define eb emplace_back 5 | #define pb push_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef pair pii; 11 | typedef long long ll; 12 | 13 | const int MAXN = 1e5 + 5; 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); cin.tie(NULL); 17 | int n; cin >> n; 18 | 19 | vector z(n); 20 | for (int i = 0; i < n; i++) cin >> z[i]; 21 | 22 | vector pf(n); 23 | for (int i = 0; i < n; i++) { 24 | if (z[i]) { 25 | pf[i + z[i] - 1] = max(pf[i + z[i] - 1], z[i]); 26 | } 27 | } 28 | int cur = 0; 29 | for (int i = n-1; i >= 0; i--) { 30 | if (cur > 1 && cur > pf[i]) pf[i] = --cur; 31 | if (pf[i]) cur = pf[i]; 32 | } 33 | 34 | for (int i : pf) cout << i << ' '; 35 | cout << '\n'; 36 | } -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Tree/1/D: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/GEMA/GEMA Tree/1/D -------------------------------------------------------------------------------- /problems/solutions/GEMA/GEMA Tree/1/in: -------------------------------------------------------------------------------- 1 | 2 2 | 6 7 3 | 3 1 3 4 4 4 | 1 5 | 5 6 | 6 7 | 1 8 | 3 9 | 5 10 | 2 11 | 2 2 12 | 1 13 | 2 14 | 1 -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2019/1C/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | typedef pair pii; 7 | typedef long long ll; 8 | 9 | const int MAXN = 1e5 + 5; 10 | 11 | char grid[20][20]; 12 | 13 | int main() { 14 | ios::sync_with_stdio(false); cin.tie(NULL); 15 | int t; cin >> t; 16 | for (int cases = 1; cases <= t; cases++) { 17 | int ans = 0; 18 | int r, c; cin >> r >> c; 19 | for (int i = 1; i <= r; i++) { 20 | for (int j = 1; j <= c; j++) { 21 | cin >> grid[i][j]; 22 | } 23 | } 24 | 25 | for (int i = ) 26 | 27 | cout << "Case #" << cases << ": " << ans << '\n'; 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2019/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); cin.tie(NULL); 6 | 7 | int t; cin >> t; 8 | for (int cs = 1; cs <= t; cs++) { 9 | string s; cin >> s; 10 | string a, bb; 11 | 12 | for (int i = 0; i < s.size(); i++) { 13 | if (s[i] != '4') { 14 | a += s[i]; 15 | bb += '0'; 16 | } else { 17 | a += '3'; 18 | bb += '1'; 19 | } 20 | } 21 | 22 | string b; 23 | for (int i = 0, hs = 0; i < bb.size(); i++) { 24 | if (bb[i] != '0') hs = 1; 25 | if (hs) b += bb[i]; 26 | } 27 | 28 | cout << "Case #" << cs << ": " << a << " " << b << "\n"; 29 | } 30 | } -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2019/A.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | while(t > 0): 4 | n = str(input()) 5 | a = 0 6 | b = 0 7 | 8 | for i in range(0, len(n)) 9 | 10 | t -= 1 -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2019/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); cin.tie(NULL); 6 | 7 | int t; cin >> t; 8 | for (int cs = 1; cs <= t; cs++) { 9 | int n; cin >> n; 10 | string s; cin >> s; 11 | 12 | string ans; 13 | for (char c : s) 14 | ans += c == 'E' ? 'S' : 'E'; 15 | 16 | cout << "Case #" << cs << ": " << ans << "\n"; 17 | } 18 | } -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1B/A: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1B/A -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1B/A2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1B/A2 -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1B/in: -------------------------------------------------------------------------------- 1 | 8 2 | 2 3 3 | -2 -3 4 | 3 0 5 | -1 1 6 | 2 3 7 | -2 -3 8 | 3 0 9 | -1 1 -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/A: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1C/A -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/B: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1C/B -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/B2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1C/B2 -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1C/C -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/C$FastReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1C/C$FastReader.class -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/C.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1C/C.class -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/C.py: -------------------------------------------------------------------------------- 1 | T = int(input()) 2 | -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/gen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Google Codejam 2020/1C/gen -------------------------------------------------------------------------------- /problems/solutions/Google/Google Codejam 2020/1C/in: -------------------------------------------------------------------------------- 1 | 4 2 | 1 3 3 | 1 4 | 5 2 5 | 10 5 359999999999 123456789 10 6 | 2 3 7 | 8 4 8 | 3 2 9 | 1 2 3 -------------------------------------------------------------------------------- /problems/solutions/Google/Hash Code 2021/HashCode2021.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Hash Code 2021/HashCode2021.tar.gz -------------------------------------------------------------------------------- /problems/solutions/Google/Hash Code 2021/Practice/.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Hash Code 2021/Practice/.out -------------------------------------------------------------------------------- /problems/solutions/Google/Hash Code 2021/Practice/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -static -Wall -O2 -std=c++17 $f.cpp -o $f 3 | 4 | slow: 5 | g++ $(f).cpp -o $(f) -Wall -std=c++17 -DLOCAL_PC -fsanitize=address,undefined -ggdb3 6 | 7 | run: 8 | ./$f < cases/$(case).in 1> output/$(case)_$(shell ls output | wc -l 2> /dev/null).out 9 | 10 | run_all: 11 | make run f=$f case=a 12 | make run f=$f case=b 13 | make run f=$f case=c 14 | make run f=$f case=d 15 | make run f=$f case=e -------------------------------------------------------------------------------- /problems/solutions/Google/Hash Code 2021/Practice/cases/a.in: -------------------------------------------------------------------------------- 1 | 5 1 2 1 2 | 3 onion pepper olive 3 | 3 mushroom tomato basil 4 | 3 chicken mushroom pepper 5 | 3 tomato mushroom basil 6 | 2 chicken basil 7 | -------------------------------------------------------------------------------- /problems/solutions/Google/Hash Code 2021/Practice/statements.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Google/Hash Code 2021/Practice/statements.pdf -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/A/A: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/HackerCup/2020/Round 2/A/A -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/A/out: -------------------------------------------------------------------------------- 1 | Case #1: 1 2 | Case #2: 4 3 | Case #3: -1 4 | Case #4: 7 5 | Case #5: -1 6 | Case #6: 5172260357 7 | Case #7: 0 8 | -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/A/sample.in: -------------------------------------------------------------------------------- 1 | 7 2 | 2 2 3 | 10 20 4 | 0 0 0 100 5 | 9 21 6 | 0 0 0 100 7 | 0 0 8 | 0 0 0 100 9 | 2 2 10 | 10 20 11 | 0 0 0 100 12 | 14 14 13 | 0 0 0 100 14 | 4 4 15 | 0 0 0 100 16 | 2 2 17 | 3 5 18 | 0 0 0 100 19 | 2 7 20 | 0 0 0 100 21 | 2 0 22 | 0 0 0 100 23 | 4 4 24 | 8 2 5 7 25 | 0 0 0 100 26 | 2 0 7 2 27 | 0 0 0 100 28 | 3 5 4 1 29 | 0 0 0 100 30 | 5 2 31 | 15 12 32 | 1 2 3 20 33 | 1 9 34 | 2 3 4 10 35 | 0 6 36 | 3 4 5 10 37 | 100 2 38 | 267483417 24355960 39 | 870455564 423903642 12577889 341064999 40 | 227341573 171278940 41 | 619862831 424929478 165711514 317464772 42 | 110095480 184821992 43 | 618014398 840039189 11907142 234063725 44 | 1000 2 45 | 0 1 46 | 0 1 1 2 47 | 0 1 48 | 0 1 1 2 49 | 0 1 50 | 0 1 1 2 51 | -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/A/sample.out: -------------------------------------------------------------------------------- 1 | Case #1: 1 2 | Case #2: 4 3 | Case #3: -1 4 | Case #4: 7 5 | Case #5: -1 6 | Case #6: 5172260357 7 | Case #7: 0 8 | -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/A/val.out: -------------------------------------------------------------------------------- 1 | Case #1: 1 2 | Case #2: 4 3 | Case #3: -1 4 | Case #4: 7 5 | Case #5: -1 6 | Case #6: 5172260357 7 | Case #7: 0 8 | Case #8: 175394700 9 | Case #9: -1 10 | Case #10: 999999 11 | -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/B/B: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/HackerCup/2020/Round 2/B/B -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/B/B2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/HackerCup/2020/Round 2/B/B2 -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/B/in: -------------------------------------------------------------------------------- 1 | 3 2 | 2 1.0 3 | 3 1.0 4 | 4 1.0 -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/B/in1: -------------------------------------------------------------------------------- 1 | 4 1.0 -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/B/sample.in: -------------------------------------------------------------------------------- 1 | 6 2 | 2 1.0 3 | 3 1.0 4 | 4 1.0 5 | 3 0.5 6 | 5 0.678 7 | 9 0.87 8 | -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/B/sample.out: -------------------------------------------------------------------------------- 1 | Case #1: 2 | 1.00000000 3 | 1.00000000 4 | Case #2: 5 | 1.33333333 6 | 1.66666667 7 | 2.00000000 8 | Case #3: 9 | 1.66666667 10 | 1.94444444 11 | 2.38888889 12 | 3.00000000 13 | Case #4: 14 | 1.66666667 15 | 1.66666667 16 | 1.66666667 17 | Case #5: 18 | 2.47452857 19 | 2.61417298 20 | 2.77465280 21 | 2.96047982 22 | 3.17616583 23 | Case #6: 24 | 3.64469088 25 | 3.83725603 26 | 4.05825232 27 | 4.31581280 28 | 4.62193375 29 | 4.99526004 30 | 5.46668555 31 | 6.09114388 32 | 6.96896476 33 | -------------------------------------------------------------------------------- /problems/solutions/HackerCup/2020/Round 2/B/stupid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/HackerCup/2020/Round 2/B/stupid -------------------------------------------------------------------------------- /problems/solutions/Hackerearth/FebEasy/goodsub.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MOD = 1e9 + 7; 5 | int freq[30]; 6 | 7 | int main() { 8 | ios::sync_with_stdio(false); cin.tie(NULL); 9 | int t; cin >> t; 10 | 11 | string s; 12 | while (t--) { 13 | memset(freq, 0, sizeof freq); 14 | 15 | cin >> s; 16 | sort(s.begin(), s.end()); 17 | for (char c : s) freq[c - 'a']++; 18 | 19 | long long pos = 1; 20 | for (int i = 0; i < 26; i++) { 21 | if (freq[i] == 0) continue; 22 | pos = (pos * freq[i]) % MOD; 23 | } 24 | 25 | cout << pos << '\n'; 26 | } 27 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos 2 2020/01/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 3e5 + 5; 5 | 6 | char s[MAXN]; 7 | inline void _solve_(int __ntest__) { 8 | int n; cin >> n; 9 | vector price(1024); 10 | for (int i = 0; i < n; i++) { 11 | unsigned char c; 12 | int p; 13 | cin >> c >> p; 14 | price[c] = p; 15 | } 16 | 17 | int k; cin >> k; 18 | 19 | long long ans = 0; 20 | string s; 21 | cin.ignore(); 22 | for (int i = 0; i < k; i++) { 23 | getline(cin, s); 24 | for (int j = 0; j < (int) s.size(); j++) 25 | ans += price[s[j]]; 26 | } 27 | 28 | cout << ans/100 << '.'; 29 | if (ans%100 < 10) cout << 0; 30 | cout << ans%100 << "$"; 31 | } 32 | 33 | int main() { 34 | ios::sync_with_stdio(false); cin.tie(NULL); 35 | int __T__; cin >> __T__; 36 | for (int __i__ = 1; __i__ <= __T__; __i__++) { 37 | _solve_(__i__); 38 | cout << "\n"; 39 | } 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos 2 2020/01/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int T; scanf("%d", &T); 6 | for (int cs = 1; cs <= T; cs++) { 7 | int n; scanf("%d", &n); 8 | vector> a(n); 9 | 10 | map f; 11 | for (vector& v : a) { 12 | int k; scanf("%d", &k); 13 | v.resize(k); 14 | for (int& i : v) scanf("%d", &i); 15 | sort(v.begin(), v.end()); 16 | v.erase(unique(v.begin(), v.end()), v.end()); 17 | for (int i : v) f[i]++; 18 | } 19 | 20 | int cnt = 0; 21 | for (auto p : f) cnt += p.second == 1; 22 | 23 | printf("Case %d: ", cs); 24 | for (int i = 0; i < n; i++) { 25 | if (i) printf(" "); 26 | 27 | int cur = 0; 28 | for (int x : a[i]) cur += f[x] == 1; 29 | printf("%.6lf%%", cnt == 0 ? 0 : 100.0 * cur / cnt); 30 | } 31 | printf("\n"); 32 | } 33 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos 2 2020/03/B/0.in: -------------------------------------------------------------------------------- 1 | 2 2 | 2 3 4 0 3 | 1 5 7 8 4 | 9 6 10 12 5 | 13 14 11 15 6 | 13 1 2 4 7 | 5 0 3 7 8 | 9 6 10 12 9 | 15 8 11 14 -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos 2 2020/03/B/0.out: -------------------------------------------------------------------------------- 1 | drdruulddruuldlurrldldrrulldrrulurdd 2 | 3 | rdruulddruuldlurrldldrrulldrrulurdd 4 | 5 | ddruulddruuldlurrldldrrulldrrulurdldr 6 | 7 | rruldluurddluurdruldrdllurrdllururdd 8 | 9 | druulddruuldlurrldldrrulldrrulurdd 10 | 11 | druulddruuldlurrldldrrulldrrulurdldr 12 | 13 | dldruulddruuldlurrldldrrulldrrulurdlldrr 14 | 15 | ululdrrulldrurldrdluurdd 16 | 17 | dluldrrulldrurldrdluldrurdllururdd 18 | 19 | uldrrulldrurldrdluldrurdllururdd 20 | 21 | uurrdllurdrullddrurdlulurrdd 22 | 23 | ruldrruuldrulddrulurdlldrrulldrr 24 | -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos 2 2020/03/B/2.in: -------------------------------------------------------------------------------- 1 | 1 2 3 4 2 | 6 7 8 0 3 | 5 10 11 12 4 | 9 13 14 15 -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos 2 2020/03/B/out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Lab de Algoritmos 2 2020/03/B/out -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos 2 2020/03/B/sol: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Lab de Algoritmos 2 2020/03/B/sol -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos 2 2020/03/B/sol2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Lab de Algoritmos 2 2020/03/B/sol2 -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/01/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | string s; cin >> s; 8 | int qtd = 0; 9 | for (int i = 0; i < (int)s.size(); i++) { 10 | char c = s[i]; 11 | if (c == '(') qtd++; 12 | else { 13 | if (qtd == 0) { 14 | cout << "NO\n"; 15 | return 0; 16 | } else qtd--; 17 | } 18 | } 19 | 20 | if (qtd == 0) cout << "YES\n"; 21 | else cout << "NO\n"; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/01/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int n; scanf("%d", &n); 9 | 10 | int ans = 0; 11 | map freq; 12 | while(n--) { 13 | int x; scanf("%d", &x); 14 | freq[x]++; 15 | if (freq[x] == 1) ans++; 16 | } 17 | 18 | printf("%d\n", ans); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/01/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int n; 10 | while (scanf("%d ", &n), n) { 11 | map freq; 12 | int maxi = 0; 13 | for (int i = 1; i <= n; i++) { 14 | int act[10]; 15 | for (int i = 0; i < 5; i++) scanf("%d", &act[i]); 16 | sort(act, act+5); 17 | 18 | string rep = ""; 19 | for (int i = 0; i < 5; i++) 20 | rep += to_string(act[i]); 21 | 22 | freq[rep]++; 23 | maxi = max(maxi, freq[rep]); 24 | } 25 | 26 | int ans = 0; 27 | for (auto it : freq) { 28 | if (it.second == maxi) { 29 | ans += it.second; 30 | } 31 | } 32 | 33 | printf("%d\n", ans); 34 | } 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/02/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int MAXN = 200005; 7 | 8 | int a[MAXN]; 9 | char pos[MAXN]; 10 | 11 | int main() { 12 | int n; scanf("%d", &n); 13 | 14 | for (int i = 1; i <= n; i++) scanf("%d ", a+i); 15 | scanf("%s", pos+1); 16 | 17 | vector > ranges; 18 | for (int i = 1; i <= n; i++) { 19 | if (pos[i] == '0') continue; 20 | 21 | int j; 22 | for (j = i+1; j < n; j++) 23 | if (pos[j] == '0') break; 24 | 25 | ranges.push_back(make_pair(i, j)); 26 | i = j+1; 27 | } 28 | 29 | for (int i = 0; i < (int)ranges.size(); i++) { 30 | int u = ranges[i].first; 31 | int v = ranges[i].second; 32 | sort(a+u, a+v+1); 33 | } 34 | 35 | for (int i = 1; i < n; i++) 36 | if (a[i] > a[i+1]) return !printf("NO\n"); 37 | 38 | return !printf("YES\n"); 39 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/02/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int n; 8 | while(~scanf("%d", &n)) { 9 | stack s; 10 | queue q; 11 | priority_queue pq; 12 | bool cS, cQ, cPQ; 13 | cS = cQ = cPQ = true; 14 | 15 | while(n--) { 16 | int op, x; scanf("%d%d", &op, &x); 17 | 18 | if (op == 1) { 19 | s.push(x); 20 | q.push(x); 21 | pq.push(x); 22 | } else { 23 | if (s.size() == 0) cS = false; 24 | if (q.size() == 0) cQ = false; 25 | if (pq.size() == 0) cPQ = false; 26 | 27 | if (cS and s.top() != x) cS = false; 28 | if (cQ and q.front() != x) cQ = false; 29 | if (cPQ and pq.top() != x) cPQ = false; 30 | 31 | if (cS) s.pop(); 32 | if (cQ) q.pop(); 33 | if (cPQ) pq.pop(); 34 | } 35 | } 36 | 37 | if (cS and !cQ and !cPQ) printf("stack\n"); 38 | else if (!cS and cQ and !cPQ) printf("queue\n"); 39 | else if (!cS and !cQ and cPQ) printf("priority queue\n"); 40 | else if (!cS and !cQ and !cPQ) printf("impossible\n"); 41 | else printf("not sure\n"); 42 | } 43 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/02/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | char inp[50]; 7 | int q, p; 8 | 9 | priority_queue > pq; 10 | while (scanf("%s ", inp), inp[0] != '#') { 11 | scanf("%d %d ", &q, &p); 12 | 13 | for (int i = 1; i <= 10000; i++) { 14 | pq.push(make_pair(-i*p, -q)); 15 | } 16 | } 17 | 18 | int k; scanf("%d", &k); 19 | 20 | while(k--) { 21 | printf("%d\n", -pq.top().second); 22 | pq.pop(); 23 | } 24 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/03/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int MAXN = 20; 6 | 7 | bool isUsed(int i, int bmask) { 8 | return bmask & (1 << i); 9 | } 10 | 11 | int newBMask(int bmask, int i) { 12 | return bmask | (1 << i); 13 | } 14 | 15 | bool isPrime(int x) { 16 | for (int i = 2; i < x; i++) if (x%i == 0) return false; 17 | return true; 18 | } 19 | 20 | vector cur; 21 | int n; 22 | 23 | void solve(int qtd, int num, int bmask) { 24 | if (qtd == n) { 25 | if (isPrime(1+num)) { 26 | for (int i : cur) printf("%d ", i); 27 | printf("\n"); 28 | return; 29 | } 30 | } 31 | 32 | for (int i = 1; i <= n; i++) { 33 | if (isUsed(i, bmask)) continue; 34 | 35 | if (isPrime(num+i)) { 36 | cur.push_back(i); 37 | solve(qtd+1, i, newBMask(bmask, i)); 38 | cur.pop_back(); 39 | } 40 | } 41 | } 42 | 43 | int main() { 44 | int ncase = 0; 45 | while (~scanf("%d", &n)) { 46 | if (ncase != 0) printf("\n"); 47 | printf("Case %d:\n", ++ncase); 48 | cur.push_back(1); 49 | 50 | solve(1, 1, newBMask(0, 1)); 51 | 52 | cur.clear(); 53 | } 54 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/03/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n; scanf("%d", &n); 5 | while(n--) { 6 | int a, b, c; scanf("%d%d%d", &a, &b, &c); 7 | 8 | for (int x = 1; x <= a; x++) { 9 | for (int y = 1; y <= c-(x*x); y++) { 10 | for (int z = 1; z <= c-(x*x)-(y*y); z++) { 11 | if (x + y + z == a && 12 | x*y*z == b && 13 | x*x + y*y + z*z == c) { 14 | printf("%d %d %d\n", x, y, z); 15 | goto LBL; 16 | } 17 | } 18 | } 19 | } 20 | 21 | printf("No solution.\n"); 22 | 23 | LBL: 24 | continue; 25 | } 26 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/03/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | const int MAXN = 20; 9 | int a[MAXN], t, n, qtt; 10 | 11 | vector cur; 12 | 13 | set used; 14 | 15 | void solve(int i, int curSum) { 16 | if (curSum == t) { 17 | string curAns; 18 | for (int k = 0; k < (int)cur.size(); k++) { 19 | curAns += to_string(cur[k]); 20 | if (k != (int)cur.size()-1) curAns += "+"; 21 | } 22 | 23 | if (used.find(curAns) == used.end()) { 24 | cout << curAns << "\n"; 25 | used.insert(curAns); 26 | } 27 | 28 | return; 29 | } 30 | 31 | if (i > n) return; 32 | 33 | cur.push_back(a[i]); 34 | solve(i+1, curSum + a[i]); 35 | cur.pop_back(); 36 | 37 | solve(i+1, curSum); 38 | } 39 | 40 | int main() { 41 | ios::sync_with_stdio(false); 42 | cin.tie(NULL); 43 | 44 | while (cin >> t >> n, t && n) { 45 | for (int i = 1; i <= n; i++) cin >> a[i]; 46 | cout << "Sums of " << t << ":\n"; 47 | 48 | solve(1, 0); 49 | 50 | if (used.size() == 0) cout << "NONE\n"; 51 | used.clear(); 52 | } 53 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/06/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 1e6 + 50; 5 | 6 | bool notPrime[MAXN]; 7 | 8 | int rev(int i) { 9 | int num = 0; 10 | while(i > 0) { 11 | num = num * 10 + i%10; 12 | i /= 10; 13 | } 14 | return num; 15 | } 16 | 17 | void sieve(int N) { 18 | for (int i = 2; i <= N; i++) { 19 | if (notPrime[i]) continue; 20 | for (int j = 2 * i; j <= N; j += i) 21 | notPrime[j] = true; 22 | } 23 | } 24 | 25 | int main() { 26 | 27 | sieve(MAXN-1); 28 | 29 | int n; 30 | while(~scanf("%d", &n)) { 31 | if (rev(n) != n and !notPrime[n] && !notPrime[rev(n)]) printf("%d is emirp.\n", n); 32 | else if (!notPrime[n]) printf("%d is prime.\n", n); 33 | else printf("%d is not prime.\n", n); 34 | } 35 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/06/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool solve(int n, int m) { 7 | map fac; 8 | for (int i = 2; i*i <= m; i++) { 9 | while (m%i == 0) { 10 | m /= i; 11 | fac[i]++; 12 | } 13 | } 14 | 15 | if (m > 1) fac[m]++; 16 | 17 | int p, aux, qtd; 18 | 19 | for (auto it : fac) { 20 | p = it.first; 21 | aux = n; 22 | qtd = 0; 23 | while (aux) qtd += (aux/=p); 24 | if (qtd < it.second) return false; 25 | } 26 | 27 | return true; 28 | } 29 | 30 | int main() { 31 | int n, m; 32 | while (~scanf("%d%d", &n, &m)) { 33 | if (solve(n, m)) printf("%d divides %d!\n", m, n); 34 | else printf("%d does not divide %d!\n", m, n); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/06/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | vector a; 8 | int n; 9 | NEW_VEC: 10 | a.clear(); 11 | while (scanf("%d", &n), n != 0) { 12 | a.push_back(n); 13 | } 14 | 15 | if (a.size() == 0) return 0; 16 | 17 | sort(a.begin(), a.end()); 18 | 19 | int gcd = a[1] - a[0]; 20 | for (int i = 0; i < (int)a.size(); i++) { 21 | for (int j = i+1; j < (int)a.size(); j++) { 22 | gcd = __gcd(gcd, a[j] - a[i]); 23 | } 24 | } 25 | 26 | printf("%d\n", gcd); 27 | 28 | goto NEW_VEC; 29 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/07/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int n = 131071; 6 | 7 | bool validate(string& s) { 8 | int r = 0; 9 | 10 | for (char c : s) { 11 | if (c == '#') break; 12 | 13 | if (c == '1') r = (r << 1 | 1) % n; 14 | else r = (r << 1) % n; 15 | } 16 | 17 | return r%n == 0; 18 | } 19 | 20 | int main() { 21 | ios::sync_with_stdio(false); cin.tie(NULL); 22 | 23 | string s; 24 | while (cin >> s) { 25 | if (validate(s)) cout << "YES\n"; 26 | else cout << "NO\n"; 27 | } 28 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/07/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | vector divi(int n) { 8 | vector ret; 9 | for (int i = 1; i * i <= n; i++) { 10 | if (n%i == 0) { 11 | ret.push_back(i); 12 | if (i*i != n) ret.push_back(n/i); 13 | } 14 | } 15 | return ret; 16 | } 17 | 18 | int lcm(int a, int b) { 19 | return a/ __gcd(a, b) * b; 20 | } 21 | 22 | int solve(int n) { 23 | vector div = divi(n); 24 | 25 | set > st; 26 | 27 | for (int i : div) { 28 | for (int j : div) { 29 | if (lcm(i, j) == n) 30 | st.insert({min(i, j), max(i, j)}); 31 | } 32 | } 33 | 34 | return st.size(); 35 | } 36 | 37 | int main() { 38 | int n; 39 | while(scanf("%d", &n), n) { 40 | printf("%d %d\n", n, solve(n)); 41 | } 42 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/08/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int p, q, r, s, t, u; 7 | 8 | const double eps = 1e-12; 9 | 10 | double eval(double x) { 11 | double ans = p * pow(exp(1.0), -x); 12 | ans += q * sin(x); 13 | ans += r * cos(x); 14 | ans += s * tan(x); 15 | ans += t * x * x; 16 | ans += u; 17 | 18 | return ans; 19 | } 20 | 21 | int main() { 22 | while(~scanf("%d%d%d%d%d%d", &p, &q, &r, &s, &t, &u)) { 23 | double ans = -1, lo = 0, hi = 1, mi; 24 | int cnt = 500; 25 | 26 | while(cnt--) { 27 | mi = (lo + hi) / 2; 28 | 29 | double cur = eval(mi); 30 | if (cur+eps < 0) hi = mi; 31 | else lo = mi; 32 | 33 | if (abs(cur) < eps) ans = mi; 34 | } 35 | 36 | if (ans < 0) printf("No solution\n"); 37 | else printf("%.4lf\n", ans); 38 | } 39 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/08/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std;; 3 | 4 | int n, k, a[700]; 5 | 6 | bool check(int m) { 7 | int cur = 0, qtt = 0; 8 | for (int i = 1; i <= n+1; i++) { 9 | if (a[i] > m) return false; 10 | 11 | if (cur + a[i] > m) { 12 | qtt++; 13 | cur = a[i]; 14 | } else cur += a[i]; 15 | } 16 | 17 | if (cur > m) return false; 18 | return qtt <= k; 19 | } 20 | 21 | int main() { 22 | while(~scanf("%d%d", &n, &k)) { 23 | for (int i = 1; i <= n+1; i++) scanf("%d", a+i); 24 | 25 | int lo = 1, hi = 1e9, mi; 26 | while(lo < hi) { 27 | mi = (lo+hi)/2; 28 | 29 | if (check(mi)) hi = mi; 30 | else lo = mi+1; 31 | } 32 | 33 | printf("%d\n", hi); 34 | } 35 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/09/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define pb push_back 7 | const int MAXN = 300; 8 | 9 | int color[MAXN]; 10 | 11 | bool dfs(int u, int c, vector >& edges) { 12 | if (color[u] && color[u] != c) return false; 13 | if (color[u]) return true; 14 | 15 | color[u] = c; 16 | for (int v : edges[u]) 17 | if (!dfs(v, 3 - c, edges)) return false; 18 | 19 | return true; 20 | } 21 | 22 | 23 | int main() { 24 | 25 | int n, m; 26 | while (scanf("%d%d", &n, &m) > 1 && n != 0) { 27 | vector > edges(n + 1); 28 | while(m--) { 29 | int u, v; scanf("%d%d", &u, &v); 30 | edges[u].pb(v); 31 | edges[v].pb(u); 32 | } 33 | 34 | memset(color, 0, sizeof color); 35 | bool ans = true; 36 | 37 | for (int i = 1; i <= n; i++) { 38 | if (!color[i]) 39 | ans = ans && dfs(i, 1, edges); 40 | } 41 | 42 | printf("%s.\n", ans ? "BICOLORABLE" : "NOT BICOLORABLE"); 43 | } 44 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/09/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void dfs(int u, vector& vis, vector >& edges) { 7 | vis[u] = true; 8 | for (int v : edges[u]) 9 | if (!vis[v]) dfs(v, vis, edges); 10 | } 11 | 12 | int main() { 13 | ios::sync_with_stdio(false); cin.tie(NULL); 14 | int t; cin >> t; 15 | char m; cin >> m; 16 | 17 | while(t--) { 18 | int n = m - 'A'; 19 | vector > edges(n + 1); 20 | 21 | string s; 22 | while(cin >> s) { 23 | if (s.size() == 1) { 24 | m = s[0]; 25 | break; 26 | } 27 | 28 | int u = s[0] - 'A'; 29 | int v = s[1] - 'A'; 30 | 31 | edges[u].push_back(v); 32 | edges[v].push_back(u); 33 | } 34 | 35 | 36 | vector vis(n + 1, false); 37 | int ans = 0; 38 | for (int u = 0; u <= n; u++) { 39 | if (!vis[u]) { 40 | ans++; 41 | dfs(u, vis, edges); 42 | } 43 | } 44 | 45 | cout << ans; 46 | if (t) cout << "\n\n"; 47 | else cout << "\n"; 48 | } 49 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/09/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void dfs(int u, vector& vis, vector >& edges, vector& ans) { 7 | if (vis[u]) return; 8 | vis[u] = true; 9 | 10 | for (int v : edges[u]) { 11 | if (!vis[v]) dfs(v, vis, edges, ans); 12 | } 13 | 14 | ans.push_back(u); 15 | } 16 | 17 | int main() { 18 | int n, m; 19 | while(scanf("%d%d", &n, &m), n || m) { 20 | vector > edges(n+10); 21 | while(m--) { 22 | int u, v; scanf("%d%d", &u, &v); 23 | edges[u].push_back(v); 24 | } 25 | 26 | vector ans; 27 | vector vis(n+10, false); 28 | 29 | for (int u = 1; u <= n; u++) { 30 | if (!vis[u]) dfs(u, vis, edges, ans); 31 | } 32 | 33 | reverse(ans.begin(), ans.end()); 34 | 35 | for (int i = 0; i < (int)ans.size(); i++) { 36 | printf("%d", ans[i]); 37 | if (i != (int)ans.size() - 1) 38 | printf(" "); 39 | 40 | } 41 | 42 | printf("\n"); 43 | } 44 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/12/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int memo[21][201][201]; 8 | 9 | int solve(int i, int cur, int S, vector > prices) { 10 | if (cur > S) return INT_MIN; 11 | if (i == (int)prices.size()) return 0; 12 | 13 | if (~memo[i][cur][S]) return memo[i][cur][S]; 14 | 15 | int& ret = memo[i][cur][S]; 16 | ret = INT_MIN; 17 | 18 | for (int k : prices[i]) { 19 | ret = max(ret, k + solve(i+1, cur+k, S, prices)); 20 | } 21 | 22 | return ret; 23 | } 24 | 25 | int main() { 26 | int t; scanf("%d", &t); 27 | while(t--) { 28 | memset(memo, -1, sizeof memo); 29 | 30 | int m, c; scanf("%d%d", &m, &c); 31 | vector > prices(c); 32 | for (int i = 0; i < c; i++) { 33 | int k; scanf("%d", &k); 34 | prices[i].resize(k); 35 | for (int& j : prices[i]) scanf("%d", &j); 36 | } 37 | 38 | int ans = solve(0, 0, m, prices); 39 | if (ans < 0) printf("no solution\n"); 40 | else printf("%d\n", ans); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/12/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | 10 | int main() { 11 | int x, cs = 0; 12 | vector vec; 13 | 14 | SCAN: 15 | while(scanf("%d", &x), x != -1) { 16 | if (x == -1) break; 17 | 18 | vec.push_back(x); 19 | } 20 | 21 | if (vec.size() == 0) return 0; 22 | 23 | vector memo(32768, 0); 24 | 25 | for (int v : vec) { 26 | for (int k = v; k < (int)memo.size(); k++) 27 | memo[v] = max(memo[v], memo[k] + 1); 28 | } 29 | 30 | if (cs) printf("\n"); 31 | printf("Test #%d:\n maximum possible interceptions: %d\n", ++cs, *max_element(memo.begin(), memo.end())); 32 | 33 | vec.clear(); 34 | goto SCAN; 35 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/13/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int INF = 0x3f3f3f3f; 6 | 7 | typedef long long ll; 8 | 9 | int a[51]; 10 | ll memo[51][251]; 11 | 12 | ll solve(int i, int cur, int n, int m) { 13 | if (cur > n) return 0; 14 | if (i > m) return cur == n; 15 | 16 | ll& ret = memo[i][cur]; 17 | if (~ret) return ret; 18 | 19 | return ret = solve(i, cur+a[i], n, m) + solve(i+1, cur, n, m); 20 | } 21 | 22 | int main() { 23 | int n, m; scanf("%d%d", &n, &m); 24 | for (int i = 1; i <= m; i++) scanf("%d", a+i); 25 | memset(memo, -1, sizeof memo); 26 | printf("%lld\n", solve(1, 0, n, m)); 27 | } -------------------------------------------------------------------------------- /problems/solutions/Lab de Algoritmos Avancados 2019/13/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | const int INF = 0x3f3f3f3f; 9 | 10 | int memo[1000][300]; 11 | 12 | int solve(int i, int cur, int n, int S, vector& p, vector& w) { 13 | if (cur > S) return -INF; 14 | if (i >= n) return 0; 15 | 16 | int& ret = memo[i][cur]; 17 | if (~ret) return ret; 18 | 19 | int tk = p[i] + solve(i+1, cur+w[i], n, S, p, w); 20 | int nt = solve(i+1, cur, n, S, p, w); 21 | 22 | return ret = max(tk, nt); 23 | } 24 | 25 | int main() { 26 | int t; scanf("%d", &t); 27 | while(t--) { 28 | int n; scanf("%d", &n); 29 | vector p(n), w(n); 30 | for (int i = 0; i < n; i++) scanf("%d%d", &p[i], &w[i]); 31 | int g; scanf("%d", &g); 32 | vector m(g); 33 | for (int& i : m) scanf("%d", &i); 34 | 35 | ll ans = 0; 36 | for (int c : m) { 37 | memset(memo, -1, sizeof memo); 38 | ans += solve(0, 0, n, c, p, w); 39 | } 40 | 41 | printf("%lld\n", ans); 42 | } 43 | } -------------------------------------------------------------------------------- /problems/solutions/MAPS/MAPS20/B.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/MAPS/MAPS20/B.cpp -------------------------------------------------------------------------------- /problems/solutions/MAPS/MAPS20/J.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = [0] * n 3 | 4 | for i in range(n): 5 | a[i] = int(input()) 6 | 7 | xx = 0 8 | pt = 1 9 | for i in range(n): 10 | xx += a[i] * pt 11 | pt *= 4/5 12 | xx = 1/5 * xx 13 | 14 | ans = 0 15 | for i in range(n): 16 | ret = 0 17 | pt = 1 18 | for j in range(n): 19 | if i != j: 20 | ret += a[j] * pt 21 | pt *= 4/5 22 | ans += ret * 1/5 23 | 24 | print(xx) 25 | print(ans/n) -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 1/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #ifdef LOCAL_PC 5 | #define debug(args...) {cout.flush(); fflush(stdout); fprintf(stderr, args);} 6 | #else 7 | #define debug(args...) ; /* */ 8 | #endif 9 | #define pb push_back 10 | #define eb emplace_back 11 | #define mk make_pair 12 | #define fi first 13 | #define se second 14 | typedef long long ll; 15 | typedef pair pii; 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | template inline void rd(num &x) { 19 | char c; while(isspace(c = getchar())); 20 | bool neg = false; 21 | if(!isdigit(c)) neg = (c == '-'), x = 0; 22 | else x = c - '0'; 23 | while(isdigit(c = getchar())) 24 | x = (x << 3) + (x << 1) + c - '0'; 25 | if (neg) x = -x; 26 | } 27 | template inline void rd(T& x, Args&... args) { rd(x); rd(args...); } 28 | 29 | const int MAXN = 2e5 + 5, INF = 0x3f3f3f3f; 30 | 31 | int main() { 32 | // ios::sync_with_stdio(false); cin.tie(NULL); 33 | int x; rd(x); 34 | cout << (x - 9 + 24) % 24 << endl; 35 | } -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/A: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 2/A -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/B: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 2/B -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 2/C -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/D: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 2/D -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/D2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 2/D2 -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/E: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 2/E -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/E.in1: -------------------------------------------------------------------------------- 1 | 6 7 2 | 0 7 2 5 1 3 3 | a 4 6 4 | o 2 5 5 | x 1 4 6 | u 4 6 7 | a 4 6 8 | o 2 5 9 | x 1 4 -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/E.in2: -------------------------------------------------------------------------------- 1 | 15 10 2 | 3 55 65 15 54 32 40 46 47 36 34 57 83 28 87 3 | a 6 12 4 | u 9 57 5 | x 7 12 6 | u 9 10 7 | x 8 10 8 | u 15 28 9 | o 14 15 10 | u 3 52 11 | x 1 3 12 | o 7 10 -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/E.in3: -------------------------------------------------------------------------------- 1 | 7 2 2 | 10 11 18 27 23 3 35 3 | o 1 4 4 | a 4 7 -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/F: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 2/F -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/gen.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | N = 100 4 | n = 350 5 | m = 345 6 | print(n, m, 5) 7 | for i in range(n): 8 | for j in range(m): 9 | print(random.randint(-N, N), end=' ') 10 | print() 11 | -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/in2: -------------------------------------------------------------------------------- 1 | 3 4 1 2 | 1 2 -4 -8 3 | N O K A 4 | 0 1 -1 1 -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/o: -------------------------------------------------------------------------------- 1 | --Rank do Nepscord-- 2 | #1. pdstiago - Nivel 2 3 | #2. sisterolli - Nivel 2 4 | #3. drangel - Nivel 1 5 | -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 2/ou: -------------------------------------------------------------------------------- 1 | --Rank do Nepscord-- 2 | #1. pdstiago - Nivel 2 3 | #2. sisterolli - Nivel 2 4 | #3. drangel - Nivel 1 -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 4/A: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 4/A -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 4/B: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 4/B -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 4/C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 4/C -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 4/F: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Neps/Liga de Programação 2020/Etapa 4/F -------------------------------------------------------------------------------- /problems/solutions/Neps/Liga de Programação 2020/Etapa 4/in: -------------------------------------------------------------------------------- 1 | 10 11 2 | 2 1 3 | 3 4 | 1 1 5 3 5 | 2 3 6 | 2 1 7 | 3 8 | 1 2 7 4 9 | 2 1 10 | 2 3 11 | 2 4 12 | 3 -------------------------------------------------------------------------------- /problems/solutions/Stanford ProCo 2015/N1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef pair pii; 11 | typedef long long ll; 12 | 13 | const int MAXN = 1e5 + 5; 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); cin.tie(nullptr); 17 | 18 | cout << "Hello. I am Baymax, your personal healthcare companion."; 19 | 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /problems/solutions/Stanford ProCo 2015/N2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef pair pii; 11 | typedef long long ll; 12 | 13 | const int MAXN = 1e5 + 5; 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); cin.tie(nullptr); 17 | 18 | int n; cin >> n; 19 | 20 | for (int i = 0; i < n; i++) { 21 | for (int j = i + 1; j < n; j++) cout << " "; 22 | cout << '/'; 23 | for (int j = 0; j < 2 * i; j++) cout << " "; 24 | cout << "\\\n"; 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /problems/solutions/Stanford ProCo 2015/N3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | using lint = long long; 5 | 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | 10 | int n; 11 | cin >> n; 12 | 13 | int curr = 0; 14 | map hist = {{0, 0}}; 15 | for (int i = 1; i <= n; i++) { 16 | char c; 17 | int k; 18 | cin >> c; 19 | 20 | if (c == '+') { 21 | curr++; 22 | } else if (c == '-') { 23 | curr--; 24 | } else { 25 | cin >> k; 26 | curr = hist[i - k - 1]; 27 | } 28 | hist[i] = curr; 29 | } 30 | cout << curr << endl; 31 | } -------------------------------------------------------------------------------- /problems/solutions/Stanford ProCo 2015/N4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef pair pii; 11 | typedef long long ll; 12 | 13 | const int MAXN = 200; 14 | 15 | vector edges[MAXN]; 16 | int a[MAXN]; 17 | 18 | double ans = 0; 19 | 20 | pii dfs(int u) { 21 | 22 | pii ret = mp(1, a[u]); 23 | 24 | for (int v : edges[u]) { 25 | pii cur = dfs(v); 26 | ret.fi += cur.fi; 27 | ret.se += cur.se; 28 | } 29 | 30 | ans = max(ans, ret.se / (double)ret.fi); 31 | return ret; 32 | } 33 | 34 | int main() { 35 | ios::sync_with_stdio(false); cin.tie(nullptr); 36 | 37 | int n; cin >> n; 38 | 39 | for (int i = 1; i <= n; i++) { 40 | int x, y; cin >> x >> y; 41 | if (x) edges[x].pb(i); 42 | a[i] = y; 43 | } 44 | 45 | dfs(1); 46 | 47 | cout << fixed << setprecision(3) << ans << endl; 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /problems/solutions/Stanford ProCo 2015/N5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef pair pii; 11 | typedef long long ll; 12 | 13 | const int MAXN = 1005; 14 | 15 | int memo[4][11][11][11]; 16 | 17 | ll solve(int cur, int a, int b, int c) { 18 | if (a < 0 || b < 0 || c < 0) return 0; 19 | 20 | int& ret = memo[cur][a][b][c]; 21 | if (~ret) return ret; 22 | 23 | if (!a && !b && !c) return ret = 1; 24 | 25 | if (cur == 1) { 26 | return ret = solve(2, a, b - 1, c) + solve(3, a, b, c - 1); 27 | } else if (cur == 2) { 28 | return ret = solve(1, a - 1, b, c) + solve(3, a, b, c - 1); 29 | } else { 30 | return ret = solve(1, a - 1, b, c) + solve(2, a, b - 1, c); 31 | } 32 | } 33 | 34 | int main() { 35 | ios::sync_with_stdio(false); cin.tie(nullptr); 36 | 37 | int n; cin >> n; 38 | memset(memo, -1, sizeof memo); 39 | 40 | cout << (3 * solve(1, n, n, n)) / 2 << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /problems/solutions/Training Camp Argentina/6/B.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | for line in sys.stdin: 4 | if line == '=\n': 5 | break 6 | 7 | expr = "int('" 8 | m = 2 9 | 10 | ok = True 11 | for c in line: 12 | if c >= '0' and c <= '9': 13 | if not ok: 14 | expr += "int('" 15 | ok = True 16 | expr += c 17 | m = max(m, int(c) + 1) 18 | else: 19 | expr += "', b)" 20 | ok = False 21 | expr += c 22 | if c == '=': 23 | expr += '=' 24 | 25 | ans = [0] * 37 26 | for b in range(m, 37): 27 | ans[b] = eval(expr) 28 | if (sum(ans) == 0): 29 | print('*', flush=False) 30 | else: 31 | if (ans[-1] == 1): 32 | for i in range(m, 37): 33 | if ans[i]: 34 | print(f'{i}+', flush=False) 35 | break 36 | else: 37 | for i in range(m, 37): 38 | if ans[i]: 39 | print(i, end=' ', flush=False) 40 | print('', flush=False) -------------------------------------------------------------------------------- /problems/solutions/UFSCAR/Desafio de Programadores 2019/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 50; 5 | 6 | int memo[N][N][N][N]; 7 | 8 | bool solve(int m, int n, int x, int y) { 9 | if (m == 1 && n == 1) return true; 10 | 11 | int& ret = memo[m][n][x][y]; 12 | if (~ret) return ret; 13 | 14 | for (int i = 1; i < m; i++) { 15 | if (x < i && solve(i, n, x, y)) return ret = false; 16 | if (x >= i && solve(m-i, n, x-i, y)) return ret = false; 17 | } 18 | 19 | for (int i = 1; i < n; i++) { 20 | if (y < i && solve(m, i, x, y)) return ret = false; 21 | if (y >= i && solve(m, n-i, x, y-i)) return ret = false; 22 | } 23 | 24 | return ret = true; 25 | } 26 | 27 | int main() { 28 | memset(memo, -1, sizeof memo); 29 | int t; scanf("%d", &t); 30 | while(t--) { 31 | int n, m, x, y; scanf("%d%d%d%d", &n, &m, &x, &y); 32 | printf("%s\n", solve(n, m, x, y) == 0 ? "Vinicius" : "Bianca"); 33 | } 34 | } -------------------------------------------------------------------------------- /problems/solutions/UFSCAR/Desafio de Programadores 2019/E.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | map books; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); cin.tie(NULL); 7 | 8 | int n; 9 | while(cin >> n) { 10 | int tmp, all; 11 | books.clear(); 12 | 13 | vector> input; 14 | 15 | for(int i = 1; i <= n; i++){ 16 | cin >> tmp; 17 | input.push_back({tmp, i}); 18 | } 19 | cin >> all; 20 | 21 | for(auto p : input) { 22 | books[all - p.first] = p.second; 23 | } 24 | 25 | 26 | int best = 0; 27 | for(auto p : input) { 28 | if(books[p.first] != 0 && books[p.first] != p.second) { 29 | if( abs((all / 2) - p.first) < abs((all / 2) - best)) { 30 | best = p.first; 31 | } 32 | } 33 | } 34 | 35 | if(best < all - best) { 36 | cout << best << " " << (all - best) << '\n'; 37 | } else { 38 | cout << (all - best) << " " << best << '\n'; 39 | } 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /problems/solutions/UFSCAR/Desafio de Programadores 2019/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; scanf("%d", &t); 6 | for (int cs = 1; cs <= t; cs++) { 7 | int a, b; scanf("%d%d", &a, &b); 8 | 9 | long long ans = 0; 10 | for (int i = a + (a%2 == 0); i <= b; i += 2) { 11 | ans += i; 12 | } 13 | 14 | printf("Caso %d: %lld\n", cs, ans); 15 | } 16 | } -------------------------------------------------------------------------------- /problems/solutions/UFSCAR/Desafio de Programadores 2019/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector a; 5 | 6 | bool check(long long k, int m) { 7 | long long cur = 0, qtt = 0; 8 | 9 | for (int& i : a) { 10 | if (i > k) return false; 11 | if (cur + i <= k) { 12 | cur += i; 13 | } else { 14 | qtt++; 15 | cur = i; 16 | } 17 | } 18 | 19 | return qtt <= m; 20 | } 21 | 22 | int main() { 23 | int n, k; 24 | while(~scanf("%d%d", &n, &k)) { 25 | a = vector(n+1); 26 | for (int& i : a) cin >> i; 27 | 28 | long long lo = 0, hi = 1e14, mi; 29 | while(lo < hi) { 30 | mi = (lo + hi) / 2; 31 | 32 | if (check(mi, k)) hi = mi; 33 | else lo = mi + 1; 34 | } 35 | 36 | printf("%lld\n", hi); 37 | } 38 | } -------------------------------------------------------------------------------- /problems/solutions/UFSCAR/Desafio de Programadores 2019/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | map books; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); cin.tie(NULL); 7 | 8 | string s; 9 | while(cin >> s) { 10 | stack st; 11 | 12 | bool ok = true; 13 | for (char c : s) { 14 | if (c == '(') st.push(c); 15 | if (c == ')') { 16 | if (st.size() && st.top() == '(') st.pop(); 17 | else ok = false; 18 | } 19 | if (c == '{') st.push(c); 20 | if (c == '}') { 21 | if (st.size() && st.top() == '{') st.pop(); 22 | else ok = false; 23 | } 24 | if (c == '[') st.push(c); 25 | if (c == ']') { 26 | if (st.size() && st.top() == '[') st.pop(); 27 | else ok = false; 28 | } 29 | } 30 | 31 | ok &= st.empty(); 32 | 33 | if (ok) { 34 | cout << "YES" << '\n'; 35 | } else { 36 | cout << "NO" << '\n'; 37 | } 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /problems/solutions/UFSCAR/Desafio de Programadores 2019/L.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 1e5 + 5; 5 | typedef long long ll; 6 | long long bt[MAXN]; 7 | 8 | void clear() { 9 | memset(bt, 0, sizeof bt); 10 | } 11 | 12 | ll query(int r) { 13 | ll sum = 0; 14 | while(r) { 15 | sum += bt[r]; 16 | r -= (r&-r); 17 | } 18 | return sum; 19 | } 20 | 21 | ll query(int l, int r) { 22 | if (l == 0) return query(r); 23 | if (l > r) return 0; 24 | return query(r) - query(l-1); 25 | } 26 | 27 | void update(int p, int x) { 28 | while(p < MAXN) { 29 | bt[p] += x; 30 | p += (p&-p); 31 | } 32 | } 33 | 34 | int main() { 35 | int n; 36 | while(~scanf("%d", &n), n) { 37 | clear(); 38 | 39 | ll ans = 0; 40 | for (int i = 1; i <= n; i++) { 41 | int x; scanf("%d", &x); 42 | ans += query(x+1, MAXN-1); 43 | update(x, 1); 44 | } 45 | printf("%s\n", ((ans%2 == 1) ? "Martinho" : "Rene")); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /problems/solutions/USACO/Gold 2015/in: -------------------------------------------------------------------------------- 1 | 5 2 | 8 1 3 | 1 4 4 | 8 8 5 | 7 15 6 | 4 20 7 | -------------------------------------------------------------------------------- /problems/solutions/USACO/Gold 2015/in2: -------------------------------------------------------------------------------- 1 | 31 2 | 17 248 3 | 20 115 4 | 17 273 5 | 17 312 6 | 25 384 7 | 18 53 8 | 17 296 9 | 201 3 10 | 21 379 11 | 17 208 12 | 20 374 13 | 40 421 14 | 17 158 15 | 19 357 16 | 17 167 17 | 18 103 18 | 17 330 19 | 18 128 20 | 17 268 21 | 17 288 22 | 17 172 23 | 17 285 24 | 18 340 25 | 37 408 26 | 30 33 27 | 17 182 28 | 18 36 29 | 17 235 30 | 18 18 31 | 17 231 32 | 22 35 33 | -------------------------------------------------------------------------------- /problems/solutions/USACO/Gold 2015/out2: -------------------------------------------------------------------------------- 1 | 251 2 | -------------------------------------------------------------------------------- /problems/solutions/USACO/Gold 2015/trapped: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/USACO/Gold 2015/trapped -------------------------------------------------------------------------------- /problems/solutions/USACO/Gold 2020/C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/USACO/Gold 2020/C -------------------------------------------------------------------------------- /problems/solutions/USACO/Gold 2020/C2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/USACO/Gold 2020/C2 -------------------------------------------------------------------------------- /problems/solutions/USACO/Gold 2020/in: -------------------------------------------------------------------------------- 1 | 5 1000000007 -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2016/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int n, k; scanf("%d%d", &n, &k); n++; 4 | return !printf("%s\n", (n%k)? "no" : "yes"); 5 | } -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2017/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define INF 0x3f3f3f3f 5 | #define pb push_back 6 | 7 | int n, X; 8 | 9 | int main() { 10 | int x, menor = INF, maior = 0; scanf("%d%d", &n, &X); 11 | for (int i = 0; i < n; i++) { 12 | scanf("%d", &x); 13 | menor = min(menor, x); 14 | maior = max(maior, x); 15 | if (x == X) return !printf("1\n"); 16 | } 17 | if (menor > X || maior < X) return !printf("-1\n"); 18 | 19 | return !printf("2\n"); 20 | } -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2017/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define INF 0x3f3f3f3f 5 | #define pb push_back 6 | #define PI acos(-1.0) 7 | 8 | int main() { 9 | int a, b; scanf("%d%d", &a, &b); 10 | return !printf("%d\n", max(a, b)); 11 | } -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2018/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define fi first 7 | #define se second 8 | #define mk make_pair 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | int main() { 14 | ll n; scanf("%lld", &n); 15 | ll x1, y1, x2, y2; scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2); 16 | 17 | ll d = abs(x1-x2) + abs(y1-y2); 18 | d = d == 0 ? 2 : d; 19 | 20 | ll ans = (n - d + 2) / 2; 21 | ans = max(ans, 0ll); 22 | 23 | printf("%lld\n", ans); 24 | } -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2018/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | vector edges[MAXN]; 18 | 19 | int curTime, t_in[MAXN], t_out[MAXN]; 20 | 21 | void dfs(int u) { 22 | t_in[u] = ++curTime; 23 | for (int v : edges[u]) dfs(v); 24 | t_out[u] = ++curTime; 25 | } 26 | 27 | int main() { 28 | int n, q; scanf("%d%d", &n, &q); 29 | 30 | vector roots; 31 | for (int u = 0; u < n; u++) { 32 | int v; scanf("%d", &v); 33 | if (v == -1) roots.pb(u); 34 | else edges[v].pb(u); 35 | } 36 | 37 | for (int u : roots) dfs(u); 38 | 39 | while(q--) { 40 | int u, v; scanf("%d%d", &u, &v); 41 | if (t_in[v] <= t_in[u] && t_out[v] >= t_out[u]) 42 | printf("Yes\n"); 43 | else 44 | printf("No\n"); 45 | } 46 | } -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2018/L.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define debugg(a) cout << ":" << (#a) << " : " << (a) << endl; 5 | #define mset(x, y) memset(x, y, sizeof x) 6 | #define pb push_back 7 | #define mp make_pair 8 | #define PI acos(-1.0) 9 | #define INF 0x3f3f3f3f 10 | #define fastcin ios::sync_with_stdio(false); 11 | #define endl "\n" 12 | 13 | #define MAXN 500020 14 | 15 | 16 | vector edges[MAXN]; 17 | int vis[MAXN]; 18 | 19 | int dfs(int u) { 20 | int ans = 0; 21 | vis[u] = 1; 22 | for(int i = 0; i < edges[u].size(); ++i) { 23 | if(!vis[edges[u][i]]) 24 | ans = max(ans, dfs(edges[u][i])); 25 | } 26 | return 1+ans; 27 | } 28 | 29 | typedef long long ll; 30 | typedef pair pii; 31 | 32 | int main () { 33 | int n, A, u, v; scanf("%d%d", &n, &A); 34 | for (int i = 0; i < n-1; i++) { 35 | scanf("%d%d", &u, &v); 36 | edges[u].pb(v); 37 | edges[v].pb(u); 38 | } 39 | printf("%d\n", dfs(A)); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2019/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int MAXN = 1e5 + 5; 14 | const int INF = 0x3f3f3f3f; 15 | 16 | int main() { 17 | ios::sync_with_stdio(false); cin.tie(NULL); 18 | 19 | ll a, b; cin >> a >> b; 20 | 21 | set ans; 22 | 23 | for (ll i = 1; i * i <= a; i++) { 24 | if (a%i == 0) { 25 | if (i % b == 0) ans.insert(i); 26 | if (a/i % b == 0) ans.insert(a/i); 27 | } 28 | } 29 | 30 | for (ll i : ans) cout << i << ' '; 31 | cout << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2019/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int MAXN = 1e5 + 5; 14 | const int INF = 0x3f3f3f3f; 15 | 16 | void imp() { 17 | cout << "NO\n"; 18 | exit(0); 19 | } 20 | void pos() { 21 | cout << "YES\n"; 22 | exit(0); 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(false); cin.tie(NULL); 27 | 28 | string s, t; cin >> s >> t; 29 | 30 | if (s.size() < t.size()) imp(); 31 | 32 | int i, j; 33 | for (i = 0, j = 0; i < s.size(); i++) { 34 | j += s[i] == t[j]; 35 | if (j == t.size()) pos(); 36 | } 37 | 38 | imp(); 39 | 40 | 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /problems/solutions/USP-ICMC/USP-ICMC 2019/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int MAXN = 1e6 + 5; 14 | const int INF = 0x3f3f3f3f; 15 | 16 | map memo; 17 | 18 | // int maxi = 0; 19 | // double solve(int n, int c) { 20 | // maxi = max(maxi, c); 21 | 22 | // if (n <= 1) return 1; 23 | // pii cur = mp(n, c); 24 | 25 | // if (memo.count(cur)) return memo[cur]; 26 | // return memo[cur] = (1.0 + c) / ((double) c + n) + solve(n-1, c+1); 27 | // } 28 | 29 | int main() { 30 | ios::sync_with_stdio(false); cin.tie(NULL); 31 | int n; cin >> n; 32 | 33 | double ans = n; 34 | ans = (n + 1) / 2.0; 35 | 36 | cout << fixed << setprecision(7) << ans << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /problems/solutions/USP-IME/2017 USP Try-outs/F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 2e5 + 5; 5 | 6 | int dist(int x, int y) { 7 | return x*x + y*y; 8 | } 9 | 10 | int main() { 11 | int r; scanf("%d", &r); 12 | 13 | int x, y; 14 | 15 | int ans = 0; 16 | for (int i = 0; i < 3; i++) { 17 | scanf("%d%d", &x, &y); 18 | int d = dist(x, y); 19 | 20 | for (int k = 1; k <= 10; k++) { 21 | if (d <= r*r*k*k) { 22 | ans += 10 - k + 1; 23 | break; 24 | } 25 | } 26 | } 27 | 28 | 29 | printf("%d\n", ans); 30 | } -------------------------------------------------------------------------------- /problems/solutions/USP-IME/2019 USP Try-outs/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 5e4 + 5, N = 305; 5 | 6 | vector p[N][N], vec[MAXN]; 7 | int a[MAXN]; 8 | 9 | int main() { 10 | int n, q; scanf("%d%d", &n, &q); 11 | for (int i = 1; i <= n; i++) scanf("%d", a+i); 12 | 13 | for (int i = 1; i <= n; i++) { 14 | vec[a[i]].push_back(i); 15 | for (int j = 1; j < N; j++) { 16 | p[j][a[i]%j].push_back(i); 17 | } 18 | } 19 | 20 | while(q--) { 21 | int l, r, x, m; // a[i] % m = x 22 | scanf("%d%d%d%d", &l, &r, &x, &m); 23 | 24 | int ans = 0; 25 | if (m <= 300) { 26 | ans = upper_bound(p[m][x].begin(), p[m][x].end(), r) - lower_bound(p[m][x].begin(), p[m][x].end(), l); 27 | } else { 28 | for (int i = x; i < MAXN; i += m) { 29 | ans += upper_bound(vec[i].begin(), vec[i].end(), r) - lower_bound(vec[i].begin(), vec[i].end(), l); 30 | } 31 | } 32 | 33 | printf("%d\n", ans); 34 | } 35 | 36 | 37 | } -------------------------------------------------------------------------------- /problems/solutions/USP-IME/2019 USP Try-outs/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mp make_pair 7 | #define fi first 8 | #define se second 9 | #define DEBUG 0 10 | #define INF 100000000000000000LL 11 | #define ll long long 12 | typedef pair pii; 13 | 14 | 15 | int main() 16 | { 17 | ios::sync_with_stdio(false); cin.tie(nullptr); 18 | int n; 19 | cin >> n; 20 | vector > v; 21 | for(int i=0;i> p >> f; 25 | v.pb(mp(f,p)); 26 | } 27 | sort(v.begin(),v.end()); 28 | ll int sum=0, maior = -INF; 29 | for(int i=0;i 2 | using namespace std; 3 | 4 | const int MAXN = 1e6 + 1; 5 | 6 | int cnt[MAXN]; 7 | 8 | void pre() { 9 | for (int i = 2; i < MAXN; i++) { 10 | if (cnt[i]) continue; 11 | for (int j = i; j < MAXN; j += i) { 12 | cnt[j]++; 13 | } 14 | } 15 | } 16 | 17 | int main() { 18 | pre(); 19 | 20 | int n; 21 | while(scanf("%d", &n), n) { 22 | printf("%d : %d\n", n, cnt[n]); 23 | } 24 | } -------------------------------------------------------------------------------- /problems/solutions/UVA/374.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define mp make_pair 6 | typedef long long ll; 7 | typedef pair pii; 8 | 9 | const int MAXN = 100005; 10 | const int INF = 0x3f3f3f3f; 11 | 12 | inline ll fast_exp(ll x, ll y, ll MOD) { 13 | x%=MOD; 14 | 15 | ll ans = 1; 16 | while (y > 0) { 17 | if (y & 1LL) ans = (ans * x) % MOD; 18 | y >>= 1LL; 19 | x = (x*x) % MOD; 20 | } 21 | 22 | return ans % MOD; 23 | } 24 | 25 | int main() { 26 | // ios::sync_with_stdio(false); cin.tie(NULL); 27 | 28 | ll b, p, m; 29 | while(~scanf("%lld%lld%lld", &b, &p, &m)) { 30 | printf("%lld\n", fast_exp(b, p, m)); 31 | } 32 | 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /problems/solutions/UVA/455.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector build_z(string& s) { 5 | int n = s.size(); 6 | vector z(n, 0); 7 | 8 | for (int i = 1, l = 0, r = 0; i < n; i++) { 9 | if (i <= r) 10 | z[i] = min(r - i + 1, z[i - l]); 11 | 12 | while(i + z[i] < n && s[z[i]+i] == s[z[i]]) 13 | z[i]++; 14 | 15 | if (z[i] + i - 1 > r) 16 | l = i, r = i + z[i] - 1; 17 | } 18 | 19 | return z; 20 | } 21 | 22 | int main() { 23 | ios::sync_with_stdio(false); cin.tie(NULL); 24 | int t; cin >> t; 25 | while(t--) { 26 | string s; cin >> s; 27 | int n = s.size(); 28 | 29 | vector z = build_z(s); 30 | 31 | int ans = n; 32 | for (int i = 1; i < ans; i++) { 33 | if (n%i == 0 && z[i] + i == n) ans = i; 34 | } 35 | 36 | cout << ans; 37 | if (t) cout << "\n\n"; 38 | else cout << "\n"; 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /problems/solutions/UVA/in: -------------------------------------------------------------------------------- 1 | 5 2 | sohel 10 10 3 | mahmud 20 10 4 | sanny 5 5 5 | prince 1 1 6 | per 120 3 7 | mf 6 6 8 | kugel 50 60 9 | joey 3 24 10 | limon 6 9 11 | manzoor 0 0 12 | 1 13 | derek 9 9 14 | jimmy 10 10 15 | 0 -------------------------------------------------------------------------------- /problems/solutions/Uri/Contest de Natal 2019/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | bool isPrime(ll x) { 19 | if (x == 1) return false; 20 | for (ll i = 2; i*i <= x; i++) 21 | if (x%i == 0) return false; 22 | return true; 23 | } 24 | 25 | int main() { 26 | int n; scanf("%d", &n); 27 | while(n--) { 28 | ll x; scanf("%lld", &x); 29 | x++; 30 | if (x%2 == 1 && x%7 == 0 && isPrime(x+2)) 31 | printf("Yes\n"); 32 | else printf("No\n"); 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Contest de Natal 2019/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e5 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | const long double lambda = 700; 19 | const long double c = 3e8; 20 | 21 | int main() { 22 | ios::sync_with_stdio(false); cin.tie(NULL); 23 | 24 | long double v; cin >> v; 25 | long double l1 = (sqrt((c-v)/(c+v)) - 1) * lambda + lambda; 26 | l1 -= 1e-12; 27 | 28 | if (l1 < 400) cout << "invisivel\n"; 29 | else if (l1 < 425) cout << "violeta\n"; 30 | else if (l1 < 445) cout << "anil\n"; 31 | else if (l1 < 500) cout << "azul\n"; 32 | else if (l1 < 575) cout << "verde\n"; 33 | else if (l1 < 585) cout << "amarelo\n"; 34 | else if (l1 < 620) cout << "laranja\n"; 35 | else if (l1 < 750) cout << "vermelho\n"; 36 | else cout << "invisivel\n"; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Contest de Natal 2019/G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e4 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | int T; scanf("%d", &T); 20 | while(T--) { 21 | ll p1 = 0, p2 = 0; 22 | for (int i = 0; i < 3; i++) { 23 | ll x, d; scanf("%lld%lld", &x, &d); 24 | p1 += x*d; 25 | } 26 | for (int i = 0; i < 3; i++) { 27 | ll x, d; scanf("%lld%lld", &x, &d); 28 | p2 += x*d; 29 | } 30 | 31 | if (p1 > p2) printf("JOAO\n"); 32 | else printf("MARIA\n"); 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Contest de Natal 2019/H.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e4 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | map mp; 20 | for (int i = 0; i < 128; i++) mp[i] = i; 21 | mp['@'] = 'a', mp['&'] = 'e', 22 | mp['!'] = 'i', mp['*'] = 'o', mp['#'] = 'u'; 23 | 24 | char c; 25 | while(~scanf("%c", &c)) printf("%c", mp[c]); 26 | printf("\n"); 27 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Contest de Natal 2019/I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e4 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | map mp; 20 | int n; scanf("%d", &n); 21 | while(n--) { 22 | char c; scanf(" %*s %c", &c); 23 | mp[c]++; 24 | } 25 | 26 | printf("%d carrinhos\n%d bonecas\n", mp['M'], mp['F']); 27 | 28 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Contest de Natal 2019/J.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define mk make_pair 7 | #define fi first 8 | #define se second 9 | 10 | typedef long long ll; 11 | typedef pair pii; 12 | 13 | const int INF = 0x3f3f3f3f; 14 | const int MAXN = 1e4 + 5; 15 | 16 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 17 | 18 | int main() { 19 | int n; scanf("%d", &n); 20 | while(n--) { 21 | int h, d, g; scanf("%d%d%d", &h, &d, &g); 22 | bool ok = h >= 200 && h <= 300 && d >= 50 && g >= 150; 23 | printf("%s\n", ok ? "Sim" : "Nao"); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Maratona do Norte/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long a[] = {300, 1500, 600, 1000, 150}; 6 | long long ans = 225; 7 | 8 | for (int i = 0; i < 5; i++) { 9 | int x; scanf("%d", &x); 10 | ans += x * a[i]; 11 | } 12 | 13 | printf("%lld\n", ans); 14 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Maratona do Norte/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int p, c, n; scanf("%d%d%d", &p, &c, &n); 6 | 7 | int ans = 0; 8 | 9 | for (int i = 1; i <= n; i++) { 10 | int x; scanf("%d", &x); 11 | if (x - p < 0) break; 12 | 13 | x -= p; 14 | if (c > x) { 15 | ans += i != 1; 16 | c = x; 17 | } 18 | } 19 | 20 | printf("%d\n", ans); 21 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Maratona do Norte/K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check(string& s, int n) { 5 | int r = 0; 6 | 7 | for (char c : s) { 8 | if (c == '1') r = (r << 1 | 1) % n; 9 | else r = (r << 1) % n; 10 | } 11 | 12 | return r%n == 0; 13 | } 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); cin.tie(NULL); 17 | 18 | vector ans; 19 | 20 | string s; cin >> s; 21 | int m; cin >> m; 22 | while(m--) { 23 | int x; cin >> x; 24 | if (check(s, x)) ans.push_back(x); 25 | } 26 | 27 | if (ans.empty()) cout << "Nenhum\n"; 28 | else { 29 | sort(ans.begin(), ans.end()); 30 | for (int k : ans) cout << k << ' '; 31 | cout << endl; 32 | } 33 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/Maratona do Norte/L.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); cin.tie(NULL); 6 | 7 | int k; cin >> k; 8 | string s; cin >> s; 9 | 10 | vector > freq(k); 11 | 12 | int n; cin >> n; 13 | while(n--) { 14 | string t; cin >> t; 15 | for (int i = 0; i < k; i++) { 16 | freq[i][t[i]]++; 17 | } 18 | } 19 | 20 | int ans = 0; 21 | for (int i = 0; i < k; i++) { 22 | int best = 0; 23 | for (int j = 'A'; j <= 'Z'; j++) { 24 | if (j == s[i]) continue; 25 | best = max(best, freq[i][j]); 26 | } 27 | ans += best; 28 | } 29 | 30 | cout << ans << endl; 31 | } -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/A: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/A -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/B: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/B -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/B2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/B2 -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/C -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/D: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/D -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/G: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/G -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/H: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/H -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/J: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/J -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/J2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/J2 -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/K: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/K -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/L: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrefakhoury/competitive-programming/75e87e95a4e528a3626fecf53a8689f53a1931a0/problems/solutions/Uri/SAP 2020/L -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/in: -------------------------------------------------------------------------------- 1 | 4 2 | 31 30 3 | 63 10 4 | 127 130 5 | 15 70 -------------------------------------------------------------------------------- /problems/solutions/Uri/SAP 2020/in2: -------------------------------------------------------------------------------- 1 | 3 2 | 01 s 3 | 11 a 4 | 00 t 5 | sat -------------------------------------------------------------------------------- /problems/solutions/VTEX/VTEX Code Cup 2019/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fi first 5 | #define se second 6 | #define pb push_back 7 | #define mp make_pair 8 | 9 | typedef pair pii; 10 | typedef long long ll; 11 | 12 | const int MAXN = 1e5 + 5; 13 | 14 | int tin[MAXN], tout[MAXN]; 15 | int times[MAXN]; 16 | 17 | int main() { 18 | ios::sync_with_stdio(false); cin.tie(NULL); 19 | 20 | priority_queue pq; 21 | 22 | int n; cin >> n; 23 | for (int i = 1; i <= n; i++) { 24 | int x, y; cin >> x >> y; 25 | 26 | tin[x]++; 27 | pq.push(-(y - 1)); 28 | } 29 | 30 | int ans = -1, cur = -1, qtt = 0; 31 | for (int i = 0; i < MAXN; i++) { 32 | if (pq.empty()) break; 33 | 34 | if (cur != -1) cur++; 35 | if (tin[i]) { 36 | cur = 1; 37 | qtt += tin[i]; 38 | } 39 | 40 | while (pq.size() && -pq.top() == i) { 41 | ans = max(ans, cur); 42 | pq.pop(); 43 | qtt--; 44 | } 45 | 46 | if (qtt) ans = max(ans, cur); 47 | } 48 | 49 | cout << ans << endl; 50 | } --------------------------------------------------------------------------------