├── .output └── main ├── .vscode └── launch.json ├── 2 Closest Points in 2D Plane (N log^2 N).cpp ├── 2-SAT.cpp ├── BIT ├── PURS.cpp ├── RUPS.cpp ├── RURS.cpp └── bit1.cpp ├── Bridge Tree in Graph.cpp ├── Bridges in Graph.cpp ├── Contests ├── ACM ICPC │ ├── ACM-ICPC Asia-Amritapuri Onsite Replay Contest 2017 │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ └── p4.py │ ├── ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017 │ │ ├── prob1.py │ │ ├── prob2.py │ │ ├── prob3.py │ │ ├── prob4.cpp │ │ ├── prob5.py │ │ └── prob6.py │ └── ACM-ICPC Asia-Kharagpur Onsite Replay Contest 2017 │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ └── p5.py ├── CODE JAM │ ├── Practice Session 2018 │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ └── p4.py │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ └── testing_tool.py ├── CODEVITA │ ├── CODEVITA 2017 │ │ ├── MOCKVITA │ │ │ ├── Geek_ CodeVita 2016 Round 2 Question_ The Vita Sum.pdf │ │ │ ├── Geek_ TCS CodeVita 2015 Round 2 _ Romeo and Juliet.pdf │ │ │ ├── co1.pdf │ │ │ ├── co2.pdf │ │ │ ├── co3.pdf │ │ │ ├── co4.pdf │ │ │ ├── co5.pdf │ │ │ ├── codevita4.pdf │ │ │ ├── codevita5.pdf │ │ │ ├── codevita6.pdf │ │ │ ├── codevitamock2-1.pdf │ │ │ ├── codevitamock2-2.pdf │ │ │ ├── codevitamock2-3.pdf │ │ │ ├── codevitamock2-4.pdf │ │ │ ├── codevitamock2-5.pdf │ │ │ ├── codevitamock2-6.pdf │ │ │ ├── codevitamock2-7.pdf │ │ │ └── codevitamock2-8.pdf │ │ ├── Round 1 TCS CodeVita Coding Questions PDFs │ │ │ ├── TCS_ CodeVita - Coding Arena Round1 A.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round1 B.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round1 C.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round1 D.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round1 E.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round1 F.pdf │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ └── p6.py │ │ └── Round 2 TCS CodeVita Coding Questions PDFs │ │ │ ├── TCS_ CodeVita - Coding Arena Round 2 A.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round 2 B.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round 2 C.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round 2 D.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round 2 E.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round 2 F.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round 2 G.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena Round 2 H.pdf │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ └── p6.py │ ├── CODEVITA 2018 │ │ ├── MOCKVITA1 │ │ │ ├── A.pdf │ │ │ ├── B.pdf │ │ │ ├── C.pdf │ │ │ ├── D.pdf │ │ │ ├── E.pdf │ │ │ ├── F.pdf │ │ │ ├── p1.cpp │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.cpp │ │ │ ├── p3.py │ │ │ └── p4.cpp │ │ ├── MOCKVITA2 │ │ │ ├── TCS_ CodeVita - Coding Adasrena.pdf │ │ │ ├── TCS_ CodeVita - Coding Arena.pdf │ │ │ ├── TCS_ CodeVita - Codingf Arena.pdf │ │ │ ├── ar.pdf │ │ │ ├── daa.pdf │ │ │ ├── fa.pdf │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ └── p6.py │ │ ├── MOCKVITA3 │ │ │ ├── 1.pdf │ │ │ ├── 2.pdf │ │ │ ├── 3.pdf │ │ │ ├── 4.pdf │ │ │ ├── 5.pdf │ │ │ ├── 6.pdf │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ └── p6.py │ │ ├── PRE_QUALIFIER1 │ │ │ ├── 01.pdf │ │ │ ├── 02.pdf │ │ │ ├── 05.pdf │ │ │ ├── B (1).pdf │ │ │ ├── B.pdf │ │ │ ├── Bride Hunting.pdf │ │ │ ├── C.pdf │ │ │ ├── Colliding Cannons (1).pdf │ │ │ ├── Colliding Cannons (2).pdf │ │ │ ├── Colliding Cannons.pdf │ │ │ ├── D.pdf │ │ │ ├── E.pdf │ │ │ ├── Integer Expressions (1).pdf │ │ │ ├── Integer Expressions.pdf │ │ │ ├── Movie Sequence (1).pdf │ │ │ ├── Movie Sequence.pdf │ │ │ ├── STRING ROTATION.docx │ │ │ ├── Sorted Rank List.pdf │ │ │ ├── Spelt Wonders (1).pdf │ │ │ ├── Spelt Wonders.pdf │ │ │ ├── String Rotation.pdf │ │ │ ├── p1.py │ │ │ ├── p2 (1).pdf │ │ │ ├── p2.pdf │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── questions codevita.pdf │ │ │ ├── rr.pdf │ │ │ └── special triangle.pdf │ │ ├── PRE_QUALIFIER2 │ │ │ ├── p1.py │ │ │ ├── p1a.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ └── p3a.py │ │ └── ROUND2 │ │ │ ├── attachments(1) │ │ │ ├── A.pdf │ │ │ ├── B.pdf │ │ │ ├── C.pdf │ │ │ ├── D.pdf │ │ │ ├── E.pdf │ │ │ ├── F.pdf │ │ │ ├── G.pdf │ │ │ └── H.pdf │ │ │ ├── p1.cpp │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ └── p3.cpp │ ├── PROBLEMS │ │ ├── 1.pdf │ │ ├── 10.pdf │ │ ├── 11.pdf │ │ ├── 12.pdf │ │ ├── 13.pdf │ │ ├── 14.pdf │ │ ├── 15.pdf │ │ ├── 16.pdf │ │ ├── 17.pdf │ │ ├── 18.pdf │ │ ├── 19.pdf │ │ ├── 2.pdf │ │ ├── 20.pdf │ │ ├── 21.pdf │ │ ├── 22.pdf │ │ ├── 23.pdf │ │ ├── 24.pdf │ │ ├── 25.pdf │ │ ├── 26.pdf │ │ ├── 27.pdf │ │ ├── 28.pdf │ │ ├── 29.pdf │ │ ├── 3.pdf │ │ ├── 30.pdf │ │ ├── 31.pdf │ │ ├── 32.pdf │ │ ├── 33.pdf │ │ ├── 34.pdf │ │ ├── 35.pdf │ │ ├── 36.pdf │ │ ├── 37.pdf │ │ ├── 38.pdf │ │ ├── 39.pdf │ │ ├── 4.pdf │ │ ├── 40.pdf │ │ ├── 41.pdf │ │ ├── 42.pdf │ │ ├── 43.pdf │ │ ├── 44.pdf │ │ ├── 45.pdf │ │ ├── 46.pdf │ │ ├── 47.pdf │ │ ├── 48.pdf │ │ ├── 49.pdf │ │ ├── 5.pdf │ │ ├── 50.pdf │ │ ├── 51.pdf │ │ ├── 52.pdf │ │ ├── 53.pdf │ │ ├── 54.pdf │ │ ├── 55.pdf │ │ ├── 56.pdf │ │ ├── 57.pdf │ │ ├── 58.pdf │ │ ├── 59.pdf │ │ ├── 6.pdf │ │ ├── 60.pdf │ │ ├── 61.pdf │ │ ├── 62.pdf │ │ ├── 63.pdf │ │ ├── 64.pdf │ │ ├── 65.pdf │ │ ├── 66.pdf │ │ ├── 67.pdf │ │ ├── 68.pdf │ │ ├── 69.pdf │ │ ├── 7.pdf │ │ ├── 70.pdf │ │ ├── 8.pdf │ │ ├── 9.pdf │ │ └── STRING ROTATION.docx │ ├── SOLUTIONS │ │ ├── 70 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ └── p6.py │ │ ├── p1.py │ │ ├── p10.py │ │ ├── p11.py │ │ ├── p12.py │ │ ├── p13.py │ │ ├── p14.py │ │ ├── p15.py │ │ ├── p16.py │ │ ├── p17.py │ │ ├── p18.py │ │ ├── p19.py │ │ ├── p2.py │ │ ├── p20.py │ │ ├── p21.py │ │ ├── p22.py │ │ ├── p23.py │ │ ├── p24.py │ │ ├── p25.py │ │ ├── p26.py │ │ ├── p27.py │ │ ├── p28.py │ │ ├── p29.py │ │ ├── p3.py │ │ ├── p30.py │ │ ├── p31.py │ │ ├── p32.py │ │ ├── p33.py │ │ ├── p34.py │ │ ├── p35.py │ │ ├── p36.py │ │ ├── p37.py │ │ ├── p38.py │ │ ├── p39.py │ │ ├── p4.cpp │ │ ├── p40.py │ │ ├── p41.py │ │ ├── p42.py │ │ ├── p43.py │ │ ├── p44.py │ │ ├── p45.py │ │ ├── p46.py │ │ ├── p47.py │ │ ├── p48.py │ │ ├── p49.py │ │ ├── p5.py │ │ ├── p50.py │ │ ├── p51.py │ │ ├── p52.py │ │ ├── p53.py │ │ ├── p54.py │ │ ├── p55.py │ │ ├── p56.py │ │ ├── p57.py │ │ ├── p58.py │ │ ├── p59.py │ │ ├── p60.py │ │ ├── p61.py │ │ ├── p62.py │ │ ├── p63.py │ │ ├── p64.py │ │ ├── p65.py │ │ ├── p66.py │ │ ├── p67.py │ │ ├── p68.py │ │ ├── p69.py │ │ ├── p7.py │ │ ├── p70.py │ │ ├── p8.py │ │ └── p9.py │ └── TCS DIGITAL TEST │ │ ├── binary_to_decimal.py │ │ ├── p1.py │ │ ├── p3.py │ │ └── palinrome.py ├── College Contests │ ├── Code-Golf │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ └── p5.py │ ├── CodeNight │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.cpp │ │ ├── p4.cpp │ │ ├── p5.cpp │ │ └── p5.py │ ├── Codenigma │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ └── p4.py │ ├── Innerve Summer Code Challenge │ │ ├── p1.py │ │ ├── p10.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.cpp │ │ ├── p4.py │ │ ├── p5.cpp │ │ ├── p6.cpp │ │ ├── p7.py │ │ ├── p8.py │ │ └── p9.py │ ├── ALKHWARIZM │ │ ├── ALKHWARIZM 2012 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.cpp │ │ │ ├── p4.cpp │ │ │ └── p5.py │ │ ├── ALKHWARIZM 2015 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ └── p3.cpp │ │ ├── ALKHWARIZM 2016 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ └── p3.cpp │ │ ├── ALKHWARIZM 2017 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ └── p3.cpp │ │ └── ALKHWARIZM 2018 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ ├── p3.cpp │ │ │ └── p4.cpp │ ├── Blueprint │ │ └── p1.py │ ├── CODEOUT │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ ├── p5.py │ │ ├── p6.py │ │ ├── p7.py │ │ └── p8.py │ ├── CODERVATION IERT │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ ├── p5.cpp │ │ ├── p6.py │ │ ├── p7.py │ │ └── p8.py │ ├── Code Grounds │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ └── p4.py │ ├── Code Melange (Rated) │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ └── p5.py │ ├── Code-XII │ │ ├── p1.py │ │ ├── p2.cpp │ │ └── p3.py │ ├── CodeWhizz 1.0 │ │ ├── AlgoFlux Online Qualifier │ │ │ ├── prob1.py │ │ │ ├── prob2.py │ │ │ └── prob3.py │ │ ├── a.out │ │ ├── bitpair.py │ │ ├── sara.py │ │ └── tomandjourney.cpp │ ├── Codemania │ │ ├── prob1.py │ │ ├── prob2.py │ │ ├── prob3.py │ │ ├── prob4.cpp │ │ ├── prob5.cpp │ │ ├── prob6.cpp │ │ └── prob7.cpp │ ├── Coders' Legacy │ │ ├── Coder's Legacy 2016 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ ├── p3.cpp │ │ │ ├── p4.py │ │ │ └── p5.py │ │ ├── Coder's Legacy 2017A │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ └── p6.cpp │ │ └── Coder's Legacy 2018 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.cpp │ │ │ ├── p4.cpp │ │ │ ├── p4a.cpp │ │ │ └── p5.py │ ├── Coding Arena 2018 Qualifiers │ │ ├── p1.py │ │ └── p2.py │ ├── Cook-a-Code │ │ ├── p1.py │ │ └── p2.py │ ├── DEMENTIA │ │ ├── DEMENTIA 16 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.cpp │ │ │ ├── p4.cpp │ │ │ └── p5.py │ │ ├── Dementia (Rated for Division 2) │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ ├── Dementia 2012 - IIT Mandi │ │ │ ├── p1.cpp │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.cpp │ │ │ ├── p5.py │ │ │ ├── p6.py │ │ │ ├── p7.py │ │ │ └── p8.cpp │ │ ├── Dementia 2014 │ │ │ ├── p1.cpp │ │ │ ├── p2.cpp │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ ├── p6.cpp │ │ │ └── p7.py │ │ └── Dementia 2015 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ ├── p3.cpp │ │ │ ├── p4.cpp │ │ │ ├── p5.py │ │ │ ├── p6.py │ │ │ └── p7.py │ ├── Da-vinci Code │ │ ├── p1.py │ │ ├── p2.cpp │ │ ├── p3.py │ │ ├── p4.py │ │ ├── p5.py │ │ ├── p6.py │ │ └── p7.py │ ├── Encipher │ │ ├── prob1.py │ │ └── prob2.py │ ├── Fool You │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ └── p5.py │ ├── GST February SRM │ │ ├── prob1.py │ │ ├── prob2.py │ │ └── prob3.py │ ├── GST January SRM │ │ ├── problem1.py │ │ ├── problem2.py │ │ └── problem3.cpp │ ├── Game of Codes │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ └── p4.py │ ├── Hacktivate │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.cpp │ │ └── p5.py │ ├── IGNITION │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ └── p5.py │ ├── KJSCE │ │ ├── KJSCE Code Spree 2015 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ ├── KJSCE CodeSpree 2016 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ ├── KJSCE CodeSpree 2017 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ ├── KJSCE Codecell Practice Round 2 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ ├── KJSCE Coding Challenge 2015 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ ├── p6.py │ │ │ └── p7.cpp │ │ ├── KJSCE Coding Challenge 2016 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ ├── KJSCE Coding Challenge 2017 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ └── KJSCE Coding Challenge 2018 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.cpp │ │ │ ├── p4.py │ │ │ ├── p5.py │ │ │ └── p6.py │ ├── Keteki │ │ ├── Keteki Beta Contest │ │ │ ├── p1.py │ │ │ └── p2.cpp │ │ ├── Keteki Power Match 2 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ └── p3.py │ │ ├── Keteki Quick Match 10 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ └── p2.py │ │ ├── Keteki Quick Match 2 │ │ │ ├── p1.py │ │ │ └── p2.py │ │ ├── Keteki Quick Match 4 │ │ │ ├── p1.py │ │ │ └── p2.py │ │ ├── Keteki Quick Match 6.5 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── prob1.py │ │ │ └── prob2.cpp │ │ ├── Keteki Quick Match 8 │ │ │ ├── p1.py │ │ │ └── p2.py │ │ ├── Keteki Quick Match 9 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ └── p3.py │ │ ├── Keteki Quick Match-6 │ │ │ ├── prob1.py │ │ │ └── prob2.py │ │ ├── Keteki Quick Power Match │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ └── p3.py │ │ └── Keteki quick match 6 │ │ │ ├── p1.py │ │ │ └── p2.py │ ├── LOC │ │ ├── LoC July 2018 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ ├── p3.py │ │ │ └── p4.cpp │ │ ├── LoC April 2018 (Rated for Division 2) │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ └── LoC June 2018 │ │ │ ├── p1.py │ │ │ ├── p2.cpp │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ ├── p4a.py │ │ │ └── p5.py │ ├── NITD Code Golf │ │ ├── p1.py │ │ ├── p2.py │ │ └── p3.py │ ├── NITD Reverse Coding v2.0 │ │ ├── p1.py │ │ ├── p10.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ ├── p5.py │ │ ├── p6.py │ │ ├── p7.py │ │ ├── p8.py │ │ └── p9.py │ ├── Overnite Coding │ │ ├── prob1.py │ │ └── prob2.cpp │ └── comp.exe │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ ├── p5.py │ │ └── p6.py ├── FB HACKER CUP │ ├── Facebook Hacker Cup 2017 Qualification Round │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ ├── progress_pie.txt │ │ ├── progress_pie_example_input.txt │ │ ├── progresspie.out │ │ └── progresspieexampleinput.out │ ├── Facebook Hacker Cup 2018 Qualification Round │ │ ├── interception.out │ │ ├── interception.txt │ │ ├── p1.py │ │ ├── p1 │ │ │ ├── tourist.out │ │ │ └── tourist.txt │ │ ├── p2.py │ │ └── p3.py │ └── Facebook Hacker Cup 2018 Round1 │ │ ├── p1.py │ │ ├── p1 │ │ ├── tourist.out │ │ └── tourist.txt │ │ ├── p2.py │ │ └── p3.py ├── KICKSTART │ ├── 2018 │ │ ├── Kickstart Round B 2018 │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ ├── Round A │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ ├── p4.py │ │ │ └── p5.py │ │ └── Round D │ │ │ ├── abc.txt │ │ │ ├── p1.py │ │ │ ├── p2.py │ │ │ ├── p3.py │ │ │ └── p4.py │ └── Google_APAC-master │ │ ├── .gitignore │ │ ├── 2016_A │ │ ├── B_gcube.py │ │ ├── gcampus.py │ │ ├── googol.py │ │ └── gsanke.py │ │ ├── 2017_A │ │ ├── B_Rain.py │ │ ├── C_Jane's_Flower_Shop.py │ │ ├── D_Clash_Royale.py │ │ └── p1.py │ │ ├── 2017_B │ │ ├── A_Sherlock_and_Parentheses.py │ │ ├── B_Sherlock_and_Watson_Gym_Secrets.py │ │ ├── C_Watson_and_Intervals.py │ │ └── D_Sherlock_and_Permutation_Sorting.py │ │ ├── 2017_C │ │ ├── A_Monster_Path.py │ │ ├── B_Safe_Squares.py │ │ ├── C_Evaluation.py │ │ ├── D_Soldiers.cpp │ │ └── D_Soldiers.py │ │ ├── 2017_Practice │ │ ├── A_Lazy_Spelling_Bee.py │ │ └── B_Robot_Rock_Band.py │ │ └── README.md └── Others │ ├── C Cubed │ ├── prob1.py │ └── prob2.py │ ├── CODE BATTLELive │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ ├── p5.py │ └── p6.cpp │ ├── Code Conquest │ └── prob1.py │ ├── GST March SRM │ ├── p1.py │ ├── p2.py │ └── p3.py │ ├── Insomnia 2018 (Rated) │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ └── p5.py │ ├── International Coding League (Rated) │ ├── p1.py │ ├── p2.py │ ├── p3.cpp │ └── p4.cpp │ ├── Week of Code │ └── Week of Code 37 │ │ ├── p1.py │ │ ├── p2.py │ │ ├── p3.py │ │ ├── p4.py │ │ └── p5.py │ ├── BUG4EVER │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ ├── p5.py │ ├── p6.cpp │ └── p7.cpp │ ├── Bytecode 2018 │ └── prob1.py │ ├── COUT-2K18 │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ ├── p5.cpp │ ├── p6.py │ ├── p7.cpp │ └── p8.cpp │ ├── Code-e-Mania │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ ├── p5.py │ └── p6.py │ ├── CodeItUp │ ├── p1.py │ └── p2.py │ ├── Codefeast │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ ├── p5.py │ └── p6.py │ ├── Encoding August '18 │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ ├── p5.py │ ├── p6.cpp │ ├── p7.py │ ├── p8.cpp │ ├── p8.py │ └── p9.cpp │ ├── GST SRM Aug │ ├── p1.cpp │ ├── p1.py │ ├── p2.py │ └── p3.py │ ├── Hackon Feb │ ├── prob1.py │ ├── prob2.py │ ├── prob3.py │ ├── prob4.py │ ├── prob5.py │ └── prob6.py │ ├── MNM Online Programming Contest │ ├── p1.py │ ├── p2.cpp │ ├── p3.py │ ├── p4.py │ └── p5.py │ ├── MSTC March FaceOff │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ ├── p5.py │ ├── p6.py │ └── p7.py │ ├── Noobie Night │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.py │ └── p5.py │ ├── Practice │ └── medium │ │ ├── flip.py │ │ ├── nextpalin.py │ │ ├── numfact.py │ │ └── treeroot.py │ ├── Pradnya Wildcard Round │ ├── p1.py │ └── p2.cpp │ ├── Python-modules-for-competitive-programming-master │ ├── BFS.py │ ├── BST.py │ ├── Check if a graph is bipartite or not.py │ ├── Check if a number is a power of 2 or not.py │ ├── Check if a number is prime or not.py │ ├── Coin Change Minimum Number of Coins.py │ ├── Combinations.py │ ├── Connected Components using DFS.py │ ├── DFS.py │ ├── Day on a particular date.py │ ├── Factors or divisors of a number.py │ ├── Fenwick Tree or Binary Indexed Tree.py │ ├── Fibonacci Series in logn.py │ ├── Fractions in reduced form.py │ ├── GCD of n numbers.py │ ├── KMP Algorithm for pattern search.py │ ├── Knapsack 01.py │ ├── LCM of n numbers.py │ ├── Lexicographic rank of a string.py │ ├── List of ncr.py │ ├── Longest Common Subsequence.py │ ├── Lowest Common Ancestor (Optimised).py │ ├── Lowest Common Ancestor.py │ ├── Maximum of all mods in an array.py │ ├── Maximum of all subarrays of size k (Sliding Window).py │ ├── Maximum subarray sum mod m.py │ ├── Minimum Spanning Tree (Prims's).py │ ├── Modular Inverse when mod is prime.py │ ├── Modular Inverse.py │ ├── Number of integral points in a triangle.py │ ├── Number of prime factors of a number.py │ ├── Number of values in a range.py │ ├── Prime factors of a number.py │ ├── Prime numbers till n.py │ ├── Print matrix diagonally.py │ ├── Segment Tree - Range Minimum Query (RMQ).py │ ├── Segment Tree - Sum of a given range.py │ ├── Submatrix Sum Queries.py │ ├── Subset Sum.py │ ├── Sum of factors or divisors of a number.py │ ├── Sum of maximum elements of sliding window of length K using fenwick tree.py │ ├── Sum of products of all possible subarrays.py │ ├── Z Algorithm for pattern search.py │ ├── isprime.py │ └── ncr mod m.py │ ├── TSEC August Codeathon │ ├── p1.py │ ├── p2.py │ ├── p3.py │ ├── p4.cpp │ └── p5.cpp │ ├── TSEC Codeathon │ ├── p1.py │ ├── p10.cpp │ ├── p11.py │ ├── p2.py │ ├── p3.cpp │ ├── p4.cpp │ ├── p5.cpp │ ├── p6.py │ ├── p7.py │ ├── p8.py │ └── p9.py │ ├── Tessellathon │ ├── p1.cpp │ └── prob1.cpp │ ├── Turing Cup Qualifier 1 │ ├── p1 (another copy).py │ ├── p1 (copy).py │ ├── p1.py │ └── p2.py │ ├── Turing Cup Qualifier 2 │ ├── p1.py │ ├── p2.py │ ├── p3.py │ └── p4.cpp │ └── Women's CodeSprint 5 │ └── p1.py ├── Convex Hull (Dynamic) ├── Convex Hull (Graham's Scan).cpp ├── DSU ├── DSU (Disjoint Set Union).cpp ├── DSU On Trees.cpp ├── DSU.cpp └── dsu_hackerearth.cpp ├── Dijkstra.cpp ├── Division.cpp ├── Djikstra.cpp ├── Edit_Distance.cpp ├── Euler_phi.cpp ├── Exponentiation.cpp ├── Extended Euclidean Algorithm (Extensive).cpp ├── Extended_euclid.cpp ├── FFT (Iterative) ├── FFT (Recursive) ├── FFTApplications.cpp ├── Factorials with nCr.cpp ├── FlyodWarshall(Dj).cpp ├── Gaussian Elimination.cpp ├── Hashing (Strings) ├── IO_int128.cpp ├── Intervals Handling ├── KMP.cpp ├── Kruskal's MST.cpp ├── LCA ├── LCA (with Time Traversal DFS).cpp ├── LCA.cpp └── lca1.cpp ├── LCS.cpp ├── Lcp_kasai.cpp ├── Logical CF Problems ├── Longest_Palindromic_substring(Manchar's).cpp ├── MO └── mo.cpp ├── Matching (Hopcroft-Karp) in Bipartite Graph ├── Matrix Struct.cpp ├── MaxFlow - Push Relabel [V^2 sqrt(E)] ├── MaxFlow - Push Relabel [V^3] ├── Merge-sort_tree.cpp ├── Min Cost Max Flow - Dijkstra ├── Minimum_spanning_tree(Kruskal).cpp ├── Nth_Fibonacci.cpp ├── Power_matrix.cpp ├── Prim's MST.cpp ├── README.md ├── Rabin-Karp.cpp ├── Random Generator ├── SQRT_TREE.cpp ├── Sort.cpp ├── Sparse Matrix (RMQ).cpp ├── Strongly Connected Component.cpp ├── Tree Construction with Specific Vertices ├── Tree ├── HLD.cpp ├── SmallToLarge.cpp ├── binary_tree.cpp ├── centroid_decomposition.cpp ├── centroid_example.cpp └── lca_sparse_min.cpp ├── UF_DSU.cpp ├── Union_find.cpp ├── Z Algorithm.cpp ├── algorepo ├── DataStructures │ ├── BIT │ │ ├── BIT1.c │ │ ├── BIT2.c │ │ ├── BIT3.c │ │ └── inversion_count.cpp │ ├── Union_Find.cpp │ ├── segmentTree │ │ ├── 2dSegmentTree.cpp │ │ └── rangeSumSegmentTree.cpp │ ├── sparse table │ │ ├── 1D_RMQ.cpp │ │ └── 2D_RMQ.cpp │ └── stack │ │ └── Histogram.cpp ├── LICENSE ├── README.md ├── bigint.cpp ├── computational geometry │ └── closest point pair.cpp ├── dp │ ├── CoinChange.cpp │ ├── Coin_ways.cpp │ ├── LCS.cpp │ ├── LCS_spaceOptimized.cpp │ ├── LIS_LDS_BTS.cpp │ ├── catalanNumber.cpp │ ├── kadane.cpp │ ├── kadaneIdx.cpp │ ├── maxSumRectangle.cpp │ └── subMatrixWithAll1s.cpp ├── graph │ ├── 0-1BFS.cpp │ ├── BFS.cpp │ ├── DFS.cpp │ ├── Kruskal_MST.cpp │ ├── LCA_logn.cpp │ ├── LCA_new.cpp │ ├── articulationPoint.cpp │ ├── bellmanFord.cpp │ ├── bridges.cpp │ ├── checkBridge.cpp │ ├── checkEuler.cpp │ ├── check_bipartite.cpp │ ├── cycleDetection_directedGraph.cpp │ ├── cycleDetection_undirectedGraph.cpp │ ├── dijikstra.cpp │ ├── flloydWarshall.cpp │ ├── kosaraju_SCC.cpp │ └── topologicalSort.cpp ├── linearAlgebra │ └── gaussian_elimination.cpp ├── numberTheory │ ├── LinearModularEquation.cpp │ ├── discreteLog.cpp │ ├── etf.cpp │ ├── matrix_expo.cpp │ ├── modexpo.cpp │ ├── modular_multiplicative_inverse.cpp │ ├── nCr.cpp │ ├── nCrModP.cpp │ ├── nCrModP_pascal.cpp │ └── sieve_fast.cpp ├── stringTheory │ ├── KMP.cpp │ └── trie.cpp └── template.cpp ├── assign.cpp ├── bfs ├── BFS ├── BFS 2D ├── bfs.cpp ├── bfs1.cpp ├── bfs2.cpp └── bfs3.cpp ├── binary_search ├── bs.cpp └── bs1.cpp ├── bit.cpp ├── bitmasking+dp └── different_caps.cpp ├── bitset_string_int.cpp ├── circles.cpp ├── codechef_template.cpp ├── convexHull.cpp ├── cyclic_check.cpp ├── dfs ├── DFS - Cycle Detection in Directed Graph ├── dfs.cpp ├── dfs1.cpp ├── dfs2.cpp ├── dfs3.cpp ├── dfs4.cpp └── dfs5.cpp ├── diameter_of_a_tree.cpp ├── digit_dp └── digit_dp.cpp ├── dijkstra.cpp ├── dp ├── dp-on-digits.cpp ├── dp-on-matrices.cpp ├── dp-on-tree-type-c-LCA.cpp ├── dp.cpp ├── dp_bit_masking1.cpp ├── dp_bit_masking2.cpp ├── dp_string.cpp ├── dp_top_down.1.cpp ├── dp_top_down.2.cpp ├── dp_top_down.cpp └── memoization.cpp ├── dynamic_segment_tree.cpp ├── edmonds_karp_maxflow.cpp ├── eggDrop.cpp ├── expression.cpp ├── fenwick └── Fenwick_tree.cpp ├── fibonacci.cpp ├── find_cycle.cpp ├── inverse_modulo_map.cpp ├── iterative-tree-traversal-1.cpp ├── iterative-tree-traversal-2.cpp ├── kadane.cpp ├── leetcode ├── leetcode.cpp ├── leetcode.o ├── leetcode1 ├── leetcode1.cpp ├── leetcode1.o ├── level_order.py ├── make_pair.cpp ├── matrix_expo.cpp ├── mobius.cpp ├── nikhil_temp.cpp ├── nim_game ├── nim_game1.cpp ├── nim_game2.cpp └── nim_game3.cpp ├── p1.py ├── p2.py ├── parallel_binary_search.cpp ├── policy_based_DS.cpp ├── power.h ├── preTopost.cpp ├── prime_check(Miller-Rabin).cpp ├── prime_check.cpp ├── prime_factorization.cpp ├── prime_sieve.cpp ├── priorityQueue.cpp ├── productoffact.cpp ├── sa_lcp.cpp ├── sa_lcp2.cpp ├── samsung_interview_prep ├── abc.cpp ├── prob1.cpp └── prob2.cpp ├── sccgraph.cpp ├── segment_tree ├── Segment_tree.cpp ├── count_inversions.cpp ├── segment_tree1.cpp ├── segment_tree2.cpp ├── segment_tree3.cpp ├── segment_tree4.cpp ├── segment_tree5.cpp ├── segment_tree6.cpp ├── segment_tree7.cpp ├── segment_tree8.cpp └── segment_tree_lazy.cpp ├── sliding_window.cpp ├── slidingwindow.cpp ├── sparse_table ├── RMQ.cpp ├── RMQ2.cpp ├── RMQ3.cpp └── sptable.cpp ├── squareroot.cpp ├── stack_queue ├── priority_queue.cpp ├── raman_stack.cpp ├── stack.cpp └── stack_queue.cpp ├── suffix_array.cpp ├── sumOfPermutations.cpp ├── tarjan_scc.cpp ├── template_addition.cpp ├── ternary_search.cpp ├── topo.cpp ├── topological_sort.cpp ├── tricky-oops-problem.cpp ├── trie ├── Trie.cpp ├── trie-xor.cpp ├── trie1.cpp ├── trie2.cpp ├── trie3.cpp ├── trie4.cpp ├── trie5.cpp ├── trie6.cpp ├── trie7.cpp ├── trie8.cpp └── trie9.cpp └── union_find.cpp /.output/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/.output/main -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "(gdb) Launch", 9 | "type": "cppdbg", 10 | "request": "launch", 11 | "program": "enter program name, for example ${workspaceFolder}/a.out", 12 | "args": [], 13 | "stopAtEntry": false, 14 | "cwd": "${workspaceFolder}", 15 | "environment": [], 16 | "externalConsole": true, 17 | "MIMode": "gdb", 18 | "setupCommands": [ 19 | { 20 | "description": "Enable pretty-printing for gdb", 21 | "text": "-enable-pretty-printing", 22 | "ignoreFailures": true 23 | } 24 | ] 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /Bridges in Graph.cpp: -------------------------------------------------------------------------------- 1 | int tim=0; 2 | int u[N], v[N], vis[N]; 3 | int tin[N], tout[N], isBridge[M], minAncestor[N]; 4 | vector > g[N]; //vertex, index of edge 5 | 6 | void dfs(int k, int par) 7 | { 8 | vis[k]=1; 9 | tin[k]=++tim; 10 | minAncestor[k]=tin[k]; 11 | for(auto it:g[k]) 12 | { 13 | if(it.first==par) 14 | continue; 15 | if(vis[it.first]) 16 | { 17 | minAncestor[k]=min(minAncestor[k], tin[it.first]); 18 | continue; 19 | } 20 | dfs(it.first, k); 21 | minAncestor[k]=min(minAncestor[k], minAncestor[it.first]); 22 | if(minAncestor[it.first]>tin[k]) 23 | isBridge[it.second]=1; 24 | } 25 | tout[k]=tim; 26 | } 27 | 28 | //Sample Problem 1: https://www.spoj.com/problems/EC_P/ 29 | //Sample Solution 1: http://p.ip.fi/shDA 30 | -------------------------------------------------------------------------------- /Contests/ACM ICPC/ACM-ICPC Asia-Amritapuri Onsite Replay Contest 2017/p2.py: -------------------------------------------------------------------------------- 1 | from sys import stdin 2 | t = int(stdin.readline()) 3 | def gcd(a,b): 4 | while a%b: 5 | x = a%b 6 | a=b 7 | b=x 8 | return b 9 | for _ in xrange(t): 10 | a,b,c = map(int,stdin.readline().split()) 11 | d,e,f = map(int,stdin.readline().split()) 12 | x1,x2 = map(int,stdin.readline().split()) 13 | den = 6 14 | num = 2 * (a-d) * (x1**3 - x2**3) + 3 * (b-e) * (x1**2 - x2**2) + 6 * (c-f) * (x1 - x2) 15 | if num < 0: 16 | num = -num 17 | g = gcd(num,den) 18 | print "%d/%d"%(num/g,den/g) 19 | -------------------------------------------------------------------------------- /Contests/ACM ICPC/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob1.py: -------------------------------------------------------------------------------- 1 | for __ in range(int(input())): 2 | n = int(raw_input()) 3 | # map(int, raw_input().split()) 4 | a = [0,0,0] 5 | b = [100,100,100] 6 | for i in range(n): 7 | [c,l,x] = map(int, raw_input().split()) 8 | if x>a[l-1]: 9 | a[l-1]=x 10 | b[l-1]=c 11 | elif x==a[l-1]: 12 | if b[l-1]>c: 13 | b[l-1]=c 14 | for i in xrange(3): 15 | print str(a[i])+' '+str(b[i]) 16 | -------------------------------------------------------------------------------- /Contests/ACM ICPC/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob2.py: -------------------------------------------------------------------------------- 1 | for kohli in range(input()): 2 | n = int(input()) 3 | x=[] 4 | y=[] 5 | xx=[] 6 | yy=[] 7 | ans=[] 8 | for i in range(n): 9 | a,b = map(int,raw_input().split(' ')) 10 | x.append(a) 11 | y.append(b) 12 | # print(x,y) 13 | for i in range(n): 14 | yy.append([y[i],i]) 15 | print(yy) 16 | yy.sort(key = lambda x:x[0]) 17 | print(yy) 18 | yy = yy[::-1] 19 | print(yy) 20 | for i in yy: 21 | if(len(ans)==3): 22 | break 23 | if(x[i[1]] in xx): 24 | continue 25 | else: 26 | xx.append(x[i[1]]) 27 | ans.append(i[0]) 28 | if(len(ans)==3): 29 | print(sum(ans)) 30 | else: 31 | print('0') 32 | -------------------------------------------------------------------------------- /Contests/ACM ICPC/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob3.py: -------------------------------------------------------------------------------- 1 | # cook your code here 2 | t=int(raw_input()) 3 | for a0 in xrange(t): 4 | a,k=map(float,raw_input().split()) 5 | b=map(float,raw_input().split()) 6 | # print a,k,b 7 | m=min(b) 8 | if m<0: 9 | for i in xrange(3): 10 | b[i]+=abs(m) 11 | [x1,x2,x3]=sorted(b) 12 | if (k >= (x3-x1)/2): # k is the move we can take 13 | print float(a*a) 14 | else: 15 | if x3-x1-2*k>a: print float(0) 16 | else: print abs(x3-x1-2*k-a)*a 17 | -------------------------------------------------------------------------------- /Contests/ACM ICPC/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob6.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/ACM ICPC/ACM-ICPC Asia-Gwalior Onsite Replay Contest 2017/prob6.py -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/Geek_ CodeVita 2016 Round 2 Question_ The Vita Sum.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/Geek_ CodeVita 2016 Round 2 Question_ The Vita Sum.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/Geek_ TCS CodeVita 2015 Round 2 _ Romeo and Juliet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/Geek_ TCS CodeVita 2015 Round 2 _ Romeo and Juliet.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co1.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co2.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co3.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co4.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/co5.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevita4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevita4.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevita5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevita5.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevita6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevita6.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-1.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-2.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-3.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-4.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-5.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-6.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-7.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/MOCKVITA/codevitamock2-8.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 A.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 A.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 B.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 B.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 C.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 C.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 D.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 D.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 E.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 E.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 F.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 1 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round1 F.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 A.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 A.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 B.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 B.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 C.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 C.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 D.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 D.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 E.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 E.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 F.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 F.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 G.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 G.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 H.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2017/Round 2 TCS CodeVita Coding Questions PDFs/TCS_ CodeVita - Coding Arena Round 2 H.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/A.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/A.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/B.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/B.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/C.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/C.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/D.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/D.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/E.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/E.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/F.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/F.pdf -------------------------------------------------------------------------------- /Contests/CODEVITA/CODEVITA 2018/MOCKVITA1/p1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Input 3 | 5 4 | 20,-20,0,50,50 5 | Output 6 | 7 7 | */ 8 | #include 9 | int main() 10 | { 11 | int a[100],m,i,k,j,c=0; 12 | char b[50]; 13 | scanf("%d",&m); 14 | for(i=0;i0: 12 | print a[-1] 13 | else: 14 | print "-1" 15 | -------------------------------------------------------------------------------- /Contests/CODEVITA/TCS DIGITAL TEST/palinrome.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | n = sys.argv[1] 4 | 5 | n1 = n[::-1] 6 | 7 | if n==n1: 8 | print "Palindrome" 9 | else: 10 | print "Not Palindrome" 11 | 12 | 13 | #### Actual way to check palindrome 14 | 15 | -------------------------------------------------------------------------------- /Contests/College Contests/ CodeNight/p3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long lnt; 4 | int a[110],b[110]; 5 | int x[110],y[110],ok[110]; 6 | int main(){ 7 | int n; 8 | scanf("%d",&n); 9 | for(int k=1;k<=n;k++) scanf("%d%d",&a[k],&b[k]); 10 | for(int k=1;k<=n;k++){ 11 | if(k<=n/2) x[k]=a[k]-b[k]; 12 | else y[k-n/2]=b[k]; 13 | } 14 | sort(x+1,x+n/2+1); 15 | sort(y+1,y+n/2+1); 16 | int ans=0; 17 | for(int k=n/2;k>=1;k--){ 18 | for(int i=n/2;i>=1;i--){ 19 | if(ok[i]) continue; 20 | if(y[k]<=x[i]){ 21 | ans++; 22 | ok[i]=1; 23 | break; 24 | } 25 | } 26 | } 27 | printf("%d\n",ans); 28 | } 29 | -------------------------------------------------------------------------------- /Contests/College Contests/ Codenigma/p4.py: -------------------------------------------------------------------------------- 1 | a=[1,9] 2 | # print a 3 | mod=10**9+7 4 | t=int(raw_input()) 5 | while t!=0: 6 | t=t-1 7 | n=int(raw_input()) 8 | print (pow(2,n-1,mod)*(pow(4,n,mod)+pow(5,n,mod))%mod) 9 | # print (((pow(8,n,mod)+pow(10,n,mod)))%mod)/2 10 | 11 | ''' 12 | 2 13 | 1 14 | 2 15 | 16 | 9 17 | 82 18 | ''' 19 | -------------------------------------------------------------------------------- /Contests/College Contests/ Innerve Summer Code Challenge/p4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define mx 100005 4 | #define mod 1000000009 5 | #define lim 100 6 | using namespace std; 7 | 8 | int seg[mx]; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(false); 12 | memset(seg,0,sizeof(seg)); 13 | 14 | int n,m;cin>>n>>m; 15 | 16 | while(m--) 17 | { 18 | int l,r;cin>>l>>r;l--,r--; 19 | seg[l]++,seg[r+1]--; 20 | } 21 | 22 | for(int i=1;i>q; 26 | 27 | while(q--) 28 | { 29 | int k;cin>>k; 30 | int low=lower_bound(seg,seg+n,k)-seg; 31 | low++; 32 | cout< 2 | 3 | #define ll long long 4 | #define mx 200005 5 | #define mod 1000000009 6 | #define lim 100 7 | using namespace std; 8 | 9 | 10 | ll power(ll a,ll b) 11 | { 12 | ll x=1,y=a; 13 | while(b) 14 | { 15 | if(b%2) 16 | { 17 | x*=y; 18 | x%=mod; 19 | } 20 | b/=2; 21 | y*=y; 22 | y%=mod; 23 | } 24 | return x; 25 | } 26 | 27 | mapmp; 28 | 29 | int main(){ 30 | ios_base::sync_with_stdio(false); 31 | int n,q;cin>>n>>q; 32 | 33 | while(n--) 34 | { 35 | ll a,b,c;cin>>a>>b; 36 | c=4*a*b+2*max(a,b); 37 | mp[c]++; 38 | } 39 | 40 | while(q--) 41 | { 42 | ll k;cin>>k; 43 | ll fre=mp[(k*(k+1))]; 44 | if(fre) 45 | { 46 | cout< 2 | 3 | #define ll long long 4 | #define mx 200005 5 | #define mod 1000000007 6 | #define lim 100 7 | using namespace std; 8 | 9 | 10 | ll power(ll a,ll b) 11 | { 12 | ll x=1,y=a; 13 | while(b) 14 | { 15 | if(b%2) 16 | { 17 | x*=y; 18 | x%=mod; 19 | } 20 | b/=2; 21 | y*=y; 22 | y%=mod; 23 | } 24 | return x; 25 | } 26 | 27 | 28 | int main(){ 29 | ios_base::sync_with_stdio(false); 30 | 31 | int t;cin>>t; 32 | while(t--) 33 | { 34 | ll n,k;cin>>n>>k; 35 | 36 | if(k==1) 37 | { 38 | cout<<1<<"\n"; 39 | continue; 40 | } 41 | ll nd=((1-power(k,n))+mod)%mod; 42 | ll rd=(power(1-k,mod-2)+mod)%mod; 43 | ll ans=(((rd*nd)%mod)*k)%mod; 44 | 45 | cout<=li[heap[-1]]: 7 | heap.pop() 8 | heapq.heappush(heap,i) 9 | for i in xrange(k,n): 10 | ans.append(li[heap[0]]) 11 | while heap!=[] and heap[0]<=i-k: 12 | heap.pop(0) 13 | while heap!=[] and li[i]>=li[heap[-1]]: 14 | heap.pop() 15 | heapq.heappush(heap,i) 16 | ans.append(li[heap[0]]) 17 | return ans 18 | n,k=6,3 19 | li=[5,1,4,2,3,9] 20 | print subarray_max(li,n,k) 21 | -------------------------------------------------------------------------------- /Contests/College Contests/Code-XII/p3.py: -------------------------------------------------------------------------------- 1 | def solve(N, A): 2 | T = N / 4 3 | count = [0, 0, 0, 0] 4 | for x in A: count[x] += 1 5 | if all(x <= T for x in count): return 0 6 | j = 0 7 | ans = N 8 | for i, x in enumerate(A): 9 | while j < len(A) and any(x > T for x in count): 10 | count[A[j]] -= 1 11 | j += 1 12 | if all(x <= T for x in count): 13 | ans = min(ans, j - i) 14 | count[A[i]] += 1 15 | return ans 16 | 17 | N = int(raw_input()) 18 | A = [] 19 | S = raw_input() 20 | for c in S: 21 | if c == 'A': A.append(0) 22 | elif c == 'C': A.append(1) 23 | elif c == 'T': A.append(2) 24 | else: A.append(3) 25 | 26 | 27 | print solve(N, A) 28 | -------------------------------------------------------------------------------- /Contests/College Contests/CodeWhizz 1.0/AlgoFlux Online Qualifier/prob1.py: -------------------------------------------------------------------------------- 1 | # cook your code here 2 | a = [] 3 | n = 10**6+10**5 4 | prime = [True for i in range(n+1)] 5 | p = 2 6 | while (p * p <= n): 7 | # If prime[p] is not changed, then it is a prime 8 | if (prime[p] == True): 9 | 10 | # Update all multiples of p 11 | for i in range(p * 2, n+1, p): 12 | prime[i] = False 13 | p += 1 14 | # Print all prime numbers 15 | for p in range(2, n): 16 | if prime[p]: 17 | a.append(p+1) 18 | # print(a) 19 | 20 | for kohli in range(int(raw_input())): 21 | n = int(raw_input()) 22 | # print(a) 23 | print(a[n]*-1) 24 | -------------------------------------------------------------------------------- /Contests/College Contests/CodeWhizz 1.0/AlgoFlux Online Qualifier/prob2.py: -------------------------------------------------------------------------------- 1 | for _ in range(input()): 2 | arr=list(raw_input()) 3 | string='' 4 | for i in arr: 5 | if ord('A')<=ord(i)<=ord('D'): 6 | string+='A' 7 | elif ord('E')<=ord(i)<=ord('H'): 8 | string+='H' 9 | elif ord('I')<=ord(i)<=ord('K'): 10 | string+='I' 11 | elif ord('L')<=ord(i)<=ord('N'): 12 | string+="M" 13 | elif ord("O")<=ord(i)<=ord("Q"): 14 | string+="O" 15 | elif ord("R")<=ord(i)<=ord("T"): 16 | string+="T" 17 | elif i=="U" or i=="V" or i=="W" or i=="X" or i=="Y": 18 | string+=i 19 | elif i=="Z": 20 | string+="A" 21 | print string 22 | -------------------------------------------------------------------------------- /Contests/College Contests/CodeWhizz 1.0/AlgoFlux Online Qualifier/prob3.py: -------------------------------------------------------------------------------- 1 | for kohli in range(input()): 2 | a = raw_input() 3 | b = raw_input() 4 | n = len(a) 5 | m = len(b) 6 | t = m//n 7 | # print "aage baddd " 8 | # print a,b,n,m 9 | # print "aage baddd ",t 10 | for i in range(t): 11 | if(b.find(a)!=-1): 12 | b = b.replace(a,'') 13 | # print b 14 | # print b 15 | if(b==''): 16 | print "YES" 17 | else: 18 | print "NO" 19 | 20 | ''' 21 | 22 | 4 23 | ab 24 | abab 25 | ab 26 | aabb 27 | ab 28 | aba 29 | abc 30 | aabcbcabc 31 | 32 | ''' 33 | -------------------------------------------------------------------------------- /Contests/College Contests/CodeWhizz 1.0/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/College Contests/CodeWhizz 1.0/a.out -------------------------------------------------------------------------------- /Contests/College Contests/CodeWhizz 1.0/bitpair.py: -------------------------------------------------------------------------------- 1 | for z in range(int(input())): 2 | s=0 3 | n = int(input()) 4 | bi=bin(n)[2:] 5 | length=len(bi) 6 | for i in range(length): 7 | for j in range(1,length): 8 | if iint(math.log(n+1,2)) else 0)) 5 | # print(int(math.log(n+1,2))) 6 | # print(math.log(n+1,2)) 7 | -------------------------------------------------------------------------------- /Contests/College Contests/Codemania/prob2.py: -------------------------------------------------------------------------------- 1 | for nitish in range(int(raw_input())): 2 | n=int(raw_input()) 3 | if(n==1): 4 | print(1) 5 | continue 6 | mod=10**9+7 7 | 8 | # x=((n*(n+1)*(2*n+1))//6)%mod 9 | # y=((n*n+1-2*n)%mod)*(n//2) 10 | # y=y%mod 11 | # xx=n//2-1 12 | # c=(((xx*(xx+1))%mod*(2-2*n))%mod+(xx*(xx+1)*(xx*2+1)*4)//6)%mod 13 | print(((((n*(n+1))//2)%mod)*n)%mod) 14 | 15 | 16 | # print((x+y+c)%(mod)) 17 | -------------------------------------------------------------------------------- /Contests/College Contests/Codemania/prob4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int mod=1e9+7; 5 | int po(int x,int k) 6 | { 7 | int sum=1; 8 | while(k) 9 | { 10 | if(k&1) 11 | { 12 | sum=1LL*sum*x%mod; 13 | } 14 | x=1LL*x*x%mod; 15 | k>>=1; 16 | } 17 | return sum; 18 | } 19 | int main() 20 | { 21 | int t; 22 | scanf("%d",&t); 23 | while(t--) 24 | { 25 | int n,m; 26 | scanf("%d%d",&n,&m); 27 | printf("%d\n",1LL*po(n+1,m-1)*(n-m+1)%mod*po (2,m)%mod); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Contests/College Contests/Codemania/prob7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/College Contests/Codemania/prob7.cpp -------------------------------------------------------------------------------- /Contests/College Contests/Coders' Legacy/Coder's Legacy 2018/p1.py: -------------------------------------------------------------------------------- 1 | t=int(input()) 2 | while t: 3 | s1=input() 4 | a=[] 5 | s=set(s1) 6 | if len(s)>=3: 7 | for i in s: 8 | a.append(s1.count(i)) 9 | a=sorted(a) 10 | l=len(a) 11 | f=0 12 | for i in range(2,l): 13 | if a[i]==a[i-1]+a[i-2]: 14 | f=1 15 | break 16 | if f==1: 17 | print("Dynamic") 18 | else: 19 | print("Not") 20 | else: 21 | print("Dynamic") 22 | t=t-1 23 | -------------------------------------------------------------------------------- /Contests/College Contests/Coders' Legacy/Coder's Legacy 2018/p2.py: -------------------------------------------------------------------------------- 1 | 2 | from math import floor 3 | from fractions import Fraction as F 4 | t = int(input()) 5 | 6 | for case in range(t): 7 | p, q, N = map(int, input().split()) 8 | speed = abs(1 - F(p, q)) 9 | time = F(1, 2) / speed * (N-1) 10 | print(floor(time+1)) 11 | -------------------------------------------------------------------------------- /Contests/College Contests/Cook-a-Code/p1.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | 14 | for __ in range(readInt()): 15 | n = readInt() 16 | a = readInts() 17 | r = readInts() 18 | sum = 0 19 | for i in range(n): 20 | sum += a[i]*pow(r[i],3) 21 | print(sum) 22 | 23 | ''' 24 | Input: 25 | 1 26 | 3 27 | 2 2 2 28 | 3 1 2 29 | 30 | Output: 31 | 72 32 | ''' 33 | -------------------------------------------------------------------------------- /Contests/College Contests/Cook-a-Code/p2.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | 14 | for __ in range(readInt()): 15 | a,b = readInts() 16 | arr = readInts() 17 | brr = readInts() 18 | win = 0 if(a>b) else: 1 19 | print(winner) 20 | c= [0 ,0] 21 | for i in range(len(arr)): 22 | if(arr[i]&1): 23 | c[0]+=1 24 | for i in range(len(brr)): 25 | if(brr[i]&1): 26 | c[1]+=1 27 | print(c[0],c[1]) 28 | if(c[win]>=c[(win+1)%2]): 29 | print("alan") 30 | else: 31 | print("chef") 32 | 33 | 34 | 35 | ''' 36 | Input: 37 | 1 38 | 3 2 39 | 1 2 3 8 9 10 11 4 5 6 7 40 | 21 31 43 52 11 36 22 11 23 17 8 41 | Output: 42 | chef 43 | ''' 44 | -------------------------------------------------------------------------------- /Contests/College Contests/DEMENTIA /DEMENTIA 16/p4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int N=5050; 3 | int n,m,w[N],c[N],d[N],a; 4 | char g[N][N]; 5 | int main(){ 6 | scanf("%d%d",&n,&m); 7 | for(int i=0;i0;)if((d[j]=d[j+1]+c[j])*j>a)a=d[j]*j; 23 | } 24 | printf("%d\n",a); 25 | } 26 | -------------------------------------------------------------------------------- /Contests/College Contests/DEMENTIA /Dementia 2014/p2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define s(n) scanf("%d",&n) 3 | int main() 4 | { 5 | int n,m,i,j,ans,t,px,py,qx,qy; 6 | s(n); 7 | s(m); 8 | int a[n+1][m+1],sum[n+1][m+1]; 9 | for (i=1;i<=n;i++) 10 | for (j=1;j<=m;j++) 11 | s(a[i][j]); 12 | sum[0][0] = 0; 13 | for (i=1;i<=n;i++) 14 | sum[i][0] = 0; 15 | for (i=1;i<=m;i++) 16 | sum[0][i] = 0; 17 | for (i=1;i<=n;i++) 18 | for (j=1;j<=m;j++) 19 | sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + a[i][j]; 20 | s(t); 21 | while(t--) 22 | { 23 | s(px); 24 | s(py); 25 | s(qx); 26 | s(qy); 27 | ans = sum[qx][qy] - sum[qx][py-1] - sum[px-1][qy] + sum[px-1][py-1]; 28 | // ans = sum[qx][qy] - sum[px-1][py-1]; 29 | 30 | printf("%d\n",ans); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Contests/College Contests/DEMENTIA /Dementia 2014/p6.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | scanf("%d",&t); 8 | while(t--) 9 | { 10 | long long n,ans=0; 11 | scanf("%lld",&n); 12 | while(n>0) 13 | { 14 | ans^=n; // gray code to binary 15 | n>>=1; 16 | // std::cout << ans << "ans"<< n << "n"<<" "; 17 | } 18 | printf("%lld\n",ans); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Contests/College Contests/DEMENTIA /Dementia 2015/p4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define MAXN 111 6 | #define MOD 100003 7 | #define LL long long int 8 | 9 | LL dp[MAXN][MAXN] = {0}; 10 | LL a, b, c, i, j; 11 | 12 | inline void Solve(){ 13 | cin>>a>>b>>c; 14 | memset(dp, 0, sizeof dp); 15 | dp[0][0] = 1; 16 | for(int i=0;i=b) 21 | { 22 | dp[i+1][j] += dp[i][j] * (j - b); 23 | dp[i + 1][j] %= MOD; 24 | dp[i+1][j+1] += dp[i][j] * (a - j); 25 | dp[i + 1][j + 1] %= MOD; 26 | } 27 | else{ 28 | dp[i+1][j+1] += dp[i][j] * (a - j); 29 | dp[i + 1][j + 1] %= MOD; 30 | } 31 | } 32 | } 33 | cout<0 else 0)) 4 | -------------------------------------------------------------------------------- /Contests/College Contests/Encipher/prob2.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/College Contests/Encipher/prob2.py -------------------------------------------------------------------------------- /Contests/College Contests/GST February SRM/prob1.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().split())) 4 | def readInt(): 5 | return int(input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStr(): 9 | return raw_input() 10 | 11 | for __ in range(readInt()): 12 | arr = readInts() 13 | f=0 14 | for i in range(1,len(arr)): 15 | if(arr[i]%10!=7): 16 | print arr[i], 17 | f=1 18 | elif(f==0 and i==len(arr)-1): 19 | print 20 | 21 | 22 | 23 | 24 | ''' 25 | Input: 26 | 2 27 | 4 7 17 27 37 28 | 7 21 67 98 34 17 23 16 29 | 30 | Output: 31 | 32 | 21 98 34 23 16 33 | 34 | ''' 35 | -------------------------------------------------------------------------------- /Contests/College Contests/GST February SRM/prob2.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().split())) 4 | def readInt(): 5 | return int(input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStr(): 9 | return raw_input() 10 | 11 | def knapSack(W, wt, val, n): 12 | K = [[0 for x in range(W+1)] for x in range(n+1)] 13 | 14 | # Build table K[][] in bottom up manner 15 | for i in range(n+1): 16 | for w in range(W+1): 17 | if i==0 or w==0: 18 | K[i][w] = 0 19 | elif wt[i-1] <= w: 20 | K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]) 21 | else: 22 | K[i][w] = K[i-1][w] 23 | 24 | return K[n][W] 25 | 26 | for __ in range(readInt()): 27 | n,m = readInts() 28 | p = readInts() 29 | w = readInts() 30 | print(knapSack(m,w,p,n)) 31 | 32 | 33 | ''' 34 | 35 | Input: 36 | 1 37 | 4 7 38 | 1 4 5 7 39 | 1 3 4 5 40 | 41 | Output: 42 | 9 43 | 44 | ''' 45 | -------------------------------------------------------------------------------- /Contests/College Contests/GST January SRM/problem1.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | r,a,b,c,d = map(int,raw_input().split()) 3 | if(r==a or r==b or r==c or r==d): 4 | print("YES") 5 | else : 6 | print("NO") 7 | -------------------------------------------------------------------------------- /Contests/College Contests/GST January SRM/problem2.py: -------------------------------------------------------------------------------- 1 | for kandu in xrange(int(raw_input())): 2 | sum = 0 3 | n = int(input()) 4 | for i in range(n): 5 | a = map(int, raw_input().split()) 6 | a = sorted(a) 7 | sum+=abs(a[n-1]-a[0]) 8 | print(sum) 9 | -------------------------------------------------------------------------------- /Contests/College Contests/Hacktivate/p1.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStr(): 9 | return raw_input() 10 | 11 | for __ in range(readInt()): 12 | n,k= readInts() 13 | arr = readInts() 14 | arr = sorted(arr) 15 | diff = k-arr[0] 16 | if(diff>0): 17 | print(k-arr[0]) 18 | else: 19 | print("0") 20 | 21 | 22 | 23 | 24 | 25 | 26 | ''' 27 | Sample Input 0 28 | 29 | 3 30 | 5 3 31 | 1 2 3 4 5 32 | 6 9 33 | 1 1 1 1 1 1 34 | 3 -1 35 | 1 2 4 36 | Sample Output 0 37 | 38 | 2 39 | 8 40 | ''' 41 | -------------------------------------------------------------------------------- /Contests/College Contests/Hacktivate/p3.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | a = readStr() 14 | n = len(a) 15 | c=0 16 | a = list(a) 17 | for i in range(n/2): 18 | if(a[i]==a[n-i-1]): 19 | continue 20 | elif(a[i]x: 8 | x=a[i+1]-a[i] 9 | x=x/2 10 | x=max(x,a[0],n-1-a[m-1]) 11 | print x 12 | -------------------------------------------------------------------------------- /Contests/College Contests/KJSCE/KJSCE Codecell Practice Round 2/p2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int a[1002]; 4 | int main() { 5 | for(int i=0;i<1002;i++) 6 | a[i]=i*i; 7 | int t; 8 | cin>>t; 9 | while(t--){ 10 | int n; 11 | cin>>n; 12 | int ans=0; 13 | for(int i=n;i>=2;i--){ 14 | int l = 0; 15 | int r = i-1; 16 | while (l < r){ 17 | if (a[l] + a[r] == a[i]) 18 | ans=max(ans,l+r+i); 19 | (a[l] + a[r] < a[i])? l++: r--; 20 | } 21 | } 22 | cout< 2 | using namespace std; 3 | #define MOD (1000000007LL) 4 | typedef long long ll; 5 | 6 | ll n, t, h; 7 | 8 | int main(){ 9 | cin>>n>>t>>h; 10 | h += n*t*15; 11 | // cout<= h) ans = min(ans, cost); 23 | } 24 | cout<= h) ans = min(ans, cost); 32 | } 33 | cout< 2 | using namespace std; 3 | int main(){ 4 | 5 | multiset< int > s; 6 | multiset< int >::iterator it; 7 | 8 | int T , N; 9 | cin >> T >> N; 10 | for(int i = 1 ; i <= N ;++i ){ 11 | int x; 12 | cin >> x; 13 | s.insert(x); 14 | } 15 | while(T--){ 16 | int ch , l; 17 | cin >> ch >> l; 18 | if(ch == 1) 19 | s.insert(l); 20 | else 21 | if(ch==2) 22 | s.erase(l); 23 | else { 24 | if(s.size() < 2 ) 25 | cout << "-1" << endl; 26 | else 27 | { 28 | it = s.end(); 29 | it--; 30 | int A = *it; 31 | --it; 32 | int B = *it; 33 | cout << (A+B) << endl; 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Contests/College Contests/KJSCE/KJSCE Coding Challenge 2017/p2.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | if n == 0: 3 | return 1 4 | else: 5 | return n * factorial(n-1) 6 | 7 | for _ in range(int(raw_input())): 8 | final = 0 9 | s = raw_input() 10 | for i in range(0,len(s)): 11 | count = 0 12 | for j in range(i+1,len(s)): 13 | if (s[j]>s[i]): 14 | count = count+1 15 | final = final + count*factorial(len(s)-i-1) 16 | print final 17 | -------------------------------------------------------------------------------- /Contests/College Contests/Keteki/Keteki Quick Match 10/p1.py: -------------------------------------------------------------------------------- 1 | s=str(input()) 2 | c,d=0,0 3 | for i in range(0,len(s)): 4 | if(s[i]=='*'): 5 | if(i%2==0): 6 | c=c+1 7 | else: 8 | d=d+1 9 | else: 10 | pass 11 | print(pow(21,c)*pow(5,d)) 12 | 13 | -------------------------------------------------------------------------------- /Contests/College Contests/Keteki/Keteki Quick Match 10/p2.cpp: -------------------------------------------------------------------------------- 1 | /*2018-06-17-10.01.52*/ 2 | 3 | #include 4 | #define ll long long int 5 | #define N 100010 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n; 11 | scanf("%d", &n); 12 | int mat[n][n]; 13 | for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d", &mat[i][j]); 14 | int maxi = 1; 15 | for(int i = 0; i < n; i++) 16 | { 17 | int cnt = 0; 18 | for(int j = 0; j < n; j++) if(mat[i][j]) cnt++; 19 | maxi = max(maxi, cnt + 1); 20 | } 21 | printf("%d\n", maxi); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Contests/College Contests/Keteki/Keteki Quick Match 10/p2.py: -------------------------------------------------------------------------------- 1 | n = int(raw_input()()) 2 | arr= [] 3 | c=0 4 | for i in range(n): 5 | row = [int(i) for i in raw_input()().split(' ')] 6 | # arr.append(row) 7 | c = max(c, row.count(1)) 8 | print(c+1) 9 | -------------------------------------------------------------------------------- /Contests/College Contests/Keteki/Keteki Quick Match 6.5/prob1.py: -------------------------------------------------------------------------------- 1 | for __ in range(int(input())): 2 | k = int(raw_input()) 3 | p = list() 4 | ne= list() 5 | n1 = list() 6 | for j in range(k): 7 | n1 = raw_input().split() 8 | s1 = raw_input() 9 | if s1=="yes": 10 | p.extend(n1) 11 | else: 12 | ne.extend(n1) 13 | # print(p) 14 | # print(ne) 15 | # 16 | # print(set(p)) 17 | # print(set(ne)) 18 | # print(set(p)-set(ne)) 19 | 20 | print(len(set(p)-set(ne))) 21 | # print(p) 22 | # print(ne) 23 | -------------------------------------------------------------------------------- /Contests/College Contests/Keteki/Keteki Quick Match-6/prob1.py: -------------------------------------------------------------------------------- 1 | for kohli in range(input()): 2 | #x = map(int,raw_input().split()) 3 | x = int(input()) 4 | print((2*pow(x,3)+x)/3) 5 | -------------------------------------------------------------------------------- /Contests/College Contests/Keteki/Keteki Quick Match-6/prob2.py: -------------------------------------------------------------------------------- 1 | for kohli in range(int(input())): 2 | n,k = map(int,raw_input().split()) 3 | a = list(map(int,raw_input().split())) 4 | t = 1e10 5 | for i in range(len(a)-k+1): 6 | t = min(t, max(a[i:i+k])) 7 | print(t) 8 | -------------------------------------------------------------------------------- /Contests/College Contests/LOC/ LoC July 2018/p4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | vector adj[1000006]; 5 | ll a[1000006]; 6 | ll dp[1000006]; 7 | void dfs(ll s,ll p) 8 | { 9 | dp[s]=dp[p]^a[s]; 10 | for(ll i=0;i>n; 25 | for(ll i=0;i>x>>y; 28 | adj[x].push_back(y); 29 | adj[y].push_back(x); 30 | } 31 | for(ll i=1;i<=n;i++) 32 | { 33 | cin>>a[i]; 34 | } 35 | dfs(1,0); 36 | cin>>q; 37 | while(q--) 38 | { 39 | cin>>x>>y; 40 | cout<<(dp[x]^dp[y])<<"\n"; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Contests/College Contests/LOC/LoC June 2018/p2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define ll long long int 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | ll t;cin>>t; 11 | while(t--){ 12 | ll A,B;cin>>A>>B; 13 | ll b=2*(A+B); 14 | ll c=A*B; 15 | double X1=(b+(double)sqrt((b*b)-12*c))/6; 16 | double X2=(b-(double)sqrt((b*b)-12*c))/6; 17 | ll x1=(ll)round(X1); 18 | ll x2=(ll)round(X2); 19 | if(x1maxi): 21 | maxi = sumi 22 | if(t>100000): 23 | break 24 | # sumi = sum(arr[]) 25 | print maxi/n 26 | 27 | ''' 28 | input 29 | 1 30 | 3 7 31 | 2 7 3 4 4 5 2 32 | output: 33 | 6 34 | ''' 35 | -------------------------------------------------------------------------------- /Contests/College Contests/NITD Code Golf/p2.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | def readInt(): 3 | return int(raw_input()) 4 | def readStr(): 5 | return raw_input() 6 | 7 | arr =[] 8 | t =readInt() 9 | for __ in range(t): 10 | s = readStr() 11 | arr.append(s) 12 | maxi =0 13 | thelist = Counter(arr) 14 | for k in arr: 15 | if(thelist[k]>=maxi): 16 | maxi = thelist[k] 17 | ans = k 18 | print ans 19 | -------------------------------------------------------------------------------- /Contests/College Contests/NITD Code Golf/p3.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | # def printDivisors(n) : 7 | # i = 1 8 | # arr =[] 9 | # while i <= n : 10 | # if (n % i==0) : 11 | # arr.append(i) 12 | # 13 | # i = i + 1 14 | # return len(arr) 15 | 16 | def printDivisors(n) : 17 | i = 1 18 | arr =[] 19 | while i <= math.sqrt(n): 20 | if (n % i == 0) : 21 | if (n / i == i) : 22 | arr.append(i) 23 | else : 24 | arr.append(i) 25 | arr.append(n/i) 26 | i = i + 1 27 | return len(arr) 28 | 29 | for __ in range(readInt()): 30 | n = readInt() 31 | for i in range(100000): 32 | num = n+i+1 33 | if(printDivisors(num)==2): 34 | print num 35 | break 36 | -------------------------------------------------------------------------------- /Contests/College Contests/Overnite Coding/prob1.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().split())) 4 | def readInt(): 5 | return int(input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStr(): 9 | return raw_input() 10 | 11 | for __ in range(readInt()): 12 | n,m = readInts() 13 | m = m*(m+1)/2 14 | # print(n,m) 15 | if(n>=m): 16 | print("YES") 17 | else: 18 | print("NO") 19 | 20 | 21 | ''' 22 | Input: 23 | 1 24 | 5 2 25 | 26 | Output: 27 | YES 28 | ''' 29 | -------------------------------------------------------------------------------- /Contests/College Contests/Overnite Coding/prob2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | ll t,n,phi[1000006],val[1000006],pr[1000006],frq[2000006]; 5 | int main() 6 | { 7 | ios::sync_with_stdio(0); 8 | cin.tie(0); 9 | ll i,j,k; 10 | for(i=2;i<=1000000;i++) 11 | { 12 | phi[i]=i; 13 | } 14 | for(i=2;i<=1000000;i++) 15 | { 16 | if(phi[i]==i) 17 | { 18 | phi[i]=i-1; 19 | for(j=2*i;j<=1000000;j+=i) 20 | { 21 | phi[j]-=(phi[j]/i); 22 | } 23 | } 24 | } 25 | for(i=3;i<=1000000;i++) 26 | { 27 | val[i]=phi[i]; 28 | if(i%2==0) 29 | val[i]+=phi[i/2]; 30 | } 31 | ll ans=0; 32 | for(i=1;i<=1000000;i++) 33 | { 34 | frq[val[i]]++; 35 | pr[i]=pr[i-1]+(frq[val[i]]-1); 36 | } 37 | cin>>t; 38 | while(t--) 39 | { 40 | cin>>n; 41 | cout<= 0: 14 | choose.add(word[i - 1]) 15 | if i + 1 < ls: 16 | choose.add(word[i + 1]) 17 | res *= len(choose) 18 | return res % 1000000007 19 | 20 | if __name__ == '__main__': 21 | # begin 22 | open_file = open("../test.in", 'rU') 23 | sys.stdout = open("../test.out", 'w') 24 | input_parameters = open_file.read().split('\n') 25 | num_case = int(input_parameters[0]) 26 | pos = 1 27 | for index in range(num_case): 28 | word = input_parameters[pos] 29 | pos += 1 30 | print "Case #%d: %d" % (index + 1, Monster_Path(word)) -------------------------------------------------------------------------------- /Contests/KICKSTART/Google_APAC-master/2017_C/D_Soldiers.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python 2 | # coding=utf-8 3 | import sys 4 | 5 | def Soldiers(word): 6 | ls = len(word) 7 | if ls == 1: 8 | return 1 9 | res = 1 10 | for i in range(ls): 11 | # check the distinct characters in i - 1, i, i + 1 12 | choose = set([word[i]]) 13 | if i - 1 >= 0: 14 | choose.add(word[i - 1]) 15 | if i + 1 < ls: 16 | choose.add(word[i + 1]) 17 | res *= len(choose) 18 | return res % 1000000007 19 | 20 | if __name__ == '__main__': 21 | # begin 22 | open_file = open("../test.in", 'rU') 23 | sys.stdout = open("../test.out", 'w') 24 | input_parameters = open_file.read().split('\n') 25 | num_case = int(input_parameters[0]) 26 | pos = 1 27 | for index in range(num_case): 28 | word = input_parameters[pos] 29 | pos += 1 30 | print "Case #%d: %d" % (index + 1, Lazy_Spelling_Bee(word)) -------------------------------------------------------------------------------- /Contests/KICKSTART/Google_APAC-master/2017_Practice/A_Lazy_Spelling_Bee.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python 2 | # coding=utf-8 3 | import sys 4 | 5 | def Lazy_Spelling_Bee(word): 6 | ls = len(word) 7 | if ls == 1: 8 | return 1 9 | res = 1 10 | for i in range(ls): 11 | # check the distinct characters in i - 1, i, i + 1 12 | choose = set([word[i]]) 13 | if i - 1 >= 0: 14 | choose.add(word[i - 1]) 15 | if i + 1 < ls: 16 | choose.add(word[i + 1]) 17 | res *= len(choose) 18 | return res % 1000000007 19 | 20 | if __name__ == '__main__': 21 | # begin 22 | open_file = open("../test.in", 'rU') 23 | sys.stdout = open("../test.out", 'w') 24 | input_parameters = open_file.read().split('\n') 25 | num_case = int(input_parameters[0]) 26 | pos = 1 27 | for index in range(num_case): 28 | word = input_parameters[pos] 29 | pos += 1 30 | print "Case #%d: %d" % (index + 1, Lazy_Spelling_Bee(word)) -------------------------------------------------------------------------------- /Contests/KICKSTART/Google_APAC-master/2017_Practice/B_Robot_Rock_Band.py: -------------------------------------------------------------------------------- 1 | # !/usr/bin/env python 2 | # coding=utf-8 3 | import sys 4 | 5 | def robot_rock_band(n, k, band): 6 | 7 | return 8 | 9 | if __name__ == '__main__': 10 | # begin 11 | open_file = open("../test.in", 'rU') 12 | sys.stdout = open("../test.out", 'w') 13 | input_parameters = open_file.read() 14 | num_case = int(input_parameters[0]).split('\n') 15 | pos = 1 16 | for index in range(num_case): 17 | n, k = map(int, input_parameters[pos].split(' ')) 18 | pos += 1 19 | band = [] 20 | for _ in range(4): 21 | for i in range(n): 22 | band.append(map(int, input_parameters[pos].split(' ')) 23 | pos += 1 24 | print "Case #%d: %d" % (index + 1, robot_rock_band(n, k, band)) -------------------------------------------------------------------------------- /Contests/Others/ C Cubed/prob1.py: -------------------------------------------------------------------------------- 1 | import math 2 | import statistics 3 | import itertools 4 | 5 | def findsubsets(S,m): 6 | return set(itertools.combinations(S, m)) 7 | def readInts(): 8 | return list(map(int, input().split())) 9 | def readInt(): 10 | return int(input()) 11 | 12 | n = readInt() 13 | a = [] 14 | 15 | for __ in range(n): 16 | inp = readInt() 17 | if(inp>=0): 18 | a.append(inp) 19 | n = len(a) 20 | # print(a) 21 | 22 | summ = 0 23 | 24 | for i in range(1,len(a)+1): 25 | s = [] 26 | if(i&1): 27 | s = findsubsets(a,i) 28 | s = list(s) 29 | for j in range(len(s)): 30 | med = statistics.median(s[j]) 31 | summ+=med 32 | print(summ) 33 | 34 | 35 | ''' 36 | 4 37 | 0 38 | 1 39 | 3 40 | 2 41 | 42 | 9 43 | 6 44 | -9 45 | 7 46 | 2 47 | -2 48 | 3 49 | 2 50 | 0 51 | 1 52 | 53 | 4 54 | 1 55 | 2 56 | 2 57 | -2 58 | ''' 59 | -------------------------------------------------------------------------------- /Contests/Others/ CODE BATTLELive/p6.cpp: -------------------------------------------------------------------------------- 1 | // An efficient approach based implementation 2 | // to find if a number can be written as sum 3 | // of two squares. 4 | #include 5 | using namespace std; 6 | 7 | // function to check if there exist two 8 | // numbers sum of whose squares is n. 9 | bool sumSquare(int n) 10 | { 11 | unordered_map s; 12 | for (int i = 0; i * i <= n; ++i) { 13 | 14 | // store square value in hashmap 15 | s[i * i] = 1; 16 | if (s.find(n - i * i) != s.end()) { 17 | // cout << sqrt(n - i * i) << "^2 + " 18 | // << i << "^2" << endl; 19 | return true; 20 | } 21 | } 22 | return false; 23 | } 24 | 25 | // driver Program 26 | int main() 27 | { 28 | int n; 29 | cin>>n; 30 | 31 | if (sumSquare(n)) 32 | cout << "YES"; 33 | else 34 | cout << "NO"; 35 | } 36 | -------------------------------------------------------------------------------- /Contests/Others/ Code Conquest/prob1.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/Contests/Others/ Code Conquest/prob1.py -------------------------------------------------------------------------------- /Contests/Others/ International Coding League (Rated)/p4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define pb push_back 4 | #define endl '\n' 5 | #define pii pair 6 | #define vi vector 7 | #define all(a) (a).begin(),(a).end() 8 | #define F first 9 | #define S second 10 | #define sz(x) (ll int)x.size() 11 | #define hell 1000000007 12 | #define rep(i,a,b) for(ll int i=a;i>t; 23 | while(t--) 24 | { 25 | ll n,k; 26 | cin>> 27 | 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | /* 34 | 35 | Sample Input 36 | 37 | 5 6 38 | 1 3 4 39 | 1 2 1 40 | 1 1 4 41 | 2 4 2 3 42 | 2 4 2 1 43 | 2 3 5 3 44 | 45 | Sample Output 46 | 47 | 4 48 | 1 49 | 3 50 | -1 51 | 4 52 | 5 53 | 54 | 55 | */ 56 | -------------------------------------------------------------------------------- /Contests/Others/BUG4EVER/p6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e2 + 5; 4 | int n , e , w , u , v; 5 | int a[N]; 6 | bool seen [N]; 7 | vector < int > X[N]; 8 | void solve(int u) { 9 | if(seen[u] | a[u] < w) { 10 | return; 11 | } 12 | seen[u] = true; 13 | for(int i = 0; i < (int) X[u].size(); ++i) { 14 | solve(X[u][i]); 15 | } 16 | } 17 | 18 | int main() { 19 | int tt , t = 0; 20 | scanf("%d" , &tt); 21 | while(tt--) { 22 | memset(seen , false , sizeof seen); 23 | for(int i = 0; i < N; ++i) X[i].clear(); 24 | scanf("%d %d %d" , &n, &e, &w); 25 | for(int i = 1; i <= n; ++i) { 26 | scanf("%d" , a + i); 27 | } 28 | for(; e; --e) { 29 | scanf("%d %d" , &u , &v); 30 | X[u].push_back(v); 31 | X[v].push_back(u); 32 | } 33 | solve(1); 34 | printf("Case #%d: " , ++t); 35 | printf(seen[n] ? "YES\n" : "NO\n"); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Contests/Others/Bytecode 2018/prob1.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStr(): 9 | return raw_input() 10 | 11 | for __ in range(readInt()): 12 | arr = readInts() 13 | -------------------------------------------------------------------------------- /Contests/Others/COUT-2K18/p1.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | 14 | for __ in range(readInt()): 15 | a = readStr() 16 | n = len(a) 17 | vowels,cons =0,0 18 | for i in range(n): 19 | if(a[i] in 'aeiou'): 20 | vowels+=1 21 | elif(a[i] in 'bcdfghjklmnpqrstvwxyz'): 22 | cons+=1 23 | print vowels,cons 24 | 25 | # a = 'a' 26 | # vowels ='' 27 | # cons = '' 28 | # for i in range(26): 29 | # if(chr(ord(a)+i) in 'aeiou'): 30 | # vowels += chr(ord(a)+i) 31 | # else: 32 | # cons += chr(ord(a)+i) 33 | # print(vowels) 34 | # print(cons) 35 | 36 | ''' 37 | Input 38 | 39 | 3 40 | abcde ewa 41 | aioue 42 | Aefdd 43 | 44 | Output 45 | 46 | 2 3 47 | 5 0 48 | 49 | ''' 50 | -------------------------------------------------------------------------------- /Contests/Others/COUT-2K18/p2.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | 14 | for __ in range(readInt()): 15 | a = readStr() 16 | set1 = set() 17 | n = len(a) 18 | for i in range(n): 19 | if(a[i] in 'aeiou'): 20 | set1.add(a[i]) 21 | sort = ''.join(sorted(set1)) 22 | # print(set1) 23 | # print(sort) 24 | if(sort=='aeiou'): 25 | print(a) 26 | 27 | 28 | 29 | ''' 30 | Input: 31 | 3 32 | functionality 33 | hamdard 34 | eukaryotic 35 | 36 | Output: 37 | eukaryotic 38 | ''' 39 | -------------------------------------------------------------------------------- /Contests/Others/COUT-2K18/p4.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | 14 | for __ in range(readInt()): 15 | n = readInt() 16 | a = readStr() 17 | i=0 18 | count =0 19 | print(a.count("010")) 20 | 21 | 22 | 23 | ''' 24 | Input: 25 | 26 | 2 27 | 5 28 | 11010 29 | 3 30 | 101 31 | 32 | Output: 33 | 34 | 1 35 | 0 36 | ''' 37 | -------------------------------------------------------------------------------- /Contests/Others/COUT-2K18/p6.py: -------------------------------------------------------------------------------- 1 | T = int(input()) 2 | for t in range(T): 3 | N = int(input()) 4 | if N % 9 == 2 or N % 9 == 3: 5 | print('Sailaja') 6 | else: 7 | print('Supraja') 8 | 9 | ''' 10 | Input: 11 | 5 12 | 13 | Output: 14 | Supraja 15 | 16 | 17 | Sample - 2 18 | 19 | Input: 20 | 3 21 | 22 | Output: 23 | Sailaja 24 | 25 | ''' 26 | -------------------------------------------------------------------------------- /Contests/Others/COUT-2K18/p7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef unsigned long long int ll; 4 | #define fast {ios_base::sync_with_stdio(false);cin.tie(NULL);} 5 | int main() 6 | { 7 | ll t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | ll n,d=0; 12 | cin>>n; 13 | while(n) 14 | { 15 | if(!(n&1)) 16 | d++; 17 | n=n>>1; 18 | } 19 | ll xx=pow(2,d); 20 | cout< 2 | using namespace std; 3 | int sum =0 ; 4 | int fun(int n, int k) 5 | { 6 | // cout<>t; 19 | while(t--) 20 | { 21 | cin>>n>>k; 22 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define mod 1000000007 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL),cout.tie(NULL); 9 | ll i,j,k; 10 | ll a[100007]={0}; 11 | for(i=2;i<=100000;i++) 12 | { 13 | for(j=2*i;j<=100000;j+=i) 14 | a[j]++; 15 | } 16 | ll t; 17 | cin>>t; 18 | while(t--) 19 | { 20 | ll n; 21 | cin>>n; 22 | if(a[n]==0) 23 | cout<<-1; 24 | else 25 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n,k,arr[200202]={0}; 9 | cin>>n>>k; 10 | for(int i =0;i>arr[i]; 12 | } 13 | int sum =0,f=0; 14 | for(int i =0;i=k){ 17 | f=1; 18 | break; 19 | } 20 | } 21 | if(f==1) 22 | cout<<"YES\n"; 23 | else 24 | cout<<"NO\n"; 25 | 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Contests/Others/GST SRM Aug/p3.py: -------------------------------------------------------------------------------- 1 | def readInts(): 2 | return list(map(int, raw_input().strip().split())) 3 | def readInt(): 4 | return int(raw_input()) 5 | 6 | for __ in range(readInt()): 7 | arr,brr = [],[] 8 | n = readInt() 9 | arr = readInts() 10 | m = readInt() 11 | brr =readInts() 12 | a = set(arr)-set(brr) 13 | print n-len(list(a)) 14 | 15 | ''' 16 | Input: 17 | 1 18 | 5 19 | 10 20 30 40 50 20 | 5 21 | 22 30 32 45 50 22 | Output: 23 | 2 24 | ''' 25 | -------------------------------------------------------------------------------- /Contests/Others/Hackon Feb/prob1.py: -------------------------------------------------------------------------------- 1 | def readInts(): 2 | return list(map(int, input().split())) 3 | def readInt(): 4 | return int(input()) 5 | def readIntsindex0(): 6 | return list(map(lambda x: int(x) - 1, input().split())) 7 | 8 | t=input() 9 | for __ in range(t): 10 | n = readInt() 11 | a = n//7 12 | print(7*(a*(a+1)//2)) 13 | -------------------------------------------------------------------------------- /Contests/Others/Hackon Feb/prob2.py: -------------------------------------------------------------------------------- 1 | def readInts(): 2 | return list(map(int, raw_input().split())) 3 | def readInt(): 4 | return int(input()) 5 | def readIntsindex0(): 6 | return list(map(lambda x: int(x) - 1, input().split())) 7 | 8 | a = [] 9 | 10 | def SieveOfEratosthenes(n): 11 | prime = [True for i in range(n+1)] 12 | p = 2 13 | while (p * p <= n): 14 | # If prime[p] is not changed, then it is a prime 15 | if (prime[p] == True): 16 | # Update all multiples of p 17 | for i in range(p * 2, n+1, p): 18 | prime[i] = False 19 | p += 1 20 | # Print all prime numbers 21 | for p in range(2, n): 22 | if prime[p]: 23 | # print p, 24 | a.append(p) 25 | 26 | 27 | t=input() 28 | SieveOfEratosthenes(1000001) 29 | for __ in range(t): 30 | l,r = map(int, raw_input().split()) 31 | count =0 32 | for i in a: 33 | if(i>=l and i<=r): 34 | count+=1 35 | ans = (float(count)/(r-l+1)) 36 | print("{0:.6f}".format(ans)) 37 | # print(count) 38 | -------------------------------------------------------------------------------- /Contests/Others/Hackon Feb/prob3.py: -------------------------------------------------------------------------------- 1 | def readInts(): 2 | return list(map(int, raw_input().split())) 3 | def readInt(): 4 | return int(input()) 5 | def readIntsindex0(): 6 | return list(map(lambda x: int(x) - 1, input().split())) 7 | 8 | t=input() 9 | for __ in range(t): 10 | a,b,c = readInts() 11 | x,y,z = readInts() 12 | root = pow(c+z,2) 13 | line = pow(a-x,2) + pow(b-y,2) 14 | 15 | if(root == line): 16 | print("tangential") 17 | elif(root>line): 18 | print("overlapping") 19 | else: 20 | print("not overlapping") 21 | 22 | 23 | ''' 24 | 25 | Input: 26 | 3 27 | 10 10 3 28 | 10 6 1 29 | 8 8 3 30 | 8 4 2 31 | 7 5 2 32 | 4 9 2 33 | Output: 34 | tangential 35 | overlapping 36 | not overlapping 37 | 38 | ''' 39 | -------------------------------------------------------------------------------- /Contests/Others/Hackon Feb/prob4.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().split())) 4 | def readInt(): 5 | return int(input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | 9 | 10 | import math 11 | 12 | def solve(): 13 | # T=int(input()) 14 | # for y in range(T): 15 | k=int(input()) 16 | if k==1: 17 | print(5) 18 | else: 19 | base=int(math.log(k+1,2)) 20 | diff=(k+1)-(2**base) 21 | val=bin(diff)[2:] # to remove 0b 22 | l=len(val) 23 | # print(val,base,l) 24 | ans = "" 25 | if l!=base: 26 | val='0'*(base-l)+val 27 | # print("an improvement",val) 28 | for i in val: 29 | if i=='0': 30 | ans += "5" 31 | else: 32 | ans += "8" 33 | print(ans) 34 | t = int(input()) 35 | for i in range(t): 36 | solve() 37 | ''' 38 | 39 | Input: 40 | 3 41 | 1 42 | 5 43 | 11 44 | 45 | Output: 46 | 5 47 | 85 48 | 8555 49 | 50 | ''' 51 | -------------------------------------------------------------------------------- /Contests/Others/Hackon Feb/prob5.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().split())) 4 | def readInt(): 5 | return int(input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | 9 | test = readInt() 10 | for _ in range(test): 11 | X1, Y1, R1 = readInts() 12 | X2, Y2, R2 = readInts() 13 | d = math.sqrt(((X2 - X1) ** 2) + ((Y2 - Y1) ** 2)) 14 | if d < abs(R1 - R2): 15 | if R1 > R2: 16 | print 'C1CC2' 17 | else: 18 | print 'C2CC1' 19 | elif d == (R1 - R2): 20 | print 'C2~C1' 21 | else: 22 | print 'NOT SATISFIED' 23 | -------------------------------------------------------------------------------- /Contests/Others/Hackon Feb/prob6.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().split())) 4 | def readInt(): 5 | return int(input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStr(): 9 | return raw_input() 10 | 11 | t = int(raw_input()) 12 | 13 | while t>0: 14 | t-=1 15 | n = int(raw_input()) 16 | a = n*n+2*n+1 17 | b = n*(n+1) # summation n 18 | c = b*(2*n+1) # summation n2 19 | d = b*b # summation n3 20 | e = (2*n+2) 21 | num = (a*b)/2+d/4-(e*c)/6 22 | den = (a*n)+c/6-(e*b)/2 23 | # print(num,den) 24 | print num/den 25 | -------------------------------------------------------------------------------- /Contests/Others/MNM Online Programming Contest/p2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define pb push_back 4 | #define endl '\n' 5 | #define pii pair 6 | #define vi vector 7 | #define all(a) (a).begin(),(a).end() 8 | #define F first 9 | #define S second 10 | #define sz(x) (ll int)x.size() 11 | #define hell 1000000007 12 | #define rep(i,a,b) for(ll int i=a;i>t; 23 | while(t--) 24 | { 25 | ll n,k; 26 | cin>>n>>k; 27 | if(n==0||n==1||n<0) 28 | {cout<<"0"< 2-> 3->\n 7 | 4-> 5-> 6->\n 8 | 7-> 8-> 9->\n 9 | 3 10 | 3 11 | 10-> 11-> 12->\n 12 | 13-> 14-> 15->\n 13 | 16-> 17-> 18->\n 14 | 2 15 | 3 16 | 1-> 2-> 3->\n 17 | 4-> 5-> 6->\n 18 | 4 19 | 3 20 | 7-> 8-> 9->\n 21 | 10-> 11-> 12->\n 22 | 13-> 14-> 15->\n 23 | 16-> 17-> 18->\n 24 | 25 | Output: 26 | 3 27 | 3 28 | 84-> 90-> 96->\n 29 | 201-> 216-> 231->\n 30 | 318-> 342-> 366->\n 31 | The Matrix Multiplication is not possible 32 | 33 | Explanation 34 | The matrix will look like 35 | 84-> 90-> 96->\n 36 | | | | 37 | v v v 38 | 201-> 216-> 231->\n 39 | | | | 40 | v v v 41 | 318-> 342-> 366->\n 42 | | | | 43 | v v v 44 | \n \n \n 45 | 46 | ''' 47 | -------------------------------------------------------------------------------- /Contests/Others/MNM Online Programming Contest/p5.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Input: 3 | 1 4 | 16 5 | 8 6 | 1 7 | 2 8 | 3 9 | 4 10 | 5 11 | 6 12 | 7 13 | 8 14 | 9 15 | 10 16 | 11 17 | 12 18 | 13 19 | 14 20 | 15 21 | 16 22 | 23 | 24 | 1 25 | 10 26 | 8 27 | 1 28 | 2 29 | 3 30 | 4 31 | 5 32 | 6 33 | 7 34 | 8 35 | 9 36 | 10 37 | Output: 38 | 1 3 5 7 9 11 13 16 39 | 1 2 3 5 6 8 9 10 40 | ''' 41 | -------------------------------------------------------------------------------- /Contests/Others/Practice/medium/flip.py: -------------------------------------------------------------------------------- 1 | n,m = map(int,raw_input().split()) 2 | for _ in range(m): 3 | a = map(int,raw_input().split()) 4 | if(a[0]==0): 5 | 6 | -------------------------------------------------------------------------------- /Contests/Others/Practice/medium/nextpalin.py: -------------------------------------------------------------------------------- 1 | t=int(raw_input()) 2 | for x in range(t): 3 | s=raw_input() 4 | l=len(s) 5 | if(s.count('9')==l): 6 | print(str(long(s)+2)) 7 | else: 8 | if(l%2==0): 9 | rev=''.join(reversed(s[:l/2])) 10 | if(rev>s[l/2:]): 11 | print s[:l/2]+rev 12 | else: 13 | revs=str(long(s[:l/2])+1) 14 | print revs+''.join(reversed(revs)) 15 | else:#odd 16 | rev=''.join(reversed(s[:l/2])) 17 | if(rev>s[(l/2)+1:]): 18 | print s[:l/2+1]+rev 19 | else: 20 | d=int(s[l/2])+1 21 | if(d==10): 22 | revs=str(long(s[:l/2])+1) 23 | print revs+'0'+''.join(reversed(revs)) 24 | else: 25 | print s[:l/2]+str(d)+rev 26 | -------------------------------------------------------------------------------- /Contests/Others/Practice/medium/numfact.py: -------------------------------------------------------------------------------- 1 | primes = [2,3,5,7] 2 | for i in range(11,1000): 3 | isPrime = True 4 | for j in primes: 5 | if i%j == 0: 6 | isPrime = False 7 | break 8 | if isPrime: 9 | primes.append(i) 10 | numberOfPrimes = len(primes) 11 | 12 | 13 | for _ in range(input()): 14 | n = input() 15 | a = map(int, raw_input().split()) 16 | d = {} 17 | for i in a: 18 | j = 0 19 | while j=coins[i]: 7 | dp[i][j]=min(dp[i-1][j],1+dp[i][j-coins[i]]) 8 | else: 9 | dp[i][j]=dp[i-1][j] 10 | return dp[-1][-1] 11 | coins=[1,5,6,8] 12 | total=11 13 | print min_coins(coins,total) 14 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Combinations.py: -------------------------------------------------------------------------------- 1 | def combinations(iterable, r): 2 | temp=[] 3 | pool = tuple(iterable) 4 | n = len(pool) 5 | if r > n: 6 | return 7 | indices = range(r) 8 | temp.append( tuple(pool[i] for i in indices)) 9 | while True: 10 | for i in reversed(range(r)): 11 | if indices[i] != i + n - r: 12 | break 13 | else: 14 | return temp 15 | indices[i] += 1 16 | for j in range(i+1, r): 17 | indices[j] = indices[j-1] + 1 18 | temp.append(tuple(pool[i] for i in indices)) 19 | 20 | # combinations(li,k) will print all tuples of length k from the list li 21 | li=[1,2,3,4,5] 22 | print combinations(li,3) 23 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Connected Components using DFS.py: -------------------------------------------------------------------------------- 1 | def connected_components(graph): 2 | seen = set() 3 | def dfs(v): 4 | vs = set([v]) 5 | component=[] 6 | while vs: 7 | v = vs.pop() 8 | seen.add(v) 9 | vs |= set(graph[v]) - seen 10 | component.append(v) 11 | return component 12 | ans=[] 13 | for v in graph: 14 | if v not in seen: 15 | d=dfs(v) 16 | ans.append(d) 17 | return ans 18 | 19 | graph={1: [2, 3], 2: [1, 3], 3: [2, 1], 4: [5], 5: [4, 6], 6: [5, 7], 7: [6]} 20 | n=7 21 | print connected_components(graph) 22 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/DFS.py: -------------------------------------------------------------------------------- 1 | def dfs(graph, start, path=[]): 2 | q=[start] 3 | while q: 4 | v=q.pop(0) 5 | if v not in path: 6 | path=path+[v] 7 | q=graph[v]+q 8 | return path 9 | 10 | graph = {'A':['B','C'],'B':['D','E'],'C':['D','E'],'D':['E'],'E':['A']} 11 | print dfs(graph,'A') 12 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Day on a particular date.py: -------------------------------------------------------------------------------- 1 | import calendar 2 | day = {0:'MONDAY', 1:'TUESDAY', 2:'WEDNESDAY', 3:'THURSDAY', 4:'FRIDAY', 5:'SATURDAY', 6:'SUNDAY'} 3 | m,d,y = map(int,raw_input().split()) 4 | print day[calendar.weekday(y,m,d)] 5 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Factors or divisors of a number.py: -------------------------------------------------------------------------------- 1 | def factors(n): 2 | l1, l2 = [], [] 3 | for i in range(1, int(n ** 0.5) + 1): 4 | q,r = n/i, n%i 5 | if r == 0: 6 | l1.append(i) 7 | l2.append(q) 8 | if l1[-1] == l2[-1]: 9 | l1.pop() 10 | l2.reverse() 11 | return l1 + l2 12 | 13 | n=128 14 | print factors(n) 15 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Fenwick Tree or Binary Indexed Tree.py: -------------------------------------------------------------------------------- 1 | def getsum(BITTree,i): 2 | s = 0 3 | i = i+1 4 | while i > 0: 5 | s += BITTree[i] 6 | i -= i & (-i) 7 | return s 8 | 9 | def updatebit(BITTree , n , i ,v): 10 | i += 1 11 | while i <= n: 12 | BITTree[i] += v 13 | i += i & (-i) 14 | 15 | def construct(arr, n): 16 | BITTree = [0]*(n+1) 17 | for i in range(n): 18 | updatebit(BITTree, n, i, arr[i]) 19 | return BITTree 20 | 21 | freq = [2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9] 22 | BITTree = construct(freq,len(freq)) 23 | print("Sum of elements in arr[0..5] is " + str(getsum(BITTree,5))) 24 | freq[3] += 6 25 | updatebit(BITTree, len(freq), 3, 6) 26 | print("Sum of elements in arr[0..5] is " + str(getsum(BITTree,5))) 27 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Fibonacci Series in logn.py: -------------------------------------------------------------------------------- 1 | def matrix_mul(A, B): 2 | return ([A[0][0] * B[0][0] + A[0][1] * B[1][0], 3 | A[0][0] * B[0][1] + A[0][1] * B[1][1]], 4 | [A[1][0] * B[0][0] + A[1][1] * B[1][0], 5 | A[1][0] * B[0][1] + A[1][1] * B[1][1]]) 6 | 7 | def matrix_exp(A, e): 8 | if not e: 9 | return [[1,0],[0,1]] 10 | elif e % 2: 11 | return matrix_mul(A, matrix_exp(A, e-1)) 12 | else: 13 | sq= matrix_exp(A, e//2) 14 | return matrix_mul(sq, sq) 15 | 16 | def fibo(n): 17 | M = [[1,1],[1,0]] 18 | return matrix_exp(M, n)[0][0] 19 | 20 | print fibo(4) 21 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Fractions in reduced form.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | print Fraction(3,63) 3 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/GCD of n numbers.py: -------------------------------------------------------------------------------- 1 | from fractions import gcd 2 | li=[12,16,48] 3 | print reduce(gcd,li) 4 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Knapsack 01.py: -------------------------------------------------------------------------------- 1 | def knapsack01(weight,value,max_weight): 2 | n=len(value) 3 | dp=[[0 for i in xrange(max_weight+1)] for j in xrange(n+1)] 4 | for i in xrange(1,n+1): 5 | for j in xrange(1,max_weight+1): 6 | if j 1: 22 | childrens = min_set_ancestor(tree_data, childrens) 23 | 24 | print childrens[0] 25 | 26 | '''Test Input: 27 | 28 | 0 29 | / \ 30 | 1 2 31 | / | \ \ 32 | 3 4 5 6 33 | / / \ 34 | 7 8 9 35 | 36 | 10 37 | 0 0 1 1 1 2 3 4 4 38 | 3 39 | 7 8 9 40 | ''' 41 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Maximum of all mods in an array.py: -------------------------------------------------------------------------------- 1 | li=[2,5,7,10,100] 2 | li.sort(reverse=True) 3 | for i in li: 4 | if i!=li[0]: 5 | print i 6 | break 7 | else: 8 | print 0 9 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Maximum of all subarrays of size k (Sliding Window).py: -------------------------------------------------------------------------------- 1 | import heapq 2 | def subarray_max(li,n,k): 3 | ans=[] 4 | heap=[] 5 | for i in xrange(k): 6 | while heap!=[] and li[i]>=li[heap[-1]]: 7 | heap.pop() 8 | heapq.heappush(heap,i) 9 | for i in xrange(k,n): 10 | ans.append(li[heap[0]]) 11 | while heap!=[] and heap[0]<=i-k: 12 | heap.pop(0) 13 | while heap!=[] and li[i]>=li[heap[-1]]: 14 | heap.pop() 15 | heapq.heappush(heap,i) 16 | ans.append(li[heap[0]]) 17 | return ans 18 | n,k=6,3 19 | li=[5,1,4,2,3,9] 20 | print subarray_max(li,n,k) 21 | 22 | 23 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Maximum subarray sum mod m.py: -------------------------------------------------------------------------------- 1 | def max_subarray_sum_mod_m(li,n,m): 2 | temp=0 3 | s=[] 4 | maxx=-1 5 | for i in xrange(n): 6 | temp=(temp+li[i])%m 7 | s.append([temp,i]) 8 | if temp>maxx: 9 | maxx=temp 10 | s.sort() 11 | minn=m+1 12 | for i in xrange(n-1): 13 | if s[i][1]>s[i+1][1]: 14 | minn=min(s[i+1][0]-s[i][0],minn) 15 | return max(maxx,m-minn) 16 | 17 | print max_subarray_sum_mod_m([10,15,23,28,30],5,6) 18 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Minimum Spanning Tree (Prims's).py: -------------------------------------------------------------------------------- 1 | def findMin(key,mstSet,n): 2 | m=float("inf") 3 | for v in xrange(n): 4 | if not mstSet[v] and key[v] 1: 13 | primfac[n]=1 14 | return primfac 15 | 16 | n=input() 17 | print pf(n) 18 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Number of values in a range.py: -------------------------------------------------------------------------------- 1 | from sys import stdin as ip 2 | from bisect import bisect_left as bl 3 | from bisect import bisect_right as br 4 | aa,bb=500,500 5 | n=int(ip.readline()) 6 | li=list(map(int,ip.readline().split())) 7 | q=int(ip.readline()) 8 | li.sort() 9 | for i in xrange(q): 10 | a,b=map(int,ip.readline().split()) 11 | i1= br(li,b) 12 | i2= bl(li,a) 13 | print i1-i2 14 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Prime factors of a number.py: -------------------------------------------------------------------------------- 1 | def pf(n): 2 | primfac = [] 3 | d = 2 4 | while d*d <= n: 5 | while (n % d) == 0: 6 | primfac.append(d) 7 | n //= d 8 | d += 1 9 | if n > 1: 10 | primfac.append(n) 11 | return primfac 12 | 13 | n=input() 14 | print pf(n) 15 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Prime numbers till n.py: -------------------------------------------------------------------------------- 1 | def primes(n): 2 | correction = (n%6>1) 3 | n = {0:n,1:n-1,2:n+4,3:n+3,4:n+2,5:n+1}[n%6] 4 | sieve = [True] * (n//3) 5 | sieve[0] = False 6 | for i in range(int(n**0.5)//3+1): 7 | if sieve[i]: 8 | k=3*i+1|1 9 | sieve[((k*k)//3)::2*k]=[False]*((n//6-(k*k)//6-1)//k+1) 10 | sieve[(k*k+4*k-2*k*(i&1))//3::2*k]=[False]*((n//6-(k*k+4*k-2*k*(i&1))//6-1)//k+1) 11 | return [2,3] + [3*i+1|1 for i in range(1,n//3-correction) if sieve[i]] 12 | 13 | 14 | li=primes((10**3)+1) 15 | print li 16 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Print matrix diagonally.py: -------------------------------------------------------------------------------- 1 | from sys import stdin as ip 2 | aa,bb=500,500 3 | def isvalid(i,j,r,c): 4 | if i<0 or i>=r or j>=c or j<0: 5 | return False 6 | else: 7 | return True 8 | def printDiagonally(li,n,m): 9 | for k in xrange(n): 10 | i=k-1 11 | j=1 12 | ct=0 13 | temp=[] 14 | while isvalid(i,j,n,m): 15 | temp.append( li[i][j]) 16 | ct+=1 17 | i-=1 18 | j+=1 19 | for i in xrange(ct-1,-1,-1): 20 | print temp[i], 21 | print li[k][0], 22 | for k in xrange(1,m): 23 | i=n-2 24 | j=k+1 25 | ct=0 26 | temp=[] 27 | while isvalid(i,j,n,m): 28 | temp.append( li[i][j]) 29 | i-=1 30 | j+=1 31 | ct+=1 32 | for i in xrange(ct-1,-1,-1): 33 | print temp[i], 34 | print li[n-1][k], 35 | 36 | printDiagonally([[1,2,3],[4,5,6],[7,8,9]],3,3) 37 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Submatrix Sum Queries.py: -------------------------------------------------------------------------------- 1 | def preprocess(li,aux,n,m): 2 | for i in xrange(m): 3 | aux[0][i]=li[0][i] 4 | for i in xrange(1,n): 5 | for j in xrange(m): 6 | aux[i][j]=li[i][j]+aux[i-1][j] 7 | for i in xrange(n): 8 | for j in xrange(1,m): 9 | aux[i][j]+=aux[i][j-1] 10 | return aux 11 | 12 | def calculateSum(li,lj,ri,rj): 13 | res=aux[ri][rj] 14 | if li>0: 15 | res-=aux[li-1][rj] 16 | if lj>0: 17 | res-=aux[ri][lj-1] 18 | if li>0 and lj>0: 19 | res+=aux[li-1][lj-1] 20 | return res 21 | 22 | aux,li=[],[] 23 | li=[[1, 2, 3, 4, 6], 24 | [5, 3, 8, 1, 2], 25 | [4, 6, 7, 5, 5], 26 | [2, 4, 8, 9, 4]] 27 | n,m=4,5 28 | aux=[[0 for i in xrange(m)] for i in xrange(n)] 29 | aux=preprocess(li,aux,n,m) 30 | print calculateSum(0,0,1,1) 31 | 32 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Subset Sum.py: -------------------------------------------------------------------------------- 1 | def subset_sum(li,total): 2 | l=len(li) 3 | dp=[[False for _ in xrange(total+1)] for _ in xrange(l+1)] 4 | for i in xrange(l+1): 5 | dp[i][0]=True 6 | for i in xrange(1,l+1): 7 | for j in xrange(1,total+1): 8 | if li[i-1]>j: 9 | dp[i][j]=dp[i-1][j] 10 | else: 11 | dp[i][j]=dp[i-1][j] or dp[i-1][j-li[i-1]] 12 | return dp[-1][-1] 13 | 14 | A=[3,34,4,12,5,2,124,421,412,125,1325,35,3,351,35,351,35,12312,1242142] 15 | s=8 16 | print subset_sum(A,s) 17 | -------------------------------------------------------------------------------- /Contests/Others/Python-modules-for-competitive-programming-master/Sum of factors or divisors of a number.py: -------------------------------------------------------------------------------- 1 | def pf(n): 2 | primfac = [] 3 | d = 2 4 | while d*d <= n: 5 | while (n % d) == 0: 6 | primfac.append(d) # supposing you want multiple factors repeated 7 | n //= d 8 | d += 1 9 | if n > 1: 10 | primfac.append(n) 11 | return primfac 12 | 13 | def sof(n): 14 | li=pf(n) 15 | k=li[0] 16 | i=0 17 | ct=0 18 | s=1 19 | while i 2 | 3 | using namespace std ; 4 | typedef long long ll ; 5 | 6 | ll n, threshold, speed[100005] ; 7 | 8 | ll answer(){ 9 | 10 | ll left = 0, right = threshold + 1, mid ; 11 | 12 | while(right > left + 1){ 13 | 14 | mid = (left + right) / 2 ; 15 | // logic part 16 | ll y = 0, z = 0 ; 17 | for(ll i = 0 ; i < n ; i++){ 18 | if(speed[i] > mid) y += (speed[i] - mid) / 3 ; 19 | else z += (mid - speed[i] + 1) / 2 ; // +1 addition for ceil value 20 | } 21 | 22 | if(y >= z) left = mid ; 23 | else right = mid ; 24 | 25 | } 26 | 27 | return left ; 28 | 29 | } 30 | 31 | void solve(){ 32 | 33 | cin >> n >> threshold ; 34 | for(ll i = 0 ; i < n ; i++) cin >> speed[i] ; 35 | 36 | cout << answer() << '\n' ; 37 | 38 | } 39 | 40 | int main(){ 41 | 42 | ll t ; cin >> t ; 43 | while(t--) solve() ; 44 | 45 | return 0 ; 46 | } -------------------------------------------------------------------------------- /Contests/Others/Tessellathon/p1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define pb push_back 6 | #define mp make_pair 7 | 8 | typedef long long ll; 9 | typedef pair pll; 10 | typedef vector vll; 11 | 12 | const int N = 5000; 13 | 14 | vll graph[N+1]; 15 | int num[N+1]; 16 | bool four = false; 17 | 18 | void dfs(int u,int c = 0) 19 | { 20 | num[u] = c; 21 | 22 | for(int v : graph[u]) 23 | { 24 | if(num[v] != 0) 25 | { 26 | if(abs(num[v] - num[u]) == 3) 27 | four = true; 28 | }else 29 | { 30 | dfs(v,c+1); 31 | } 32 | } 33 | } 34 | 35 | int main(void) 36 | { 37 | int n,m; 38 | cin>>n>>m; 39 | 40 | for(int i=0;i>u>>v; 44 | graph[u].pb(v); 45 | graph[v].pb(u); 46 | } 47 | 48 | for(int i=1;i<=n;i++) 49 | if(num[i] == 0) 50 | dfs(i); 51 | 52 | if(four) 53 | cout<<"Yes"; 54 | else 55 | cout<<"No"; 56 | } 57 | -------------------------------------------------------------------------------- /Contests/Others/Turing Cup Qualifier 2/p1.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | 14 | for __ in range(readInt()): 15 | a,b = readInts() 16 | arr = readInts() 17 | brr = readInts() 18 | if(a>b): 19 | win = 0 20 | else: 21 | win = 1 22 | # win = if(a>b):0 else:1 23 | # print(win) 24 | c= [0 ,0] 25 | for i in range(len(arr)): 26 | if(arr[i]&1): 27 | c[0]+=1 28 | for i in range(len(brr)): 29 | if(brr[i]&1): 30 | c[1]+=1 31 | # print(c[0],c[1]) 32 | if(c[win]>=c[(win+1)%2]): 33 | print("alan") 34 | else: 35 | print("chef") 36 | 37 | 38 | 39 | ''' 40 | Input: 41 | 1 42 | 3 2 43 | 1 2 3 8 9 10 11 4 5 6 7 44 | 21 31 43 52 11 36 22 11 23 17 8 45 | Output: 46 | chef 47 | ''' 48 | -------------------------------------------------------------------------------- /Contests/Others/Turing Cup Qualifier 2/p3.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | 14 | for __ in range(readInt()): 15 | m,n = readInts() 16 | a = readInts() 17 | c = 0 18 | for i in range(n-1): 19 | c= c+1 20 | c=c-min(c,a[i+1]-a[i]-1) 21 | c=c+1 22 | c=c-min(c,m-a[-1]) 23 | if c==0: 24 | print 1 25 | else: 26 | print c 27 | 28 | 29 | ''' 30 | Input: 31 | 2 32 | 5 5 33 | 1 2 3 4 5 34 | 5 2 35 | 1 3 36 | Output: 37 | 5 38 | 1 39 | ''' 40 | -------------------------------------------------------------------------------- /Contests/Others/Women's CodeSprint 5/p1.py: -------------------------------------------------------------------------------- 1 | import math 2 | def readInts(): 3 | return list(map(int, raw_input().strip().split())) 4 | def readInt(): 5 | return int(raw_input()) 6 | def readIntsindex0(): 7 | return list(map(lambda x: int(x) - 1, input().split())) 8 | def readStrs(): 9 | return raw_input().split() 10 | def readStr(): 11 | return raw_input() 12 | 13 | 14 | for __ in range(readInt()): 15 | a,b = readInts() 16 | 17 | 18 | 19 | 20 | ''' 21 | Sample Input 0 22 | 23 | 3 24 | 104 200 25 | 370 420 26 | 20 50 27 | Sample Output 0 28 | 29 | 4 30 | 0 31 | -1 32 | ''' 33 | -------------------------------------------------------------------------------- /Division.cpp: -------------------------------------------------------------------------------- 1 | void division(int num, int den) 2 | { 3 | string s; 4 | int x=(num/den); 5 | s=to_string(x); 6 | num%=den; 7 | num*=10; 8 | if(num!=0) 9 | { 10 | s+="."; 11 | for(int i=1;i<1000;i++) 12 | { 13 | int r=(num%den); 14 | int d=(num/den); 15 | s+=(d+48); 16 | r*=10; 17 | num=r; 18 | } 19 | } 20 | 21 | cout< >cost(n+1, vector (m+1,0)); 6 | for(int i=1;i<=n;i++) 7 | { 8 | cost[i][0]=i; 9 | } 10 | for(int i=1;i<=m;i++) 11 | { 12 | cost[0][i]=i; 13 | } 14 | 15 | for(int i=1;i<=m;i++) 16 | { 17 | for(int j=1;j<=n;j++) 18 | { 19 | cost[j][i]=min({cost[j-1][i]+1, cost[j][i-1]+1, cost[j-1][i-1]+(a[j-1]!=b[i-1])}); 20 | } 21 | } 22 | return cost[n][m]; 23 | } 24 | -------------------------------------------------------------------------------- /Euler_phi.cpp: -------------------------------------------------------------------------------- 1 | vectorphi(1000001); 2 | void pre() 3 | { 4 | for(int i=1;i<=1000000;i++) phi[i]=i; 5 | for(int i=2;i<=1000000;i++) 6 | { 7 | if(phi[i]==i) 8 | { 9 | phi[i]-=1; 10 | for(int j=i+i;j<1000001;j+=i) 11 | { 12 | phi[j]-=(phi[j]/i); 13 | } 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Exponentiation.cpp: -------------------------------------------------------------------------------- 1 | long long modexp(long long base, long long power, long long mod=1000000007) 2 | { 3 | long long res=1; 4 | while(power) 5 | { 6 | if(power&1) res=(res*base)%mod; 7 | power>>=1; 8 | base=(base*base)%mod; 9 | } 10 | return res; 11 | } 12 | 13 | long long invmod(long long num, long long mod=1000000007) 14 | { 15 | return modexp(num,mod-2,mod); 16 | } 17 | 18 | double logpow(double base, long long exp) 19 | { 20 | double res=0.0; 21 | while(exp) 22 | { 23 | if(exp&1) res+=base; 24 | exp>>=1; 25 | base+=base; 26 | } 27 | return res; 28 | } 29 | -------------------------------------------------------------------------------- /Extended_euclid.cpp: -------------------------------------------------------------------------------- 1 | int egcd(int a,int b, int* x, int* y) 2 | { 3 | if(a==0) 4 | { 5 | *x=0;*y=1; 6 | return b; 7 | } 8 | int x1,y1; 9 | int gcd=egcd(b%a,a,&x1,&y1); 10 | *x=y1-(b/a)*x1; 11 | *y=x1; 12 | return gcd; 13 | } -------------------------------------------------------------------------------- /FFTApplications.cpp: -------------------------------------------------------------------------------- 1 | 1) String Matching - Hamming Distance: 2 | 3 | // Logic: http://codeforces.com/blog/entry/59386 4 | 5 | void work(char ch1, char ch2) 6 | { 7 | int n=a.size(), m=b.size(); 8 | vector poly1(n, 0), poly2(n, 0); 9 | for(int i=0;i res; 14 | do_FFT(poly1, poly2, res); 15 | for(int i=m-1;i<=n+m-1;i++) 16 | ans[i]+=res[i]; 17 | } 18 | 19 | //Problem 1: https://www.spoj.com/problems/ADAMATCH/ 20 | //Solution 1: http://p.ip.fi/2K6S 21 | 22 | //Problem 2: https://codeforces.com/gym/101667/ : Problem H 23 | //Solution 2: http://p.ip.fi/dzmX 24 | -------------------------------------------------------------------------------- /Factorials with nCr.cpp: -------------------------------------------------------------------------------- 1 | int fact[N], invfact[N]; 2 | 3 | int pow(int a, int b, int m) 4 | { 5 | int ans=1; 6 | while(b) 7 | { 8 | if(b&1) 9 | ans=(ans*a)%m; 10 | b/=2; 11 | a=(a*a)%m; 12 | } 13 | return ans; 14 | } 15 | 16 | int modinv(int k) 17 | { 18 | return pow(k, MOD-2, MOD); 19 | } 20 | 21 | void precompute() 22 | { 23 | fact[0]=fact[1]=1; 24 | for(int i=2;i=0;i--) 31 | { 32 | invfact[i]=invfact[i+1]*(i+1); 33 | invfact[i]%=MOD; 34 | } 35 | } 36 | 37 | int nCr(int x, int y) 38 | { 39 | if(y>x) 40 | return 0; 41 | int num=fact[x]; 42 | num*=invfact[y]; 43 | num%=MOD; 44 | num*=invfact[x-y]; 45 | num%=MOD; 46 | return num; 47 | } 48 | -------------------------------------------------------------------------------- /Hashing (Strings): -------------------------------------------------------------------------------- 1 | struct Hashs 2 | { 3 | vector hashs; 4 | vector pows; 5 | int P; 6 | int MOD; 7 | 8 | Hashs() {} 9 | 10 | Hashs(string &s, int P, int MOD) : P(P), MOD(MOD) 11 | { 12 | int n = s.size(); 13 | pows.resize(n+1, 0); 14 | hashs.resize(n+1, 0); 15 | pows[0] = 1; 16 | for(int i=n-1;i>=0;i--) 17 | { 18 | hashs[i]=(1LL * hashs[i+1] * P + s[i] - 'a' + 1) % MOD; 19 | pows[n-i]=(1LL * pows[n-i-1] * P) % MOD; 20 | } 21 | pows[n] = (1LL * pows[n-1] * P)%MOD; 22 | } 23 | int get_hash(int l, int r) 24 | { 25 | int ans=hashs[l] + MOD - (1LL*hashs[r+1]*pows[r-l+1])%MOD; 26 | ans%=MOD; 27 | return ans; 28 | } 29 | }; 30 | 31 | //Problem 1: https://codeforces.com/contest/633/problem/C 32 | //Solution 1: https://codeforces.com/contest/633/submission/42330829 33 | -------------------------------------------------------------------------------- /IO_int128.cpp: -------------------------------------------------------------------------------- 1 | __int128 in() 2 | { 3 | __int128 temp=0; 4 | string s; 5 | cin>>s; 6 | for(char c:s) 7 | { 8 | temp*=10; 9 | temp+=(c-'0'); 10 | } 11 | return temp; 12 | } 13 | 14 | void out(__int128 x) 15 | { 16 | stacks; 17 | while(x) 18 | { 19 | s.push((x%10 +'0')); 20 | x/=10; 21 | } 22 | while(!s.empty()) 23 | { 24 | cout< active; 2 | 3 | void init() 4 | { 5 | active[-1] = -1; 6 | active[2e9] = 2e9; 7 | active[1] = n; 8 | } 9 | 10 | void add(int L, int R) //Always remove [L, R] before adding 11 | { 12 | active[L]=R; 13 | ans+=R-L+1; 14 | } 15 | 16 | void remove(int L, int R) 17 | { 18 | int removed=0; 19 | auto it = active.lower_bound(L); 20 | it--; 21 | if(it->second>=L) 22 | { 23 | active[L] = it->second; 24 | it->second = L-1; 25 | } 26 | it++; 27 | while(it->first <= R) 28 | { 29 | if(it->second > R) 30 | { 31 | removed+=R + 1 - it->first; 32 | active[R+1] = it->second; 33 | } 34 | else 35 | removed+= it->second - it->first + 1; 36 | auto it2=it; 37 | it++; 38 | active.erase(it2); 39 | } 40 | ans-=removed; 41 | } 42 | 43 | //Sample Problem 1: https://codeforces.com/contest/915/problem/E 44 | //Sample Solution 1: https://codeforces.com/contest/915/submission/40028187 45 | -------------------------------------------------------------------------------- /LCA/LCA (with Time Traversal DFS).cpp: -------------------------------------------------------------------------------- 1 | int tim=0; 2 | int parent[LG][N]; 3 | int tin[N], tout[N], level[N]; 4 | 5 | void dfs(int k, int par, int lvl) 6 | { 7 | tin[k]=++tim; 8 | parent[0][k]=par; 9 | level[k]=lvl; 10 | for(auto it:g[k]) 11 | { 12 | if(it==par) 13 | continue; 14 | dfs(it, k, lvl+1); 15 | } 16 | tout[k]=tim; 17 | } 18 | 19 | void precompute() 20 | { 21 | for(int i=1;i=0;i--) 33 | { 34 | if((1<=0;i--) 42 | { 43 | if(parent[i][u] && parent[i][u]!=parent[i][v]) 44 | { 45 | u=parent[i][u]; 46 | v=parent[i][v]; 47 | } 48 | } 49 | return parent[0][u]; 50 | } 51 | -------------------------------------------------------------------------------- /LCA/LCA.cpp: -------------------------------------------------------------------------------- 1 | #define SIZE 1005 2 | #define level 12 3 | #define lld int 4 | 5 | vector< vector >adj(SIZE); 6 | int depth[SIZE]; 7 | int tab[11][SIZE]; 8 | 9 | void dfs(int to, int from) 10 | { 11 | depth[to]=depth[from]+1; 12 | tab[1][to]=from; 13 | for(auto x:adj[to]) 14 | { 15 | if(x!=from) 16 | dfs(x,to); 17 | } 18 | } 19 | 20 | void process(int n) 21 | { 22 | for(int i=2;i<11;i++) 23 | { 24 | for(int j=1;j<=n;j++) 25 | { 26 | tab[i][j]=tab[i-1][tab[i-1][j]]; 27 | } 28 | } 29 | } 30 | 31 | void pre(int root, int n) 32 | { 33 | dfs(root,0); 34 | process(n); 35 | } 36 | 37 | int lca(int u, int v) 38 | { 39 | if(depth[u] > depth[v]) swap(u,v); 40 | 41 | if(depth[u] < depth[v]) 42 | { 43 | int diff=depth[v]-depth[u]; 44 | for(int i=9;i>=0;i--) 45 | { 46 | if(diff&(1<=1;i--) 53 | { 54 | if(tab[i][u]!=tab[i][v]) 55 | { 56 | u=tab[i][u]; 57 | v=tab[i][v]; 58 | } 59 | } 60 | return tab[1][u]; 61 | } -------------------------------------------------------------------------------- /LCS.cpp: -------------------------------------------------------------------------------- 1 | int lcs(string& a, string& b) 2 | { 3 | int n=a.length(); 4 | int m=b.length(); 5 | 6 | vector< vector< int > >sol(n+1, vector(m+1,0)); 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j<=m;j++) 10 | { 11 | if(a[i-1]==b[j-1]) 12 | sol[i][j]=sol[i-1][j-1]+1; 13 | else 14 | sol[i][j]=max(sol[i-1][j],sol[i][j-1]); 15 | } 16 | } 17 | return sol[n][m]; 18 | } 19 | -------------------------------------------------------------------------------- /Lcp_kasai.cpp: -------------------------------------------------------------------------------- 1 | vector kasai(string s, vector sa) 2 | { 3 | int n=s.size(),k=0; 4 | vector lcp(n,0); 5 | vector rank(n,0); 6 | 7 | for(int i=0; iP(s.length(),0); 7 | int center=0,right=-1,radius; 8 | 9 | for(int i=0;i=0 && s[i+radius]==s[i-radius]) 17 | radius++; 18 | 19 | P[i]=radius; 20 | if(i+radius-1 >right) 21 | { 22 | center=i; 23 | right=i+radius-1; 24 | } 25 | } 26 | for(int i=0;itree[400005]; 2 | int arr[100005]; 3 | 4 | void build(int idx, int l, int r) 5 | { 6 | if(l==r) 7 | { 8 | tree[idx].push_back(arr[l]); 9 | return; 10 | } 11 | int mid=(l+r)/2; 12 | build(idx*2, l ,mid); 13 | build(idx*2+1, mid+1, r); 14 | merge(tree[idx*2].begin(),tree[idx*2].end(), tree[idx*2+1].begin(), tree[idx*2+1].end(), back_inserter(tree[idx])); 15 | } 16 | 17 | int query(int idx, int l, int r, int beg, int end, int val) 18 | { 19 | if(rend || l>r) return 0; 20 | if(beg<=l && r<=end) 21 | { 22 | auto it =upper_bound(tree[idx].begin(), tree[idx].end(), val); 23 | return (tree[idx].end()-it); 24 | } 25 | return query(idx*2, l, (l+r)/2, beg, end, val)+query(idx*2+1,(l+r)/2 +1,r, beg, end, val); 26 | } -------------------------------------------------------------------------------- /Power_matrix.cpp: -------------------------------------------------------------------------------- 1 | vector< vector >mat(4, vector(4,1)); 2 | 3 | void multiply(auto& m1, auto& m2) 4 | { 5 | int n=m1.size(); 6 | vector< vector< lld> >temp(n, vector(n,0)); 7 | 8 | for(int i=0;i > power_matrix(auto mat, int n) 17 | { 18 | auto res=mat; 19 | n--; 20 | 21 | while(n) 22 | { 23 | if(n&1) multiply(res,mat); 24 | n>>=1; 25 | multiply(mat,mat); 26 | } 27 | return res; 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive Programming 2 | 3 | An effort to put all templates(dS + Algo) under one repo during the course time of May 2016 - Present . 4 | 5 | All the templates and standard algorithms are correct upto my knowledge. Feel free to contibute :) 6 | 7 | Hope it might be useful :) 8 | 9 | (Note : Try to share your thoughts and experience of competative coding in your github profile, it might be useful for others.) 10 | 11 | -------------------------------------------------------------------------------- /Random Generator: -------------------------------------------------------------------------------- 1 | //Source: http://codeforces.com/blog/entry/61587 2 | 3 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 4 | 5 | int getRand(int l, int r) 6 | { 7 | uniform_int_distribution uid(l, r); 8 | return uid(rng); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Sparse Matrix (RMQ).cpp: -------------------------------------------------------------------------------- 1 | //O(1) query sparse matrix for Max/Min/GCD 2 | 3 | int RMQ[20][N]; 4 | 5 | void precompute() 6 | { 7 | for(int i=0;(1<=1;i--) 14 | { 15 | RMQ[0][i]=dp[i]; 16 | int mxj=floorlog[n-i+1]; //2^j <= n-i+1 17 | int pw=1; 18 | for(int j=1;j<=mxj;j++) 19 | { 20 | RMQ[j][i]=max(RMQ[j-1][i], RMQ[j-1][i+pw]); 21 | pw<<=1; 22 | } 23 | } 24 | } 25 | 26 | int getMax(int L, int R) 27 | { 28 | int k=floorlog[R-L+1]; //2^k <= R-L+1 29 | return max(RMQ[k][L], RMQ[k][R - (1<point; 2 | vector< set >connected; 3 | 4 | void pre(int n) 5 | { 6 | point.resize(n+1); 7 | connected.resize(n+1); 8 | for(int i=1;i<=n;i++) point[i]=i; 9 | for(int i=1;i<=n;i++) connected[i].insert(i); 10 | } 11 | 12 | void merge(int x, int y) 13 | { 14 | for(auto node:connected[y]) 15 | { 16 | point[node]=x; 17 | } 18 | connected[x].insert(connected[y].begin(),connected[y].end()); 19 | connected[y].clear(); 20 | } 21 | 22 | bool same(int x, int y) 23 | { 24 | return point[x]==point[y]; 25 | } 26 | 27 | void add(int x, int y) 28 | { 29 | if(same(x,y)) return; 30 | if(connected[point[x]].size()<=connected[point[y]].size()) 31 | { 32 | merge(point[y],point[x]); 33 | } 34 | else 35 | { 36 | merge(point[x],point[y]); 37 | } 38 | } -------------------------------------------------------------------------------- /Z Algorithm.cpp: -------------------------------------------------------------------------------- 1 | //Logic = https://cp-algorithms.com/string/z-function.html 2 | 3 | //The Z-function for this string is an array of length n where the i-th element is equal to the greatest number 4 | //of characters starting from the position i that coincide with the first characters of s. 5 | 6 | vector z_function(string &s) 7 | { 8 | int n=s.size(); 9 | vector z(n); 10 | for(int i=1,l=0,r=0;ir) 17 | l=i, r=i+z[i]-1; 18 | } 19 | return z; 20 | } 21 | 22 | //Sample Problem 1: http://codeforces.com/problemset/problem/127/D 23 | //Sample Solution 1: http://codeforces.com/contest/127/submission/39791421 24 | 25 | //Sample Problem 2: http://codeforces.com/contest/432/problem/D 26 | //Sample Solution 2: http://codeforces.com/contest/432/submission/39791526 27 | -------------------------------------------------------------------------------- /algorepo/DataStructures/BIT/BIT1.c: -------------------------------------------------------------------------------- 1 | //Point Update, Range Sum Query Binary Indexed Tree 2 | //Author: Chandan Mittal 3 | #include 4 | 5 | int BIT[100010], n, a[100010]; 6 | 7 | void update(int x, int v) 8 | { 9 | while(x <= n) 10 | { 11 | BIT[x] += v; 12 | x += x & -x; 13 | } 14 | } 15 | 16 | int query(int k) 17 | { 18 | int s = 0; 19 | while(k > 0) 20 | { 21 | s += BIT[k]; 22 | k -= k & -k; 23 | } 24 | return s; 25 | } 26 | 27 | int main() 28 | { 29 | printf("Enter size of array>>"); 30 | scanf("%d",&n); 31 | int i, q, r; 32 | printf("Enter array elements\n"); 33 | for(i = 1 ; i <= n ; i++) 34 | { 35 | scanf("%d",&a[i]); 36 | update(i , a[i]); 37 | } 38 | printf("Enter range [x..y]>>"); 39 | scanf("%d %d",&q,&r); 40 | printf("sum of elements of range [%d..%d] is %d\n",q,r,query(r) - query(q-1)); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /algorepo/DataStructures/BIT/BIT2.c: -------------------------------------------------------------------------------- 1 | //Point sum query, Range update Binary Indexed tree 2 | #include 3 | 4 | int BIT[10010], a[10010], n; 5 | 6 | int query(int k) 7 | { 8 | int s =0; 9 | while(k > 0) 10 | { 11 | s += BIT[k]; 12 | k -= k & -k; 13 | } 14 | return s; 15 | } 16 | 17 | void update(int x, int v) 18 | { 19 | while(x <= n) 20 | { 21 | BIT[x] += v; 22 | x += x & -x; 23 | } 24 | } 25 | 26 | 27 | int main() 28 | { 29 | scanf("%d",&n); 30 | int i, x, y, v; 31 | for(i = 1; i <= n ; i++) 32 | { 33 | scanf("%d",&a[i]); 34 | update(i, a[i]); 35 | update(i+1, -a[i]); 36 | } 37 | 38 | scanf("%d %d",&l, &r); 39 | scanf("%d",&val); 40 | update(l, val); 41 | update(r+1, -val); 42 | 43 | int x;cin>>x; 44 | cout< 2 | using namespace std; 3 | #define INF 1<<29 4 | /* 5 | given infinite supply of some denominations of coins, 6 | and a value V to be paid, tell 7 | the minimum number of coins needed to pay value V 8 | */ 9 | 10 | 11 | int CoinChange(int *coinValue, int n, int V) 12 | { 13 | int coins[V+1]; 14 | //coins[j] is the minimum no of coins needed to pay 'j' dollars 15 | fill(coins, coins + V+1, INF); 16 | 17 | coins[0] = 0; 18 | for(int i = 0; i < n; i++) 19 | for(int j = coinValue[i]; j <= V; j++) 20 | coins[j] = min(coins[j], 1 + coins[j - coinValue[i]]); 21 | 22 | return coins[V]; 23 | } 24 | 25 | int main() 26 | { 27 | int coinValue[] = {1, 2, 5, 10}; 28 | int value; 29 | cin >> value; 30 | int ans = CoinChange(coinValue, 4, value); 31 | cout << ans << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /algorepo/dp/Coin_ways.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAXV 100000 4 | /* 5 | Given n denominations of coins, tell 6 | the number of ways of paying any amount V, 7 | using different combinations of coin denominations 8 | Each query can be answered in O(1) after preprocessing 9 | 10 | */ 11 | 12 | long long ways[MAXV]; 13 | int Count_ways(int *coins, int n) 14 | { 15 | //ways[i] is number of ways of paying amount 'i 16 | 17 | fill(ways, ways + MAXV, 0); 18 | ways[0] = 1; //# of ways of paying 0 dollar is 1 19 | 20 | for(int i = 0; i < n; i++) 21 | for(int j = 0; j < MAXV; j++) 22 | if( j >= coins[i]) 23 | ways[j] += ways[j - coins[i]]; 24 | } 25 | 26 | int main() 27 | { 28 | int deno[] = {5, 10, 20, 50}; 29 | int n = sizeof(deno)/sizeof(int); 30 | Count_ways(deno, n); 31 | 32 | int value; 33 | cin >> value; 34 | cout << ways[value] << endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /algorepo/dp/kadane.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int kadane(int *a, int n) 5 | { 6 | /* 7 | returns the maximum sum subArray 8 | Complexity: O(n) 9 | */ 10 | int max_ending_here = a[0], max_so_far = a[0]; 11 | for(int i = 1; i < n; i++) 12 | { 13 | max_ending_here = max(a[i], max_ending_here + a[i]); 14 | max_so_far = max(max_so_far, max_ending_here); 15 | } 16 | return max_so_far; 17 | 18 | } 19 | 20 | int main() 21 | { 22 | int a[] = {2, -2, -1, 4, 0, -7, 8}; 23 | int n = sizeof(a)/sizeof(int); 24 | int maxSumSubarray = kadane(a, n); 25 | cout << maxSumSubarray << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /algorepo/dp/kadaneIdx.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int kadane(int *a, int *start, int *end, int n) 5 | { 6 | int maxSum = INT_MIN, sum = 0; 7 | *end = -1; 8 | int local_start = 0; 9 | for(int i = 0; i < n; i++) 10 | { 11 | //sum = max(0, sum + a[i]); 12 | //maxSum = max(maxSum, sum); 13 | sum += a[i]; 14 | if(sum < 0) 15 | { 16 | sum = 0; 17 | local_start = i + 1; 18 | } 19 | else if(sum > maxSum) 20 | { 21 | maxSum = sum; 22 | *start = local_start; 23 | *end = i; 24 | } 25 | } 26 | if(*end != -1) 27 | return maxSum; 28 | } 29 | 30 | int main() 31 | { 32 | int start, end; 33 | int a[] = {-1, -2, -3}; 34 | int ans = kadane(a, &start, &end, 3 ); 35 | cout << ans << endl; 36 | cout << start << " " << end << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /algorepo/graph/DFS.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Depth First Search 3 | Time complexity: O(V + E) 4 | handle: calmhandtitan 5 | */ 6 | #include 7 | using namespace std; 8 | #define VI vector 9 | #define pb push_back 10 | #define SZ(v) (v).size() 11 | 12 | const int MAX = 1e5; // maximum no of nodes in a graph 13 | 14 | VI g[MAX]; 15 | bool vis[MAX]; 16 | int n, e; 17 | 18 | void dfs(int u) 19 | { 20 | vis[u] = 1; 21 | for(int i = 0; i < SZ(g[u]); i++) 22 | { 23 | int v = g[u][i]; 24 | if(!vis[v]) 25 | dfs(v); 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int x, y; 32 | scanf("%d %d",&n, &e); //get number of nodes and number of edges 33 | for(int i = 0; i < e; i++) 34 | { 35 | scanf("%d %d",&x, &y); 36 | x--,y--; 37 | g[x].pb(y); 38 | g[y].pb(x); //make undirected graph 39 | } 40 | 41 | for(int i = 0; i < n; i++) 42 | if(!vis[i]) //graph may be disconnected 43 | dfs(i); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /algorepo/numberTheory/LinearModularEquation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Linear Modular Equation 3 | Given a, b, c find x such that 4 | ax = b (mod c) and gcd(a, b, c) = 1 5 | 6 | then x = b*(a^phi(c)-1) (mod c) 7 | */ 8 | #include 9 | #define rep(i, x, n) for(size_t i = x, _n = n; i < _n; i++) 10 | #define lli long long 11 | using namespace std; 12 | 13 | lli modexpo(lli a, lli b, lli c) //computes a^b (mod c) in log2(b) time 14 | { 15 | lli x = 1, y = a; 16 | while(b > 0) 17 | { 18 | if(b&1) x = (x*y)%c; 19 | y = (y*y)%c; 20 | b >>= 1; 21 | } 22 | return x; 23 | } 24 | 25 | lli phi(lli n) 26 | { 27 | lli result = n; 28 | for(lli i = 2; i*i <= n; i++) 29 | { 30 | if(n%i == 0) 31 | { 32 | result -= result/i; 33 | while(n%i == 0) 34 | n /= i; 35 | } 36 | } 37 | if(n > 1) 38 | result -= result/n; 39 | return result; 40 | } 41 | 42 | int main() 43 | { 44 | lli a, b, c; 45 | cin >> a >> b >> c; 46 | lli fic = phi(c); 47 | lli x = modexpo(a, fic-1, c); 48 | x = (x*b)%c; 49 | cout << x << endl; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /algorepo/numberTheory/discreteLog.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Discrete Logarithm 3 | Given a, b, m and gcd(a, m) = 1 4 | find solutions to a^x = b (mod m) 5 | 6 | Algo: Baby Step Giant Step, meet in the middle 7 | Complexity: O(root(m) * log(m)) 8 | Author: Chandan Mittal 9 | */ 10 | #include 11 | #define lli long long 12 | using namespace std; 13 | 14 | lli modexpo(lli a, lli b, lli m) 15 | { 16 | lli x = 1, y = a; 17 | while(b > 0) 18 | { 19 | if(b&1) 20 | x = (x * y) % m; 21 | y = (y * y) % m 22 | b >>= 1; 23 | } 24 | return x % m; 25 | } 26 | 27 | 28 | VI solve(lli a, lli b, lli m) 29 | { 30 | vector ans; 31 | lli n = (lli) sqrt(m + .0) + 1; 32 | map vals; 33 | 34 | for(lli p = n; p >= 1; --p) 35 | vals[ modexpo(a, n * p, m) ] = p; 36 | for(lli q = 0; q <= n; q++) 37 | { 38 | lli cur = (modexpo(a, q, m) * b) % m; 39 | if(vals.cout(cur)) 40 | { 41 | lli tmp = n * vals[cur] - q; 42 | if(tmp < m) 43 | ans.pb(tmp); 44 | } 45 | } 46 | return ans; 47 | } 48 | 49 | 50 | int main() 51 | { 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /algorepo/numberTheory/etf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX = 100010; 5 | int phi[MAX]; 6 | 7 | void prePhi() //O(NlogN) precomputation of phi 8 | { 9 | 10 | for (int i = 1; i < MAX; i++) phi[i] = i; 11 | for (int i = 2; i < MAX; i++) 12 | if (phi[i] == i) 13 | { 14 | phi[i]--; 15 | for (int j = i * 2; j < MAX; j += i) 16 | phi[j] -= phi[j] / i; 17 | } 18 | } 19 | 20 | 21 | int main() 22 | { 23 | prePhi(); 24 | for(int i = 0; i < 10; i++) 25 | cout << phi[i] << " "; 26 | cout << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /algorepo/numberTheory/modular_multiplicative_inverse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Modular Multiplicative Inverse 3 | compute MMI of a (mod m) 4 | Using Fermat't theorem: O(log(m)) 5 | Usig Euler's theorem: O(n) for n numbers 6 | */ 7 | #include 8 | #define VI vector 9 | #define lli long long 10 | using namespace std; 11 | 12 | lli modexpo(lli a, lli b, lli c) 13 | { 14 | lli x = 1, y = a; 15 | while(b > 0) 16 | { 17 | if(b&1) x = (x*y)%c; 18 | y = (y&y)%c; 19 | b >>= 1; 20 | } 21 | return x; 22 | } 23 | 24 | lli MMI(lli a, lli m) //computes MMI of a (mod m) in O(log(m)) time 25 | { 26 | return modexpo(a, m-2, m); //if m is prime, and gcd(a, m) = 1 27 | } 28 | 29 | VI inverseArray(lli n, lli m) //computes MMI of n numbers from 1 to n (mod m) in O(n) time 30 | { 31 | VI modInverse(n+1, 0); 32 | modInverse[1] = 1; 33 | for(int i = 2; i <= n; i++) 34 | { 35 | modInverse[i] = (-(m/i) * modInverse[m % i])%m + m; 36 | } 37 | return modInverse; 38 | } 39 | 40 | int main() 41 | { 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /algorepo/numberTheory/nCr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long 3 | using namespace std; 4 | 5 | const int MOD = 1e9+7; 6 | const int MAX = 200010; 7 | 8 | //NOTE: factorial(30) doesn't fit in 64bit integer but 9 | // using below fn we can compute C(61, r) [this is the upperbound] 10 | 11 | //IMP: No modulus here else life would have been much easier 12 | 13 | lli C(int n, int r) //calculates nCr in O(r) time 14 | { 15 | lli z = 1; 16 | for(int i = 0; i < r; i++) 17 | z = z * (n-i)/(i+1); 18 | return z; 19 | } 20 | 21 | int main() 22 | { 23 | //no precomputation required 24 | int n, r; 25 | scanf("%d %d", &n , &r); 26 | lli ans = C(n, r); 27 | printf("(%dC%d) is %lld\n", n, r, ans); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /algorepo/numberTheory/nCrModP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long 3 | using namespace std; 4 | 5 | const int MOD = 1e9+7; 6 | const int MAX = 200010; 7 | 8 | lli mod_expo(lli a, lli b) 9 | { 10 | lli x = 1, y = a; 11 | while(b > 0) 12 | { 13 | if(b&1) x = (x*y)%MOD; 14 | y = (y*y)%MOD; 15 | b >>= 1; 16 | } 17 | return x; 18 | } 19 | 20 | lli fact[MAX], modFact[MAX]; 21 | 22 | void pre() //calculates nCr mod P where P is prime(1e9+7) and P>n, in O(n) time 23 | { 24 | fact[0] = 1; 25 | for(int i = 1 ; i <= MAX; i++) 26 | fact[i] = (i*fact[i-1])%MOD; 27 | 28 | 29 | modFact[0] = 1; 30 | 31 | for(int i = 1; i <= MAX; i++) 32 | modFact[i] = mod_expo(fact[i], MOD-2); 33 | } 34 | 35 | int main() 36 | { 37 | pre(); //do the MATH 38 | 39 | int n, r; 40 | scanf("%d %d", &n , &r); 41 | lli ans = fact[n]; 42 | ans = (ans*modFact[r])%MOD; 43 | ans = (ans*modFact[n-r])%MOD; 44 | printf("(%dC%d)%%%d is %lld\n", n, r,MOD, ans); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /algorepo/numberTheory/nCrModP_pascal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define lli long long 3 | using namespace std; 4 | 5 | const int MOD = 1e9+7; 6 | const int MAX = 1010; 7 | 8 | lli c[MAX][MAX]; 9 | void pre() //calculates nCr % P using pascals triangle in O(n^2) time 10 | { 11 | c[0][0] = 1; 12 | for(int i = 1; i < MAX; i++) 13 | { 14 | c[i][0] = 1; 15 | for(int j = 1; j <= i; j++) 16 | c[i][j] = (c[i-1][j-1] + c[i-1][j]) % MOD; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | pre(); //do the MATH 23 | 24 | int n, r; 25 | scanf("%d %d", &n , &r); 26 | 27 | printf("(%dC%d)%%%d is %lld\n", n, r,MOD, c[n][r]); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /algorepo/numberTheory/sieve_fast.cpp: -------------------------------------------------------------------------------- 1 | //sieves upto 10^8 in 0.5 sec, on core i5 CPU 2 | //Algo: Sieve of Eratosthenes using bitmasks 3 | #include 4 | #define i64 unsigned long long 5 | using namespace std; 6 | #define MAX 100000000 7 | #define LMT 10000 8 | 9 | unsigned flag[MAX/64]; 10 | unsigned primes[5761460], total; 11 | 12 | #define chkC(n) (flag[n>>6] & (1<<((n>>1)&31))) 13 | #define setC(n) (flag[n>>6] |= (1<<((n>>1)&31))) 14 | 15 | void sieve() 16 | { 17 | unsigned i, j, k; 18 | flag[0]|=0; 19 | for(i=3;i g[N]; 5 | 6 | void bfs(int k) 7 | { 8 | queue q; 9 | q.push(k); 10 | ways[k]=1; 11 | vis[k]=1; 12 | while(!q.empty()) 13 | { 14 | int node=q.front(); 15 | q.pop(); 16 | for(auto it:g[node]) 17 | { 18 | if(!vis[it]) 19 | { 20 | dist[it]=dist[node]+1; 21 | vis[it]=1; 22 | ways[it]+=ways[node]; 23 | q.push(it); 24 | } 25 | else 26 | { 27 | if(dist[node]+1==dist[it]) 28 | { 29 | ways[it]+=ways[node]; 30 | } 31 | } 32 | } 33 | } 34 | } 35 | 36 | 37 | //Problem 1: https://codeforces.com/contest/208/problem/C 38 | //Solution 1: https://codeforces.com/contest/208/submission/38315030 39 | -------------------------------------------------------------------------------- /bfs/BFS 2D: -------------------------------------------------------------------------------- 1 | int vis[N][N], dist[N][N], ways[N][N]; 2 | int dx[4]={-1, +1, 0, 0}; 3 | int dy[4]={0, 0, +1, -1}; 4 | 5 | void bfs(int x, int y, int n, int m) 6 | { 7 | queue > q; 8 | q.push({x, y}); 9 | ways[x][y]=1; 10 | vis[x][y]=1; 11 | dist[x][y]=0; 12 | while(!q.empty()) 13 | { 14 | int x=q.front().first; 15 | int y=q.front().second; 16 | q.pop(); 17 | for(int dir=0;dir<4;dir++) 18 | { 19 | int nx=x+dx[dir]; 20 | int ny=y+dy[dir]; 21 | if(nx<1 || ny<1 || nx>n || ny>m) 22 | continue; 23 | if(!vis[nx][ny]) 24 | { 25 | dist[nx][ny]=dist[x][y]+1; 26 | vis[nx][ny]=1; 27 | ways[nx][ny]+=ways[x][y]; 28 | q.push({nx, ny}); 29 | } 30 | else 31 | { 32 | if(dist[x][y] + 1 == dist[nx][ny]) 33 | ways[nx][ny]+=ways[x][y]; 34 | } 35 | } 36 | } 37 | } 38 | 39 | //Problem 1: https://www.spoj.com/problems/CLEANRBT/ 40 | //Solution 1: http://p.ip.fi/2fxO 41 | -------------------------------------------------------------------------------- /binary_search/bs1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/binary_search/bs1.cpp -------------------------------------------------------------------------------- /bitset_string_int.cpp: -------------------------------------------------------------------------------- 1 | 2 | int bitset_to_int(string s) 3 | { 4 | int num = 0; 5 | for(int i=0; i 2 | using namespace std; 3 | vectoradj[100]; 4 | bool vis[100]; 5 | bool instack[100]; 6 | bool dfs(int v) 7 | { 8 | vis[v]=1; 9 | instack[v]=1; 10 | for(int i=0;i>n>>m; 27 | for(int i=1;i<=m;i++) 28 | { 29 | int u,v;cin>>u>>v; 30 | adj[u].push_back(v); 31 | } 32 | bool flag=0; 33 | for(int i=0;i 2 | using namespace std; 3 | const int N = 1e2 + 5; 4 | int n , e , w , u , v; 5 | int a[N]; 6 | bool seen [N]; 7 | vector < int > X[N]; 8 | void solve(int u) { 9 | if(seen[u] | a[u] < w) { 10 | return; 11 | } 12 | seen[u] = true; 13 | for(int i = 0; i < (int) X[u].size(); ++i) { 14 | solve(X[u][i]); 15 | } 16 | } 17 | 18 | int main() { 19 | int tt , t = 0; 20 | scanf("%d" , &tt); 21 | while(tt--) { 22 | memset(seen , false , sizeof seen); 23 | for(int i = 0; i < N; ++i) X[i].clear(); 24 | scanf("%d %d %d" , &n, &e, &w); 25 | for(int i = 1; i <= n; ++i) { 26 | scanf("%d" , a + i); 27 | } 28 | for(; e; --e) { 29 | scanf("%d %d" , &u , &v); 30 | X[u].push_back(v); 31 | X[v].push_back(u); 32 | } 33 | solve(1); 34 | printf("Case #%d: " , ++t); 35 | printf(seen[n] ? "YES\n" : "NO\n"); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /dfs/dfs2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define pb push_back 6 | #define mp make_pair 7 | 8 | typedef long long ll; 9 | typedef pair pll; 10 | typedef vector vll; 11 | 12 | const int N = 5000; 13 | 14 | vll graph[N+1]; 15 | int num[N+1]; 16 | bool four = false; 17 | 18 | void dfs(int u,int c = 0) 19 | { 20 | num[u] = c; 21 | 22 | for(int v : graph[u]) 23 | { 24 | if(num[v] != 0) 25 | { 26 | if(abs(num[v] - num[u]) == 3) 27 | four = true; 28 | }else 29 | { 30 | dfs(v,c+1); 31 | } 32 | } 33 | } 34 | 35 | int main(void) 36 | { 37 | int n,m; 38 | cin>>n>>m; 39 | 40 | for(int i=0;i>u>>v; 44 | graph[u].pb(v); 45 | graph[v].pb(u); 46 | } 47 | 48 | for(int i=1;i<=n;i++) 49 | if(num[i] == 0) 50 | dfs(i); 51 | 52 | if(four) 53 | cout<<"Yes"; 54 | else 55 | cout<<"No"; 56 | } 57 | -------------------------------------------------------------------------------- /dfs/dfs3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | vector adj[1000006]; 5 | ll a[1000006]; 6 | ll dp[1000006]; 7 | void dfs(ll s,ll p) 8 | { 9 | dp[s]=dp[p]^a[s]; 10 | for(ll i=0;i>n; 25 | for(ll i=0;i>x>>y; 28 | adj[x].push_back(y); 29 | adj[y].push_back(x); 30 | } 31 | for(ll i=1;i<=n;i++) 32 | { 33 | cin>>a[i]; 34 | } 35 | dfs(1,0); 36 | cin>>q; 37 | while(q--) 38 | { 39 | cin>>x>>y; 40 | cout<<(dp[x]^dp[y])<<"\n"; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /diameter_of_a_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define SIZE 100001 5 | vector< vector >arr(SIZE, vector()); 6 | vector< int >depth(SIZE); 7 | vector< int >diameter_path; 8 | 9 | void dfs(int from, int to) 10 | { 11 | depth[to]=depth[from]+1; 12 | for(auto x:arr[to]) 13 | { 14 | if(x!=from) 15 | dfs(to,x); 16 | } 17 | } 18 | 19 | void add_path(int from, int to, int last, vector& v) 20 | { 21 | if(diameter_path.size() > 0) return; 22 | 23 | v.push_back(to); 24 | if(to == last) 25 | { 26 | diameter_path.resize(v.size()); 27 | for(int i=0;iv; 48 | add_path(0, leaf1, leaf2, v); 49 | } -------------------------------------------------------------------------------- /dp/dp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define MAXN 111 6 | #define MOD 100003 7 | #define LL long long int 8 | 9 | LL dp[MAXN][MAXN] = {0}; 10 | LL a, b, c, i, j; 11 | 12 | inline void Solve(){ 13 | cin>>a>>b>>c; 14 | memset(dp, 0, sizeof dp); 15 | dp[0][0] = 1; 16 | for(int i=0;i=b) 21 | { 22 | dp[i+1][j] += dp[i][j] * (j - b); 23 | dp[i + 1][j] %= MOD; 24 | dp[i+1][j+1] += dp[i][j] * (a - j); 25 | dp[i + 1][j + 1] %= MOD; 26 | } 27 | else{ 28 | dp[i+1][j+1] += dp[i][j] * (a - j); 29 | dp[i + 1][j + 1] %= MOD; 30 | } 31 | } 32 | } 33 | cout< 2 | using namespace std; 3 | 4 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 5 | #define endl "\n" 6 | #define int long long 7 | 8 | const int N=205; 9 | 10 | int n, k, x; 11 | int a[N]; 12 | int cache[N][N][N]; 13 | 14 | int dp(int idx, int prev, int taken) 15 | { 16 | if(prev>=k) 17 | return -1e15; 18 | if(taken>x) 19 | return -1e15; 20 | if(idx==n+1) 21 | { 22 | if(taken==x) 23 | return 0; 24 | return -1e15; 25 | } 26 | int &ans=cache[idx][prev][taken]; 27 | if(ans!=-1) 28 | return ans; 29 | ans=max(dp(idx+1, prev+1, taken), a[idx]+dp(idx+1, 0, taken+1)); 30 | return ans; 31 | } 32 | 33 | int32_t main() 34 | { 35 | IOS; 36 | memset(cache, -1, sizeof(cache)); 37 | cin>>n>>k>>x; 38 | for(int i=1;i<=n;i++) 39 | cin>>a[i]; 40 | int ans=dp(1, 0, 0); 41 | if(ans<0) 42 | ans=-1; 43 | cout< 2 | using namespace std; 3 | 4 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 5 | #define endl "\n" 6 | #define int long long 7 | 8 | const int N=405; 9 | 10 | int n, k, x; 11 | int a[N]; 12 | int cache[N][N]; 13 | 14 | int dp(int idx, int taken) 15 | { 16 | if(taken>x) 17 | return -1e15; 18 | if(taken==x && idx+k-1>n) 19 | return 0; 20 | if(idx>n) 21 | { 22 | if(taken==x) 23 | return 0; 24 | return -1e15; 25 | } 26 | int &ans=cache[idx][taken]; 27 | if(ans!=-1) 28 | return ans; 29 | ans=-1e15; 30 | for(int j=idx;j<=min(idx+k-1, n);j++) 31 | ans=max(ans, a[j] + dp(j+1, taken+1)); 32 | return ans; 33 | } 34 | 35 | int32_t main() 36 | { 37 | IOS; 38 | memset(cache, -1, sizeof(cache)); 39 | cin>>n>>k>>x; 40 | for(int i=1;i<=n;i++) 41 | cin>>a[i]; 42 | int ans=dp(1, 0); 43 | if(ans<0) 44 | ans=-1; 45 | cout< 2 | using namespace std; 3 | 4 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 5 | #define endl "\n" 6 | #define int long long 7 | 8 | const int N=405; 9 | 10 | int n, k, x; 11 | int a[N]; 12 | int cache[N][N]; 13 | 14 | int dp(int idx, int taken) 15 | { 16 | if(taken>x) 17 | return -1e15; 18 | if(idx>n) 19 | { 20 | if(taken==x) 21 | return 0; 22 | return -1e15; 23 | } 24 | int &ans=cache[idx][taken]; 25 | if(ans!=-1) 26 | return ans; 27 | ans=-1e15; 28 | for(int j=idx;j<=idx+k-1;j++) 29 | ans=max(ans, a[j] + dp(j+1, taken+(j<=n))); 30 | return ans; 31 | } 32 | 33 | int32_t main() 34 | { 35 | IOS; 36 | memset(cache, -1, sizeof(cache)); 37 | cin>>n>>k>>x; 38 | for(int i=1;i<=n;i++) 39 | cin>>a[i]; 40 | int ans=dp(1, 0); 41 | if(ans<0) 42 | ans=-1; 43 | cout< 2 | using namespace std; 3 | #define ll long long 4 | ll memo[10000]; 5 | ll countt(char ch[],ll i,ll n){ 6 | if(memo[i]!=-1) 7 | return memo[i]; 8 | ll j=i; 9 | ll sum=0; 10 | ll k=(ch[i]-'0'); 11 | if(ch[i]=='\0'){ 12 | if(k%2==0) 13 | return 1; 14 | else 15 | return 0; 16 | } 17 | if(k%2==0) 18 | sum++; 19 | sum=sum+countt(ch,j+1,n); 20 | memo[i]=sum; 21 | return sum; 22 | } 23 | int main(){ 24 | fill(memo,memo+10000,-1); 25 | char ch[100000]; 26 | cin>>ch; 27 | ll l=strlen(ch); 28 | for(ll i=0;i&tree) 2 | { 3 | for(int i=1;i<=n;i++) 4 | { 5 | if(i+(i&(-i))<=n) 6 | { 7 | tree[i+(i&(-i))]+=tree[i]; 8 | } 9 | } 10 | } 11 | 12 | void update(int pos,long long val, int n, vector&tree) 13 | { 14 | tree[pos]+=val; 15 | while(pos+pos&(-pos)<=n) 16 | { 17 | pos+=pos&(-pos); 18 | tree[pos]+=val; 19 | } 20 | } 21 | 22 | long long query(int idx, vector&tree) 23 | { 24 | lld ans=tree[idx]; 25 | while(idx-idx&(-idx)>0) 26 | { 27 | idx-=idx&(-idx); 28 | ans+=tree[idx]; 29 | } 30 | return ans; 31 | } -------------------------------------------------------------------------------- /find_cycle.cpp: -------------------------------------------------------------------------------- 1 | vector< int >cycle; 2 | int v1=0,v2=0; 3 | 4 | void dfs(int i, auto& adj, auto& vis, auto& in_stack, auto& path) 5 | { 6 | // cout<0) return; 8 | if(in_stack[i]) 9 | { 10 | cycle.push_back(i); 11 | while(path.top()!=i) 12 | { 13 | cycle.push_back(path.top()); 14 | in_stack[path.top()]=0; 15 | path.pop(); 16 | } 17 | cycle.push_back(i); 18 | in_stack[i]=0; 19 | return; 20 | } 21 | if(vis[i]) return; 22 | 23 | vis[i]=1; 24 | in_stack[i]=1; 25 | path.push(i); 26 | for(auto x:adj[i]) 27 | { 28 | if(i==v1 && x==v2) continue; 29 | dfs(x,adj,vis,in_stack,path); 30 | } 31 | in_stack[i]=0; 32 | if(!path.empty() && path.top()==i) 33 | path.pop(); 34 | } 35 | 36 | void find_cycle(vector< list >& adj) 37 | { 38 | int n=adj.size(); 39 | vectorvis(n,0),in_stack(n,0); 40 | for(int i=1;ipath; 45 | dfs(i,adj,vis,in_stack,path); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /inverse_modulo_map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define ll long long 4 | #define mx 200005 5 | #define mod 1000000009 6 | #define lim 100 7 | using namespace std; 8 | 9 | 10 | ll power(ll a,ll b) 11 | { 12 | ll x=1,y=a; 13 | while(b) 14 | { 15 | if(b%2) 16 | { 17 | x*=y; 18 | x%=mod; 19 | } 20 | b/=2; 21 | y*=y; 22 | y%=mod; 23 | } 24 | return x; 25 | } 26 | 27 | mapmp; 28 | 29 | int main(){ 30 | ios_base::sync_with_stdio(false); 31 | int n,q;cin>>n>>q; 32 | 33 | while(n--) 34 | { 35 | ll a,b,c;cin>>a>>b; 36 | c=4*a*b+2*max(a,b); 37 | mp[c]++; 38 | } 39 | 40 | while(q--) 41 | { 42 | ll k;cin>>k; 43 | ll fre=mp[(k*(k+1))]; 44 | if(fre) 45 | { 46 | cout< msum) msum=curr; 11 | if(curr < 0) curr=0; 12 | } 13 | if(least>=0) 14 | return msum; 15 | else 16 | return least; 17 | } 18 | -------------------------------------------------------------------------------- /leetcode: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/leetcode -------------------------------------------------------------------------------- /leetcode.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/leetcode.o -------------------------------------------------------------------------------- /leetcode1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/leetcode1 -------------------------------------------------------------------------------- /leetcode1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/harrypotter0/competitive-programming/82a8497e69212dc62e75af74b0d5a3b390b8aca2/leetcode1.o -------------------------------------------------------------------------------- /mobius.cpp: -------------------------------------------------------------------------------- 1 | #define D 101 2 | 3 | vectormu(D,0); 4 | vectorisprime(D,1); 5 | 6 | void pre() 7 | { 8 | int sq=sqrt(D); 9 | for(int i=2;i 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | typedef tree< pair,null_type,less >,rb_tree_tag,tree_order_statistics_node_update> ordered_set; 7 | 8 | int main() 9 | { 10 | ordered_set X; 11 | int n,d,count=0,median; 12 | cin>>n>>d; 13 | vectorarr(n); 14 | for(int i=0;i>arr[i]; 15 | 16 | for(int i=0;i=d) 19 | { 20 | if(d&1) median=((X.find_by_order(d/2))->first)*2; 21 | else median=((*X.find_by_order(d/2)).first+(*X.find_by_order(d/2 -1)).first); 22 | 23 | if(arr[i]>=(median)) count++; 24 | X.erase({arr[i-d],i-d}); 25 | } 26 | X.insert({arr[i],i}); 27 | } 28 | cout<>1; 10 | } 11 | return ans; 12 | } 13 | 14 | long double power(long double x, int y) 15 | { 16 | if(y==0) return 1; 17 | long double temp=power(x*x,y>>1); 18 | if(y&1) 19 | { 20 | if(y>0) 21 | return temp*x; 22 | return 1/(temp*x); 23 | } 24 | if(temp>0) return temp; 25 | return 1/temp; 26 | } 27 | 28 | unsigned long long powerl(unsigned long long x,unsigned long long y, unsigned long long int MOD=1000000009 ) 29 | { 30 | unsigned long long ans=1; 31 | while(y) 32 | { 33 | if(y&1) 34 | ans=ans*x%MOD; 35 | x=x*x%MOD; 36 | y>>=1; 37 | } 38 | return ans; 39 | } 40 | 41 | long double powerf(long double x, long long int y) 42 | { 43 | if(y==0 || x==1) return 1; 44 | long long int pow=y; 45 | y=abs(y); 46 | long double ans=1; 47 | while(y) 48 | { 49 | if(y&1) 50 | ans=ans*x; 51 | x=x*x; 52 | y>>=1; 53 | } 54 | if(pow>0) 55 | return ans; 56 | return 1.0/ans; 57 | } -------------------------------------------------------------------------------- /prime_check(Miller-Rabin).cpp: -------------------------------------------------------------------------------- 1 | __int128 modexp(__int128 base, __int128 power, __int128 mod=1000000007) 2 | { 3 | __int128 res=1; 4 | while(power) 5 | { 6 | if(power&1) res=(res*base)%mod; 7 | power>>=1; 8 | base=(base*base)%mod; 9 | } 10 | return res; 11 | } 12 | 13 | 14 | bool miller_rabin(__int128 num, __int128 k) 15 | { 16 | __int128 s=0,d=num-1,one=1,temp; 17 | while(!(d&1)) d>>=1,s++; 18 | 19 | bool flag=true; 20 | temp=modexp(k,d,num); 21 | if(temp==1 || temp==num-1) return 1; 22 | for(int i=0;iA{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67}; 34 | for(auto x:A) if(x==num) return 1; 35 | if(!(num&1)) return 0; 36 | 37 | bool flag=true; 38 | for(auto x:A) flag&=miller_rabin(num, x); 39 | return flag; 40 | } -------------------------------------------------------------------------------- /prime_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define mod 1000000007 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL),cout.tie(NULL); 9 | ll i,j,k; 10 | ll a[100007]={0}; 11 | for(i=2;i<=100000;i++) 12 | { 13 | for(j=2*i;j<=100000;j+=i) 14 | a[j]++; 15 | } 16 | ll t; 17 | cin>>t; 18 | while(t--) 19 | { 20 | ll n; 21 | cin>>n; 22 | if(a[n]==0) 23 | cout<<-1; 24 | else 25 | cout<isp(n8, 1); 2 | vector< long >prime; 3 | vector< long >product; 4 | void pre() 5 | { 6 | for(int i=2; i<=n8; i++) 7 | { 8 | if(isp[i]) 9 | { 10 | prime.push_back(i); 11 | product.push_back(1LL*i*i*i); 12 | if(i < 10001) 13 | for(int j=i*i; j<=n8-2; j+=i) 14 | { 15 | isp[j] = 0; 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /productoffact.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | #define ll long long 10 | #define MAX 1000100 11 | 12 | int n; 13 | ll mod; 14 | 15 | void input(){ 16 | scanf("%d", &n); 17 | } 18 | 19 | ll mul(ll a,ll b){ 20 | if(b == 0) return 0; 21 | if(b == 1) return a % mod; 22 | ll m = mul(a, b / 2); 23 | m = (m + m) % mod; 24 | if(b & 1) m = (m + a) % mod; 25 | return m; 26 | } 27 | 28 | void process(){ 29 | if(n >= 587117){ 30 | printf("0"); 31 | return; 32 | } 33 | ll ans = 1, gt = 1; 34 | // mod = 109546051211; 35 | mod = 329885391853; 36 | for(ll i = 1; i <= n; i++){ 37 | gt = mul(gt, i); 38 | ans = mul(ans, gt); 39 | } 40 | printf("%lld", ans); 41 | } 42 | 43 | int main(){ 44 | //freopen("in.txt", "r", stdin); 45 | int t; 46 | cin>>t; 47 | while(t--){ 48 | input(); 49 | } 50 | process(); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /segment_tree/Segment_tree.cpp: -------------------------------------------------------------------------------- 1 | //Bottom-up 2 | 3 | vectortree(2*(1e5+1)); 4 | 5 | void build(long long n) 6 | { 7 | for(long long i=n-1;i>0;--i) 8 | tree[i]=tree[i<<1]+tree[i<<1|1]; 9 | } 10 | 11 | void modify(long long pos, long long val, long long n) 12 | { 13 | pos+=n; 14 | for(tree[pos]=val; pos>1; pos>>=1) 15 | { 16 | tree[pos>>1]= (tree[pos] + tree[pos^1]); 17 | } 18 | } 19 | 20 | long long sum(long long l, long long r, long long n) 21 | { 22 | long long ans=0; 23 | for(l+=n, r=r+n+1; l>=1, r>>=1) 24 | { 25 | if(l&1) ans+=tree[l++]; 26 | if(r&1) ans+=tree[--r]; 27 | } 28 | return ans; 29 | } 30 | 31 | //Top-down 32 | -------------------------------------------------------------------------------- /segment_tree/segment_tree_lazy.cpp: -------------------------------------------------------------------------------- 1 | void upd(int id,int l,int r,int x){// increase all members in this interval by x 2 | lazy[id] += x; 3 | s[id] += (r - l) * x; 4 | } 5 | 6 | void shift(int id,int l,int r){//pass update information to the children 7 | int mid = (l+r)/2; 8 | upd(id * 2, l, mid, lazy[id]); 9 | upd(id * 2 + 1, mid, r, lazy[id]); 10 | lazy[id] = 0;// passing is done 11 | } 12 | 13 | void increase(int x,int y,int v,int id = 1,int l = 0,int r = n){ 14 | if(x >= r or l >= y) return ; 15 | if(x <= l && r <= y){ 16 | upd(id, l, r, v); 17 | return ; 18 | } 19 | shift(id, l, r); 20 | int mid = (l+r)/2; 21 | increase(x, y, v, id * 2, l, mid); 22 | increase(x, y, v, id*2+1, mid, r); 23 | s[id] = s[id * 2] + s[id * 2 + 1]; 24 | } 25 | 26 | int sum(int x,int y,int id = 1,int l = 0,int r = n){ 27 | if(x >= r or l >= y) return 0; 28 | if(x <= l && r <= y) return s[id]; 29 | shift(id, l, r); 30 | int mid = (l+r)/2; 31 | return sum(x, y, id * 2, l, mid) + 32 | sum(x, y, id * 2 + 1, mid, r); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /sparse_table/sptable.cpp: -------------------------------------------------------------------------------- 1 | // K should be 25 for 10^7 elements array 2 | // O(NlogN) 3 | int st[MAXN][K + 1]; 4 | 5 | for (int i = 0; i < N; i++) 6 | st[i][0] = f(array[i]); 7 | 8 | for (int j = 1; j <= K; j++) 9 | for (int i = 0; i + (1 << j) <= N; i++) 10 | st[i][j] = f(st[i][j-1], st[i + (1 << (j - 1))][j - 1]); 11 | 12 | // For Range Sum Queries 13 | // O(logMAXN) = O(K) 14 | long long sum = 0; 15 | for (int j = K; j >= 0; j--) { 16 | if ((1 << j) <= R - L + 1) { 17 | sum += st[L][j]; 18 | L += 1 << j; 19 | } 20 | } 21 | 22 | long long st[MAXN][K]; 23 | 24 | for (int i = 0; i < N; i++) 25 | st[i][0] = array[i]; 26 | 27 | for (int j = 1; j <= K; j++) 28 | for (int i = 0; i + (1 << j) <= N; i++) 29 | st[i][j] = st[i][j-1] + st[i + (1 << (j - 1))][j - 1]; 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /tarjan_scc.cpp: -------------------------------------------------------------------------------- 1 | int counter=0; 2 | 3 | void cycle(int i, auto& disc, auto& low, auto& adj, auto& path, auto& vis) 4 | { 5 | disc[i]=low[i]=++counter; 6 | path.push(i); 7 | vis[i]=true; 8 | 9 | for(auto x:adj[i]) 10 | { 11 | if(disc[x]==-1) 12 | { 13 | cycle(x,disc,low,adj,path,vis); 14 | low[i]=min(low[i],low[x]); 15 | } 16 | else if(vis[x]) 17 | { 18 | low[i]=min(low[i],disc[x]); 19 | } 20 | } 21 | 22 | if(low[i]==disc[i]) 23 | { 24 | while(path.top()!=i) 25 | { 26 | cout< >& adj) 37 | { 38 | int n=adj.size(); 39 | 40 | vectorlow(n,-1); 41 | vectordisc(n,-1); 42 | stackpath; 43 | vectorvis(n,0); 44 | 45 | for(int i=1;i f(m2)) return ter(m1, hi, iter+1); 11 | else return ter(lo, m2, iter+1); 12 | } 13 | -------------------------------------------------------------------------------- /topological_sort.cpp: -------------------------------------------------------------------------------- 1 | vectortopological; 2 | 3 | void dfs(int i, auto& vis, auto& adj) 4 | { 5 | vis[i]=true; 6 | 7 | for(auto x:adj[i]) 8 | { 9 | if(!vis[x]) 10 | { 11 | dfs(x,vis,adj); 12 | } 13 | } 14 | topological.push_back(i); 15 | } 16 | 17 | void topological_sort(vector< list >& adj) 18 | { 19 | int n=adj.size(); 20 | topological.reserve(n); 21 | vectorvis(n,false); 22 | 23 | for(int i=1;i 5 | 6 | using namespace std; 7 | 8 | class SocialWebsite{ 9 | private: 10 | protected: 11 | public: 12 | virtual void secret() {} ; 13 | }; 14 | 15 | class Facebook: public SocialWebsite{ 16 | private: 17 | string fbPassword; 18 | 19 | void secret(){ 20 | cout << "The Facebook password is: " << fbPassword << endl; 21 | cout << "Its risky, but its fine to print here as it's a private function\n"; 22 | } 23 | public: 24 | Facebook(string pwd) { 25 | fbPassword = pwd; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | Facebook f("Rachit95@fb"); 32 | 33 | SocialWebsite *ptr = &f; 34 | ptr->secret(); 35 | } 36 | -------------------------------------------------------------------------------- /trie/Trie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef struct TrieNode 7 | { 8 | int leaf=0; 9 | struct TrieNode* child[26]={NULL}; 10 | }trie; 11 | 12 | void add(string s, trie* t) 13 | { 14 | trie* temp=t; 15 | for(int i=0;ichild[s[i]-'a']) 19 | temp->child[s[i]-'a']=new trie; 20 | temp=temp->child[s[i]-'a']; 21 | } 22 | temp->leaf++; 23 | cout<<"\tdone\n"; 24 | } 25 | 26 | bool search(string s, trie* t) 27 | { 28 | trie* temp=t; 29 | for(char x:s) 30 | { 31 | cout<child[x-'a']) 33 | temp=temp->child[x-'a']; 34 | else 35 | return false; 36 | } 37 | return true; 38 | } 39 | 40 | int main() 41 | { 42 | trie *head=new trie; 43 | add("amazing",head); 44 | 45 | cout< VI; 3 | typedef pair PII; 4 | #define watch(x) cout << (#x) << " is " << (x) << endl 5 | 6 | #define REP(i,s,t) for(int i=(s);i<(t);i++) 7 | #define FILL(x,v) memset(x,v,sizeof(x)) 8 | 9 | const int INF = (int)1E9; 10 | #define MAXN 1005 11 | 12 | int rep[MAXN]; 13 | int find(int x) { 14 | return rep[x] == x ? x : rep[x] = find(rep[x]); 15 | } 16 | void unionset(int x, int y) { 17 | int rx = find(x), ry = find(y); 18 | if (rx == ry) return; 19 | rep[rx] = ry; 20 | } 21 | class Solution { 22 | public: 23 | int removeStones(vector>& st) { 24 | int n = st.size(); 25 | REP(i,0,n) rep[i] = i; 26 | REP(i,0,n) { 27 | REP(j,i+1,n) { 28 | if (st[i][0] == st[j][0] || st[i][1] == st[j][1]) unionset(i, j); 29 | } 30 | } 31 | int ans = 0; 32 | // REP(i,0,n) cout<