├── ACM ICPC Codebook ├── Data Structures │ └── segment_tree.cpp ├── Graphs and Trees │ └── Shortest Paths │ │ └── Floyd Warshall.cpp └── Number Theory │ ├── chinese remainder theorem.cpp │ ├── euler totient.cpp │ ├── matrix exponentiation.cpp │ ├── modular exponentiation.cpp │ └── modular inverse.cpp ├── ACM-ICPC ├── Amritapuri Regionals 2018-19 │ ├── CNTFAIL.cpp │ └── ISITCAKE.cpp ├── Chennai Regionals 2017-18 │ ├── MINSTR.cpp │ └── a.out ├── IITKGP Regionals 2017-18 │ ├── GENPERM.cpp │ ├── SPAMCLAS.c │ ├── STRLBP.py │ └── a.out ├── IITKGP Regionals 2018-19 │ ├── NOMATCH.cpp │ ├── PERIODCN.cpp │ ├── PLAYPIAN.cpp │ ├── SORTALGO.cpp │ └── SQRTRI.cpp ├── Kolkata Regionals 2017-18 │ ├── ZUBAPCNT.cpp │ ├── a.out │ └── out.txt ├── Kolkata Regionals 2018-19 │ ├── BEAUKONO.cpp │ ├── BUCKETWA.cpp │ ├── READCOST.cpp │ └── VALIDSTK.cpp ├── Online Round(India) 2017-18 │ ├── COMPEXP.py │ ├── ORDTEAMS.cpp │ ├── STDDEV.cpp │ └── a.out └── Online Round(India) 2018-19 │ ├── REDCGAME.cpp │ ├── ROBOGAME.cpp │ └── a.out ├── Algorithms and Data Structures ├── Algorithms │ ├── Djikstra's Shortest Path │ │ ├── Hospitals(Multisource).cpp │ │ ├── a.out │ │ └── singlesource.cpp │ ├── Lowest Common Ancestor │ │ ├── TEACHTR.cpp │ │ └── a.out │ ├── Math │ │ ├── GCD and LCM.cpp │ │ ├── modInverse_Extended_Euclidean_Algorithm.cpp │ │ └── modInverse_Fermats_Little_Theorem.cpp │ ├── Meet in the middle │ │ └── 1.cpp │ ├── Minimum Spanning Tree │ │ ├── a.out │ │ ├── kruskal.cpp │ │ ├── prim.cpp │ │ └── roadquality.cpp │ ├── String Algorithms │ │ └── Z_Algorithm.cpp │ └── Tree Flattening │ │ └── TYTACTIC.cpp └── Data Structures │ ├── Linked Lists │ └── main.c │ ├── Merge Sort Tree │ └── 1.cpp │ ├── Segment Trees │ ├── Lazy Propagation │ │ └── MadProf.cpp │ └── Range Query wth Point Updates │ │ └── TYTACTIC.cpp │ ├── Stack │ └── 1.cpp │ └── Tries │ └── 1.cpp ├── AtCoder └── dp │ ├── A.cpp │ ├── B.cpp │ ├── D.cpp │ ├── E.cpp │ └── F.cpp ├── CodeChef └── Solved Problems │ ├── ABX01.cpp │ ├── ABX02.cpp │ ├── ALTARAY.cpp │ ├── AMR14D.cpp │ ├── ANUUND.cpp │ ├── ASHIGIFT.cpp │ ├── BEERUS.cpp │ ├── CHEFARP.cpp │ ├── CHEFSOC2.cpp │ ├── CHEFST.cpp │ ├── CHFQUEUE.cpp │ ├── CHN05.cpp │ ├── CHRL4.cpp │ ├── CSUB.cpp │ ├── FRIEZA.cpp │ ├── GCDMOD.py │ ├── ICPC16B.c │ ├── ICPC16D.cpp │ ├── ICPC16F.cpp │ ├── KRYP6.cpp │ ├── MAXEP.cpp │ ├── MIXCOLOR.cpp │ ├── NMNMX.cpp │ ├── NSA.cpp │ ├── NW1.py │ ├── OPC1701.java │ ├── PERMSUFF.cpp │ ├── PRMDIV.cpp │ ├── SKIING.cpp │ ├── SPC1602.cpp │ ├── SPC1603.py │ ├── SPC1604.py │ ├── TYTACTIC.cpp │ ├── WOUT.cpp │ └── inp.in ├── Codeforces ├── Solved Problems │ ├── 1000A.cpp │ ├── 1000B.cpp │ ├── 1000C.cpp │ ├── 1000D.cpp │ ├── 1003A.cpp │ ├── 1003B.cpp │ ├── 1003C.cpp │ ├── 1003D.cpp │ ├── 1003F.java │ ├── 1004A.cpp │ ├── 1004B.cpp │ ├── 1004C.cpp │ ├── 1006A.cpp │ ├── 1006B.cpp │ ├── 1006C.cpp │ ├── 1006D.cpp │ ├── 1006E.cpp │ ├── 1006F.cpp │ ├── 1009E.cpp │ ├── 1011A.cpp │ ├── 1011B.cpp │ ├── 1011C.cpp │ ├── 1011D.cpp │ ├── 1011E.cpp │ ├── 1013A.cpp │ ├── 1013B.cpp │ ├── 1013C.cpp │ ├── 1015A.cpp │ ├── 1015B.cpp │ ├── 1015C.cpp │ ├── 1015D.cpp │ ├── 1015E.cpp │ ├── 1015F.cpp │ ├── 1025A.cpp │ ├── 1025B.cpp │ ├── 1025C.cpp │ ├── 1027A.cpp │ ├── 1027B.cpp │ ├── 1027C.cpp │ ├── 1027D.cpp │ ├── 1029A.cpp │ ├── 1029B.cpp │ ├── 1029C.cpp │ ├── 1029D.cpp │ ├── 1047A.cpp │ ├── 1047B.cpp │ ├── 1047C.cpp │ ├── 1056A.cpp │ ├── 1056B.cpp │ ├── 1056C.cpp │ ├── 1056D.cpp │ ├── 1081A.cpp │ ├── 1081B.cpp │ ├── 1081C.cpp │ ├── 1081D.cpp │ ├── 1082A.cpp │ ├── 1082B.cpp │ ├── 1082C.cpp │ ├── 1082D.cpp │ ├── 1084A.cpp │ ├── 1084B.cpp │ ├── 1084C.cpp │ ├── 1084D.cpp │ ├── 1087A.cpp │ ├── 1087B.cpp │ ├── 1087C.cpp │ ├── 1087D.cpp │ ├── 1088A.cpp │ ├── 1088B.cpp │ ├── 1088C.cpp │ ├── 1088D.cpp │ ├── 1088E.cpp │ ├── 1090A.cpp │ ├── 1090B.java │ ├── 1090D.cpp │ ├── 1090M.cpp │ ├── 1091A.cpp │ ├── 1091B.cpp │ ├── 1091C.cpp │ ├── 1091D.cpp │ ├── 1092A.cpp │ ├── 1092B.cpp │ ├── 1092C.cpp │ ├── 1092F.cpp │ ├── 1093A.cpp │ ├── 1093B.cpp │ ├── 1093C.cpp │ ├── 1093D.cpp │ ├── 1095A.cpp │ ├── 1095B.cpp │ ├── 1095C.cpp │ ├── 1095D.cpp │ ├── 1095E.cpp │ ├── 1096A.cpp │ ├── 1096B.cpp │ ├── 1096D.cpp │ ├── 1097A.cpp │ ├── 1097B.cpp │ ├── 1097C.cpp │ ├── 1099A.cpp │ ├── 1099B.cpp │ ├── 1099C.cpp │ ├── 1099D.cpp │ ├── 1100A.cpp │ ├── 1100B.cpp │ ├── 1100C.cpp │ ├── 1101A.cpp │ ├── 1101B.cpp │ ├── 1101C.cpp │ ├── 1101D.cpp │ ├── 1101E.cpp │ ├── 1102A.cpp │ ├── 1102B.cpp │ ├── 1102C.cpp │ ├── 1102D.cpp │ ├── 1102E.cpp │ ├── 1104A.cpp │ ├── 1104B.cpp │ ├── 1104C.cpp │ ├── 1105A.cpp │ ├── 1105B.cpp │ ├── 1105C.cpp │ ├── 1105D.cpp │ ├── 1119A.cpp │ ├── 1119B.cpp │ ├── 1119C.cpp │ ├── 1119D(editorial).cpp │ ├── 1119D.cpp │ ├── 1119E.cpp │ ├── 1139A.cpp │ ├── 1139B.cpp │ ├── 1139C.cpp │ ├── 1140A.cpp │ ├── 1140B.cpp │ ├── 1140C.cpp │ ├── 1140D.cpp │ ├── 1143A.cpp │ ├── 1143B.cpp │ ├── 1143C.cpp │ ├── 1143D.cpp │ ├── 1144A.cpp │ ├── 1144B.cpp │ ├── 1144C.cpp │ ├── 1144D.cpp │ ├── 1144F.cpp │ ├── 1147C.cpp │ ├── 1150A.cpp │ ├── 1150B.cpp │ ├── 1150C.cpp │ ├── 1159A.cpp │ ├── 1159B.cpp │ ├── 1159C.cpp │ ├── 1163A.cpp │ ├── 1163B(easy).cpp │ ├── 1163B(hard).cpp │ ├── 1163C.cpp │ ├── 1165A.cpp │ ├── 1165B.cpp │ ├── 1165C.cpp │ ├── 1165D.cpp │ ├── 1165E.cpp │ ├── 1167A.cpp │ ├── 1167B.cpp │ ├── 1167C.cpp │ ├── 1167D.cpp │ ├── 1176A.cpp │ ├── 1176B.cpp │ ├── 1176C.cpp │ ├── 1176E.cpp │ ├── 1176F.cpp │ ├── 1178A.cpp │ ├── 1178B.cpp │ ├── 1178C.cpp │ ├── 1178D.cpp │ ├── 1178E.cpp │ ├── 1182A.cpp │ ├── 1182B.cpp │ ├── 1182C.cpp │ ├── 1183D.cpp │ ├── 1183G.cpp │ ├── 1184A1.cpp │ ├── 1184B1.cpp │ ├── 1184C1.cpp │ ├── 1184D1.cpp │ ├── 1184E1.cpp │ ├── 1186D.cpp │ ├── 1187E.cpp │ ├── 1190D.cpp │ ├── 1191D.cpp │ ├── 1194D.cpp │ ├── 1195A.cpp │ ├── 1195B.cpp │ ├── 1195C.cpp │ ├── 1195D.cpp │ ├── 1195E.cpp │ ├── 1196E.cpp │ ├── 1196F.cpp │ ├── 1197A.cpp │ ├── 1197B.cpp │ ├── 1197C.cpp │ ├── 1197D.cpp │ ├── 1197E.cpp │ ├── 1199C.cpp │ ├── 1199D.cpp │ ├── 1201C.cpp │ ├── 1202C.cpp │ ├── 1203D.cpp │ ├── 1203E.cpp │ ├── 1209C.cpp │ ├── 1209D.cpp │ ├── 1209G1.cpp │ ├── 1215A.cpp │ ├── 1215C.cpp │ ├── 1215D.cpp │ ├── 1217A.cpp │ ├── 1217B.cpp │ ├── 1217C.cpp │ ├── 1217D.cpp │ ├── 1220E.cpp │ ├── 1230D.cpp │ ├── 1238D.cpp │ ├── 1241D.cpp │ ├── 1244A.cpp │ ├── 1244B.cpp │ ├── 1244D.cpp │ ├── 1250A.cpp │ ├── 1250B.cpp │ ├── 1250F.cpp │ ├── 1250H.cpp │ ├── 1250J.cpp │ ├── 1250L.cpp │ ├── 1254A.cpp │ ├── 1254B1.cpp │ ├── 1256E.cpp │ ├── 1257E.cpp │ ├── 1260D.cpp │ ├── 1263E.cpp │ ├── 1265C.cpp │ ├── 1265D.cpp │ ├── 1266A.cpp │ ├── 1266B.cpp │ ├── 1266C.cpp │ ├── 1267B.cpp │ ├── 1267E.cpp │ ├── 1270A.cpp │ ├── 1270B.cpp │ ├── 1270C.cpp │ ├── 1278A.cpp │ ├── 1278B.cpp │ ├── 1278C.cpp │ ├── 1278D.cpp │ ├── 1279A.cpp │ ├── 1279B.cpp │ ├── 1279C.cpp │ ├── 1279D.cpp │ ├── 1283A.cpp │ ├── 1283B.cpp │ ├── 1283C.cpp │ ├── 1287A.cpp │ ├── 1287C.cpp │ ├── 1293A.cpp │ ├── 1293B.cpp │ ├── 1293C.cpp │ ├── 1293D.cpp │ ├── 1345A.cpp │ ├── 1345B.cpp │ ├── 1345C.cpp │ ├── 1352G.cpp │ ├── 1353E.cpp │ ├── 1358A.cpp │ ├── 1358B.cpp │ ├── 1358D.cpp │ ├── 1362A.cpp │ ├── 1362B.cpp │ ├── 1362C.cpp │ ├── 1362D.cpp │ ├── 1363A.cpp │ ├── 1363B.cpp │ ├── 1363C.cpp │ ├── 1363E.cpp │ ├── 1365A.cpp │ ├── 1365B.cpp │ ├── 1365C.cpp │ ├── 1365D.cpp │ ├── 1369C.cpp │ ├── 1369D.cpp │ ├── 1373B.cpp │ ├── 1373D.cpp │ ├── 320A.py │ ├── 456B.c │ ├── 478C.cpp │ ├── 489C.cpp │ ├── 807A.c │ ├── 807B.c │ ├── 807C.c │ ├── 807E.cpp │ ├── 817D.cpp │ ├── 897A.c │ ├── 897B.c │ ├── 897C.c │ ├── 898A.cpp │ ├── 898B.cpp │ ├── 898C.cpp │ ├── 898D.cpp │ ├── 899A.cpp │ ├── 899B.cpp │ ├── 899C.cpp │ ├── 900A.cpp │ ├── 900B.cpp │ ├── 900C.cpp │ ├── 900E.cpp │ ├── 902A.cpp │ ├── 902B.cpp │ ├── 902C.cpp │ ├── 903A.cpp │ ├── 903B.cpp │ ├── 903C.cpp │ ├── 903D.py │ ├── 903E.cpp │ ├── 907A.cpp │ ├── 907B.cpp │ ├── 907C.cpp │ ├── 908A.py │ ├── 908C.py │ ├── 909A.java │ ├── 909B.cpp │ ├── 909C.cpp │ ├── 911A.py │ ├── 911B.py │ ├── 911C.py │ ├── 913A.cpp │ ├── 913B.cpp │ ├── 913C.cpp │ ├── 913D.cpp │ ├── 914A.cpp │ ├── 914B.cpp │ ├── 914C.cpp │ ├── 922D.cpp │ ├── 932C.cpp │ ├── 934A.cpp │ ├── 934B.cpp │ ├── 934C.cpp │ ├── 965D.cpp │ ├── 967D.cpp │ ├── 976E.cpp │ ├── 977A.cpp │ ├── 977B.cpp │ ├── 977C.cpp │ ├── 977D.cpp │ ├── 977E.cpp │ ├── 977F.cpp │ ├── 978G.cpp │ ├── 979C.cpp │ ├── 980A.cpp │ ├── 980C.cpp │ ├── 981D.cpp │ ├── 985D.py │ ├── 985E.cpp │ ├── 987A.cpp │ ├── 987B.py │ ├── 987C.cpp │ ├── 987D.cpp │ ├── 987E.cpp │ ├── 990D.cpp │ ├── 990E.cpp │ ├── 991A.cpp │ ├── 991B.cpp │ ├── 991C.cpp │ ├── 991D.cpp │ ├── 991E.cpp │ ├── 992A.cpp │ ├── 992B.cpp │ ├── 992C.cpp │ ├── 994A.cpp │ ├── 994B.cpp │ ├── 994D.cpp │ ├── 996A.cpp │ ├── 996B.cpp │ ├── 996D.cpp │ ├── 998A.cpp │ ├── 998B.cpp │ ├── 998C.cpp │ ├── 999A.cpp │ ├── 999B.cpp │ ├── 999C.cpp │ ├── 999E.cpp │ ├── 999F.cpp │ ├── A.cpp │ ├── C.cpp │ ├── Count The Blocks.cpp │ ├── F.cpp │ ├── Maximum White Subtree.cpp │ ├── Prefix-Suffix Palindrome (Hard version).cpp │ └── a.out └── Upsolved Problems │ └── Present.cpp ├── Facebook HackerCup ├── Facebook HackerCup 2018 │ ├── Qualification │ │ ├── Ethan_Searches_for_a_String.cpp │ │ ├── Interception.cpp │ │ └── Tourist.cpp │ ├── Round1 │ │ ├── Ethan_Traverses_a_Tree.cpp │ │ ├── Let_It_Flow.cpp │ │ └── Platform_Parkour.cpp │ └── Round2 │ │ └── jacks_candy_shop.cpp └── Facebook HackerCup 2019 │ ├── Qualification Round │ ├── Leapfrog:Ch.1 │ │ └── main.cpp │ ├── Leapfrog:Ch.2 │ │ └── main.cpp │ └── Mr.X │ │ └── main.cpp │ ├── Round1 │ ├── Class Treasurer │ │ └── main.cpp │ └── Graphs as a Service │ │ └── main.cpp │ └── Round2 │ └── Bitstrings as a Service │ └── Bitstrings as a Service.cpp ├── Google Code Jam ├── GCJ 2019 │ ├── Qualification Round │ │ ├── Foregone_Solution.cpp │ │ └── You_Can_Go_On_Your_Own_Way.cpp │ └── Round 1C │ │ ├── PowerArrangers.cpp │ │ └── RobotProgrammingStrategy.cpp └── GCJ 2020 │ ├── Nesting Depth.cpp │ ├── Parenting Partnering Returns.cpp │ ├── Vestigium.cpp │ └── a.out ├── Google Kickstart ├── Google KickStart 2019 │ ├── Practice_Round │ │ └── Number_Guessing.cpp │ ├── RoundA │ │ ├── Parcels_small.cpp │ │ ├── Training.cpp │ │ └── a.out │ └── RoundC │ │ ├── Circuit_Board.cpp │ │ └── Wiggle_Walk(small).cpp └── Google KickStart 2020 │ └── Round A │ ├── Allocation.cpp │ ├── Bundling.cpp │ ├── Plates.cpp │ ├── Workout.cpp │ └── a.out ├── IOI ├── Australia │ └── APIO 2008 │ │ └── dna.cpp ├── India │ ├── IARCS OPC Judge │ │ ├── 01TILES │ │ │ └── 0_1 Tiles.cpp │ │ ├── ENDCORR │ │ │ ├── End of Corruption.cpp │ │ │ └── End of Corruption.exe │ │ └── NEXTPERM │ │ │ └── next_perm.cpp │ ├── INOI │ │ ├── 2011 │ │ │ └── IOITRAININGCAMP20XX.cpp │ │ └── 2013 │ │ │ ├── Question Paper.pdf │ │ │ ├── Sequence Land.cpp │ │ │ └── sequenceland-data.zip │ └── IOITC │ │ ├── 2012 │ │ ├── Dragons(IOITC 2012 Test 2) │ │ │ ├── Dragons (EN).pdf │ │ │ └── Dragons.cpp │ │ ├── Microscope (IOITC 2012 Test 1) │ │ │ ├── Microscope (EN).pdf │ │ │ └── Microscope.cpp │ │ ├── roadquality.cpp │ │ └── vacations.cpp │ │ ├── 2013 │ │ ├── Bar Code (IOITC 2013 Online Test 2) │ │ │ ├── BarCode (EN).pdf │ │ │ └── BarCode.cpp │ │ ├── DivisorJumping2.cpp │ │ ├── HappyJourney.cpp │ │ ├── Kindergarten.cpp │ │ ├── MeetingPoint2.cpp │ │ ├── OddSort.cpp │ │ ├── SumThing(IOITC 2013 Online Test 2) │ │ │ ├── SumThing (EN).pdf │ │ │ └── SumThing.cpp │ │ ├── Unequal.cpp │ │ └── supersequence.cpp │ │ ├── 2014 │ │ └── StealingPyramid.cpp │ │ ├── 2015 │ │ ├── DP on Trees │ │ │ └── TreeMan.cpp │ │ ├── Dijkstra's Shortest Path │ │ │ └── Hospitals.cpp │ │ ├── Dynamic Programming │ │ │ └── SherlockWatson.cpp │ │ ├── Lowest Common Ancestor and DFS │ │ │ └── familytree.cpp │ │ ├── Priority Queue │ │ │ └── EmergencyHandling.cpp │ │ └── Segment Trees(with Lazy Propagation) │ │ │ └── MadProf.cpp │ │ └── Test Data Links.txt └── USA │ ├── USACO CONTESTS │ ├── Balanced Cow Breeds.cpp │ ├── Diamond Collector.cpp │ ├── Field Reduction.cpp │ └── Umbrellas for Cows.cpp │ └── USACO TRAINING │ ├── Bessie Come Home │ └── comehome.cpp │ ├── Controlling Companies │ └── concom.cpp │ ├── Cow Pedigrees │ └── nocows.cpp │ ├── Fractions to Decimals │ └── fracdec.java │ ├── Hamming Codes │ ├── Hamming Codes.PNG │ └── Hamming Codes.cpp │ ├── Healthy Holsteins │ ├── Healthy Holsteins.PNG │ └── Healthy Holsteins.cpp │ ├── Money Systems │ └── money.cpp │ ├── Party Lamps │ ├── Party Lamps(1).PNG │ ├── Party Lamps(2).PNG │ └── lamps.cpp │ ├── Preface Numbering │ ├── Preface Numbering (1).PNG │ ├── Preface Numbering (2).PNG │ └── preface.cpp │ ├── Runaround Numbers │ ├── Runaround Numbers.PNG │ └── runround.cpp │ ├── Sorting a Three Valued Sequence │ ├── Sorting a Three Valued Sequence.PNG │ └── Sorting a Three Valued Sequence.cpp │ ├── Subset Sums │ ├── Subset Sums.PNG │ └── subset.cpp │ ├── The Castle │ └── The Castle.cpp │ ├── The Tamworth Two │ └── ttwo.cpp │ └── Zero Sum │ └── zerosum.cpp ├── MISC ├── ITMO edx course │ ├── Fence Painting.cpp │ ├── Postfix.cpp │ └── Queue with Minimum Query in O(1).cpp └── TechGladiator2019 │ └── Qualifiers │ ├── Neighbours_and_New_Year_Party.cpp │ └── Won_or_Lose.cpp ├── README.md └── Topcoder └── SRM 698 └── RepeatStringEasy_SRM698_Google.cpp /ACM ICPC Codebook/Number Theory/euler totient.cpp: -------------------------------------------------------------------------------- 1 | // Start of template for Euler Totient Function 2 | long long phi(long long S){ 3 | long long result = S; 4 | for(long long i = 2; (i * i) <= S; ++i){ 5 | if((S % i) == 0){ 6 | while((S % i) == 0) 7 | S /= i; 8 | result = result - (result / i); 9 | } 10 | } 11 | if(S >= 2){ 12 | result = result - (result/ S); 13 | } 14 | return result; 15 | } 16 | // End of template for Euler Totient Function -------------------------------------------------------------------------------- /ACM ICPC Codebook/Number Theory/modular exponentiation.cpp: -------------------------------------------------------------------------------- 1 | // Start of template for mod exponentiation 2 | long long modExp(long long a, long long b, long long p) 3 | { 4 | if(b == 0){ 5 | return 1; 6 | } 7 | if(b == 1)return (a % p); 8 | long long ttt= modExp(a, b / 2, p); 9 | ttt = (ttt * ttt) % p; 10 | if((b % 2) == 0)return ttt; 11 | return ((ttt % p) * (a % p)) % p; 12 | } 13 | // End of template for mod exponentiation -------------------------------------------------------------------------------- /ACM-ICPC/Amritapuri Regionals 2018-19/ISITCAKE.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--){ 9 | long long int tmp; 10 | int res = 0; 11 | for(int i = 1; i <= 10; ++i) 12 | { 13 | for(int j = 1; j <= 10; ++j) 14 | { 15 | cin >> tmp; 16 | if(tmp <= 30) 17 | res++; 18 | } 19 | } 20 | if(res >= 60) 21 | cout << "yes\n"; 22 | else 23 | cout << "no\n"; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /ACM-ICPC/Chennai Regionals 2017-18/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/ACM-ICPC/Chennai Regionals 2017-18/a.out -------------------------------------------------------------------------------- /ACM-ICPC/IITKGP Regionals 2017-18/STRLBP.py: -------------------------------------------------------------------------------- 1 | T=input() 2 | while T>0: 3 | S=raw_input() 4 | CNT=0 5 | L=len(S) 6 | for i in range(1,L): 7 | if (S[i]=='1' and S[i-1]=='0') or (S[i]=='0' and S[i-1]=='1'): 8 | CNT=CNT+1 9 | if L!=1: 10 | if (S[L-1]=='1' and S[0]=='0') or (S[L-1]=='0' and S[0]=='1'): 11 | CNT=CNT+1 12 | if CNT<=2: 13 | print "uniform" 14 | else: 15 | print "non-uniform" 16 | T=T-1 -------------------------------------------------------------------------------- /ACM-ICPC/IITKGP Regionals 2017-18/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/ACM-ICPC/IITKGP Regionals 2017-18/a.out -------------------------------------------------------------------------------- /ACM-ICPC/IITKGP Regionals 2018-19/NOMATCH.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | long long int A[N]; 13 | for(int i = 0; i < N; ++i) 14 | cin >> A[i]; 15 | sort(A, A + N); 16 | vector S; 17 | int cnt = N - 1; 18 | for(int i = 0; i < N; ++i) 19 | { 20 | if(S.size() == N) 21 | break; 22 | S.push_back(A[i]); 23 | S.push_back(A[cnt]); 24 | cnt--; 25 | } 26 | long long int res = 0; 27 | for(int i = 1; i < N; i += 2) 28 | res = res + abs(S[i] - S[i - 1]); 29 | cout << res << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /ACM-ICPC/IITKGP Regionals 2018-19/PLAYPIAN.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | string S; 11 | cin >> S; 12 | int L = S.size(); 13 | int valid = 1; 14 | for(int i = 0; i < L; i += 2) 15 | { 16 | if(S[i] != S[i + 1]) 17 | { 18 | continue; 19 | } 20 | else{ 21 | valid = 0; 22 | break; 23 | } 24 | } 25 | if(valid) 26 | cout << "yes\n"; 27 | 28 | else 29 | cout << "no\n"; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /ACM-ICPC/Kolkata Regionals 2017-18/ZUBAPCNT.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const long long MOD=1e9+7; 4 | const long long MAXM=1e5+5; 5 | long long T26[MAXM]; 6 | long long T25[MAXM]; 7 | string S; 8 | int N,M,T; 9 | int main() 10 | { 11 | T26[0]=1; T25[0]=1; 12 | for(int i=1; i>T; 18 | for(int i=1; i<=T; i++) 19 | { 20 | cout<<"Case "<>N>>M; 22 | while(M--) 23 | { 24 | cin>>S; 25 | int L=S.size(); 26 | if(L>N){ 27 | cout<<"0\n"; 28 | continue; 29 | } 30 | long long prod=((N-L+1)*(T26[N-L]))%MOD; 31 | cout<> dH >> dL >> dR >> K; 32 | while(dH != 0 || dL != 0 || dR != 0 || K != 0) 33 | { 34 | cout << solve() << endl; 35 | cin >> dH >> dL >> dR >> K; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /ACM-ICPC/Kolkata Regionals 2018-19/VALIDSTK.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | int fl = 0; 13 | int valid = 1; 14 | for(int i = 0; i < N; ++i){ 15 | int tmp; 16 | cin >> tmp; 17 | if(tmp) 18 | fl++; 19 | 20 | else 21 | fl--; 22 | if(fl < 0) 23 | { 24 | valid = 0; 25 | } 26 | } 27 | if(valid) 28 | cout << "Valid\n"; 29 | else 30 | cout << "Invalid\n"; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /ACM-ICPC/Online Round(India) 2017-18/COMPEXP.py: -------------------------------------------------------------------------------- 1 | T = int(raw_input()) 2 | 3 | while T > 0: 4 | S = raw_input().split() 5 | N = int(S[0]) 6 | K = int(S[1]) 7 | ans = 2 * (N - (N - 1) * 1.0/(K * 1.0)); 8 | print("%.9f" % ans) 9 | T = T - 1 -------------------------------------------------------------------------------- /ACM-ICPC/Online Round(India) 2017-18/ORDTEAMS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int A[3][3]; 4 | bool chk(int a, int b) 5 | { 6 | int greater=0; 7 | for(int i=0; i<3; i++) 8 | { 9 | if(A[a][i]>A[b][i])greater=1; 10 | else if(A[a][i]==A[b][i])continue; 11 | else return false; 12 | } 13 | if(greater)return true; 14 | return false; 15 | } 16 | int main() 17 | { 18 | int T; 19 | cin>>T; 20 | while(T--) 21 | { 22 | 23 | for(int i=0; i<3; i++) 24 | { 25 | for(int j=0; j<3; j++)cin>>A[i][j]; 26 | } 27 | int flag=0; 28 | for(int i=0; i<3; i++) 29 | { 30 | for(int j=0; j<3; j++) 31 | { 32 | if(i==j)continue; 33 | for(int k=0; k<3; k++) 34 | { 35 | if(k==i || k==j)continue; 36 | if(chk(i,j) && chk(j,k)) 37 | { 38 | flag=1; 39 | break; 40 | } 41 | } 42 | } 43 | } 44 | if(flag==1)cout<<"yes\n"; 45 | else cout<<"no\n"; 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /ACM-ICPC/Online Round(India) 2017-18/STDDEV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--) 8 | { 9 | long long N,sigma; 10 | cin>>N>>sigma; 11 | if(N==1 && sigma!=0) 12 | { 13 | cout<<"-1\n"; 14 | } 15 | else if(sigma==0) 16 | { 17 | for(int i=0; i> T; 11 | while(T--) 12 | { 13 | cin >> S; 14 | int L = S.size(); 15 | int A[L]; 16 | for(int i = 0; i < L; i++) 17 | A[i] = -1; 18 | int safe = 1; 19 | for(int i = 0; i < L; i++) 20 | { 21 | if(S[i] == '.') 22 | continue; 23 | int num = S[i] - '0'; 24 | for(int j = max(0, i - num); j <= min(i + num, L - 1); j++) 25 | { 26 | if(A[j] != -1) 27 | { 28 | safe = 0; 29 | } 30 | else 31 | A[j] = i; 32 | } 33 | } 34 | if(safe) 35 | cout << "safe\n"; 36 | else 37 | cout << "unsafe\n"; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /ACM-ICPC/Online Round(India) 2018-19/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/ACM-ICPC/Online Round(India) 2018-19/a.out -------------------------------------------------------------------------------- /Algorithms and Data Structures/Algorithms/Djikstra's Shortest Path/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/Algorithms and Data Structures/Algorithms/Djikstra's Shortest Path/a.out -------------------------------------------------------------------------------- /Algorithms and Data Structures/Algorithms/Lowest Common Ancestor/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/Algorithms and Data Structures/Algorithms/Lowest Common Ancestor/a.out -------------------------------------------------------------------------------- /Algorithms and Data Structures/Algorithms/Math/GCD and LCM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /*The Euclidean Algorithm to compute GCD of two numbers 4 | Refer: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm 5 | Problem: https://www.codechef.com/problems/FLOW016*/ 6 | int T; 7 | long long X,Y; 8 | long long gcd(long long A, long long B){ 9 | if(B==0)return A; 10 | return gcd(B,A%B); 11 | } 12 | int main() 13 | { 14 | cin>>T; 15 | while(T--){ 16 | cin>>X>>Y; 17 | long long hcf=gcd(max(X,Y),min(X,Y)); 18 | long long lcm=(X*Y)/hcf; 19 | cout<>A>>B; 22 | cout<> N; 25 | for(int i = 0; i < N; ++i) 26 | cin >> H[i]; 27 | for(int i = 0; i < N; ++i) 28 | DP[i] = -1; 29 | cout << rec(0) << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /AtCoder/dp/B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e5 + 5; 4 | long long int DP[MAXN]; 5 | int N, K; 6 | long long H[MAXN]; 7 | long long rec(int idx) 8 | { 9 | if(idx == (N - 1)) 10 | { 11 | return 0; 12 | } 13 | if(DP[idx] != -1) 14 | return DP[idx]; 15 | DP[idx] = abs(H[idx] - H[idx + 1]) + rec(idx + 1); 16 | for(int i = 2; i <= K; ++i) 17 | if((idx + i) <= (N - 1)) 18 | DP[idx] = min(DP[idx], abs(H[idx] - H[idx + i]) + rec(idx + i)); 19 | return DP[idx]; 20 | } 21 | 22 | int main() 23 | { 24 | cin >> N >> K; 25 | for(int i = 0; i < N; ++i) 26 | cin >> H[i]; 27 | for(int i = 0; i < N; ++i) 28 | DP[i] = -1; 29 | cout << rec(0) << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /AtCoder/dp/D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 105; 4 | const int MAXW = 1e5 + 5; 5 | const long long int neg_inf = -1e18; 6 | int N, W; 7 | long long int DP[MAXN][MAXW]; 8 | long long wt[MAXN]; 9 | long long V[MAXN]; 10 | 11 | long long rec(int idx, int weight) 12 | { 13 | if (idx == N) 14 | { 15 | if (weight <= W) 16 | return 0; 17 | return neg_inf; 18 | } 19 | if (weight > W) 20 | return neg_inf; 21 | if (DP[idx][weight] != -1) 22 | return DP[idx][weight]; 23 | return DP[idx][weight] = max(V[idx] + rec(idx + 1, weight + wt[idx]), rec(idx + 1, weight)); 24 | } 25 | 26 | int main() 27 | { 28 | cin >> N >> W; 29 | for (int i = 0; i < N; ++i) 30 | cin >> wt[i] >> V[i]; 31 | for (int i = 0; i < MAXN; ++i) 32 | for (int j = 0; j < MAXW; ++j) 33 | DP[i][j] = -1; 34 | cout << rec(0, 0) << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /CodeChef/Solved Problems/ABX01.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | long long A,N; 4 | int SOD[85]; 5 | map XXX; 6 | 7 | int sod(long long S) 8 | { 9 | int ret=0; 10 | while(S>0){ 11 | ret+=(S%10); 12 | S/=10; 13 | } 14 | return ret; 15 | } 16 | int SSS(long long S) 17 | { 18 | while(S>9) 19 | S=sod(S); 20 | return S; 21 | } 22 | long long mod_pow(long long a, long long b) 23 | { 24 | if(b==0)return 1; 25 | if(b==1)return XXX[a]; 26 | long long T=mod_pow(a,b/2); 27 | long long res=T*T; 28 | res=SOD[res]; 29 | if((b%2)!=0)res*=XXX[a]; 30 | return SOD[res]; 31 | } 32 | int main() 33 | { 34 | int T; 35 | cin>>T; 36 | for(int i=1; i<=82; i++)SOD[i]=SSS(i); 37 | while(T--) 38 | { 39 | cin>>A>>N; 40 | XXX[A]=SSS(A); 41 | //cout<>T; 13 | while(T--) 14 | { 15 | cin>>N; 16 | for(int i=0; i>A[i]; 18 | } 19 | DP[N-1] = 1; 20 | for(int i=N-2; i>=0; i--){ 21 | if(A[i]>0 && A[i+1]<0){ 22 | DP[i] = 1 + DP[i+1]; 23 | continue; 24 | } 25 | if(A[i]<0 && A[i+1]>0){ 26 | DP[i] = 1 + DP[i+1]; 27 | continue; 28 | } 29 | DP[i] = 1; 30 | } 31 | for(int i=0; i>T; 8 | while(T--) 9 | { 10 | int A[1005]; 11 | for(int i=0; i<1005; i++) 12 | A[i] = 0; 13 | int N; 14 | cin>>N; 15 | for(int i=1; i<=N; i++) 16 | { 17 | int u; 18 | cin>>u; 19 | A[u] += 1; 20 | } 21 | int flag = 0; 22 | for(int i=1; i<=1000; i++){ 23 | if(A[i] > 1) 24 | { 25 | flag = 1; 26 | break; 27 | } 28 | } 29 | if(flag == 1)cout<<"NO\n"; 30 | else cout<<"YES\n"; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /CodeChef/Solved Problems/ANUUND.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | const int MAXN = 1e5 + 5; 5 | 6 | long long A[MAXN]; 7 | 8 | int main() 9 | { 10 | int T, N; 11 | cin>>T; 12 | while(T--) 13 | { 14 | cin>>N; 15 | for(int i=0; i>A[i]; 17 | sort(A, A+N); 18 | for(int i=N-1; i>=0; i-=2) 19 | { 20 | if(i-1>=0) 21 | cout<>T; 15 | while(T--) 16 | { 17 | cin>>N; 18 | for(int i=0; i>A[i]; 20 | for(int i=0; i>B[i]; 22 | for(int i=0; i 2 | using namespace std; 3 | int main() 4 | { 5 | int N,T; 6 | cin>>T; 7 | while(T--) 8 | { 9 | cin>>N; 10 | long long A[N]; 11 | int ans=0; 12 | for(int i=0; i>A[i]; 13 | for(int i=0; i= N)return 0; 13 | if(passes == -1 && ind == S)return 1; 14 | if(passes == -1)return 0; 15 | if(dp[ind][passes]!=-1)return dp[ind][passes]; 16 | 17 | return dp[ind][passes] = (rec(ind - A[passes], passes - 1) + rec(ind + A[passes], passes - 1))%MOD; 18 | } 19 | 20 | void solve() 21 | { 22 | cin>>N>>M>>S; 23 | for(int i=0; i>A[i]; 25 | for(int i=0; i>T; 38 | while(T--) 39 | solve(); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /CodeChef/Solved Problems/CHEFST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long int m,x1,x2; 7 | long long int x = min(x1,x2); 8 | if(x<=m) 9 | { 10 | cout << max(x1,x2) - x << '\n'; 11 | } 12 | if(x<=m) 13 | { 14 | long long int sum=0; 15 | long long int i=1; 16 | while(m*(m+1)/2 <= x) 17 | { 18 | m--; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /CodeChef/Solved Problems/CHFQUEUE.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | const long long MOD = 1e9 + 7; 5 | 6 | int main() 7 | { 8 | int N,K; 9 | cin>>N>>K; 10 | stack < pair > S; 11 | long long next_lesser[N]; 12 | for(int i=0; i>u; 18 | while(S.empty() == false && S.top().first > u) 19 | { 20 | next_lesser[S.top().second] = i; 21 | S.pop(); 22 | } 23 | S.push(make_pair(u,i)); 24 | } 25 | long long ret = 1; 26 | for(int i=0; i 2 | using namespace std; 3 | int main() 4 | { 5 | int T,N,M; 6 | cin>>T; 7 | while(T--) 8 | { 9 | cin>>N>>M; 10 | if(M==1){ 11 | cout<<(N-1)< > S; 9 | int N,K; 10 | int main() 11 | { 12 | cin>>N>>K; 13 | for(int i=0; i>A[i]; 15 | for(int i=0; i<=min(K, N-1); i++){ 16 | if(S.empty()){ 17 | DP[i] = log(A[i]); 18 | helper[i] = A[i]; 19 | } 20 | else{ 21 | DP[i] = (log(A[i]) + ((*S.begin()).first)); 22 | helper[i] = (A[i] * helper[(*S.begin()).second])%MOD; 23 | } 24 | S.insert(make_pair(DP[i], i)); 25 | } 26 | for(int i=K+1; i p =make_pair(DP[i-K-1], i-K-1); 28 | S.erase(S.find(p)); 29 | if(S.empty()){ 30 | DP[i] = log(A[i]); 31 | helper[i] = A[i]; 32 | } 33 | else{ 34 | DP[i] = (log(A[i]) + (*S.begin()).first); 35 | helper[i] = (A[i] * helper[(*S.begin()).second])%MOD; 36 | } 37 | S.insert(make_pair(DP[i], i)); 38 | } 39 | cout<>T; 11 | while(T--) 12 | { 13 | cin>>L; 14 | cin>>S; 15 | int A[L]; 16 | if(S[L-1] == '1') 17 | A[L-1] = 1; 18 | else 19 | A[L-1] = 0; 20 | for(int i=L-2; i>=0; i--) 21 | A[i] = A[i + 1] + (S[i] - '0'); 22 | long long ret = 0; 23 | for(int i=0; i 0: 4 | if e%2 == 1: 5 | p = ((p%m)*(b%m))%m 6 | b = ((b%m)*(b%m))%m 7 | e = e/2 8 | return p 9 | 10 | def gcd(a, b): 11 | if b > a: 12 | tmp = a 13 | a = b 14 | b = tmp 15 | if b == 0: 16 | return a 17 | while a%b != 0: 18 | tmp = a%b 19 | a = b 20 | b = tmp 21 | return b 22 | 23 | t = int(raw_input()) 24 | M = 1000000007 25 | while t > 0: 26 | s = raw_input() 27 | tmp = s.split(" ") 28 | a = int(tmp[0]) 29 | b = int(tmp[1]) 30 | n = int(tmp[2]) 31 | t = t-1 32 | y = abs(a-b) 33 | if a == b: 34 | print (exp(a%M, n, M) + exp(b%M, n, M))%M 35 | continue 36 | if y == 1: 37 | print ("1") 38 | continue 39 | x = (exp(a%y, n, y) + exp(b%y, n, y))%y 40 | print (gcd(x, y)%M) -------------------------------------------------------------------------------- /CodeChef/Solved Problems/ICPC16F.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct element{ 4 | int node; 5 | int value; 6 | }; 7 | bool comp(element a, element b) 8 | { 9 | return a.valueN) 17 | { 18 | printf("%d\n", -1); 19 | return; 20 | } 21 | if(!((N*d)<=M && M<=(N*D))) 22 | { 23 | printf("%d\n", -1); 24 | return; 25 | } 26 | element A[N]; 27 | for(i=0; i>N; 9 | long long A[N]; 10 | set S; 11 | for(int i=0; i>A[i]; 12 | for(int i=0; i :: iterator it=S.lower_bound(A[i]); 14 | if(S.empty() || *(S.begin())>=A[i]){ 15 | cout<<"-1\n"; 16 | } 17 | else 18 | { 19 | --it; 20 | cout<<*it<> N >> C; 8 | int st = 1; 9 | for(int i = 1; i <= N; i += 400){ 10 | cout << 1 << " " << i << endl; 11 | int tmp; 12 | cin >> tmp; 13 | if(tmp == 1){ 14 | cout << "2" << endl; 15 | break; 16 | } 17 | else 18 | st = i; 19 | } 20 | for(int i = st; i <= (st + 400) && i <= N; ++i) 21 | { 22 | cout << 1 << " " << i << endl; 23 | int tmp; 24 | cin >> tmp; 25 | if(tmp == 1){ 26 | cout << 3 << " " << i << endl; 27 | break; 28 | } 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /CodeChef/Solved Problems/MIXCOLOR.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int T,N; 5 | map H; 6 | map :: iterator it; 7 | int main() 8 | { 9 | cin>>T; 10 | while(T--) 11 | { 12 | int N,tmp; 13 | cin>>N; 14 | H.clear(); 15 | for(int i=1; i<=N; i++) 16 | { 17 | cin>>tmp; 18 | if(H.find(tmp)!=H.end()) 19 | H[tmp]++; 20 | else 21 | H[tmp] = 1; 22 | } 23 | long long ret = 0; 24 | for(it = H.begin(); it!=H.end(); ++it) 25 | { 26 | ret = ret + (it->second); 27 | ret -= 1; 28 | } 29 | cout<0: 3 | inp=raw_input().split(" ") 4 | W=int(inp[0]) 5 | S=str(inp[1]) 6 | pos=-1 7 | if S=="mon": 8 | pos=0 9 | elif S=="tues": 10 | pos=1 11 | elif S=="wed": 12 | pos=2 13 | elif S=="thurs": 14 | pos=3 15 | elif S=="fri": 16 | pos=4 17 | elif S=="sat": 18 | pos=5 19 | else: 20 | pos=6 21 | A=[] 22 | for i in range(0,7): 23 | A.append(0) 24 | for i in range(1,W+1): 25 | xxx=(pos+i-1)%7 26 | A[xxx]=A[xxx]+1 27 | ret="" 28 | for x in A: 29 | ret=ret+str(x)+" " 30 | print ret 31 | T=T-1 -------------------------------------------------------------------------------- /CodeChef/Solved Problems/SPC1602.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAX=1000005; 4 | int A[MAX]; 5 | long long DP[MAX]; 6 | long long cum[MAX]; 7 | int main() 8 | { 9 | int N; 10 | cin>>N; 11 | for(int i=1; i<=N; i++) 12 | { 13 | int tmp; 14 | cin>>tmp; 15 | A[tmp]++; 16 | } 17 | long long cnt=0; 18 | for(int i=N; i>0; i--) 19 | { 20 | if(A[i]==0){ 21 | cnt=0; 22 | } 23 | else 24 | { 25 | DP[i]=cnt; 26 | cnt+=A[i]; 27 | } 28 | } 29 | for(int i=1; i<=N; i++)cum[i]=cum[i-1]+A[i]; 30 | long long ANS=INT_MAX; 31 | for(int i=1; i<=N; i++) 32 | { 33 | if(A[i]==0)continue; 34 | if(cum[i]==i){ 35 | ANS=min(ANS,cum[i]); 36 | } 37 | else if(cum[i]9: 3 | n=1 4 | while N>0: 5 | dig=N%10 6 | N=N/10 7 | if dig!=0: 8 | n=n*dig 9 | N=n; 10 | print N -------------------------------------------------------------------------------- /CodeChef/Solved Problems/SPC1604.py: -------------------------------------------------------------------------------- 1 | #def chk(X): 2 | # prev=X%10 3 | # X=X/10 4 | # while X>0: 5 | # dig=X%10 6 | # if dig>=prev: 7 | # return 0 8 | # else: 9 | # X=X/10 10 | # prev=dig 11 | # return 1 12 | #N = input() 13 | 14 | #if N==0: 15 | # print "-1" 16 | #else: 17 | # ans=0 18 | # for i in range(pow(10,N-1),pow(10,N)): 19 | # if chk(i)==1: 20 | # ans=ans+i 21 | # 22 | # print ans 23 | 24 | N=input() 25 | if N==0: 26 | print -1 27 | elif N==1: 28 | print 45 29 | elif N==2: 30 | print 1440 31 | elif N==3: 32 | print 25830 33 | elif N==4: 34 | print 310968 35 | elif N==5: 36 | print 2592450 37 | elif N==6: 38 | print 14814720 39 | else: 40 | print -1 -------------------------------------------------------------------------------- /CodeChef/Solved Problems/WOUT.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin>>T; 8 | while(T--) 9 | { 10 | int N,H; 11 | cin>>N>>H; 12 | long long int A[N + 2]; 13 | for(int i=0; i<=(N + 1); i++) 14 | A[i] = 0; 15 | for(int i=1; i<=N; i++) 16 | { 17 | int l,h; 18 | cin>>l>>h; 19 | int low = N - h; 20 | int high = N - l; 21 | A[low]++; 22 | A[high + 1]--; 23 | } 24 | for(int i=1; i<=(N+1); i++) 25 | A[i] += A[i-1]; 26 | for(int i=1; i<=(N+1); i++) 27 | A[i] += A[i-1]; 28 | long long ret = LONG_LONG_MAX; 29 | for(int i=H; i<=N; i++) 30 | ret = min(ret, (H*1LL*N) - (A[i] - A[i - H])); 31 | cout<>N; 10 | for(int i=0; i>S; 13 | int L = S.size(); 14 | int p = -1; 15 | for(int j=0; j>S; 32 | int L = S.size(); 33 | int p = -1; 34 | for(int j=0; j>N; 27 | for(int i=1; i<=N; i++) 28 | cin>>A[i]; 29 | comb(); 30 | cum[1] = dp[1]; 31 | for(int i = 2; i<=N; i++) 32 | { 33 | for(int j = i - 1; j >= 1; j--) 34 | { 35 | if((A[j] -1) >= 0 && (A[j]-1) < MAXN) 36 | dp[i] = ((dp[i]%MOD) + ((((cum[j - 1] + 1)%MOD)*pas[i - j - 1][A[j] - 1])%MOD))%MOD; 37 | } 38 | cum[i] = (cum[i - 1] + dp[i])%MOD; 39 | } 40 | cout<>N; 10 | int ret = 0; 11 | for(int i=1; i<=N; i++) 12 | { 13 | int tmp; 14 | cin>>tmp; 15 | A[tmp]++; 16 | ret = max(ret, A[tmp]); 17 | } 18 | cout<>N>>K; 10 | long double ans = 0.0; 11 | for(int i=0; i>A[i]; 13 | for(int i=0; i= K) 21 | { 22 | long double lol = (sum * 1.0) / (k * 1.0); 23 | if(lol > ans) 24 | ans = lol; 25 | } 26 | } 27 | } 28 | cout<>N>>D; 9 | long long A[N]; 10 | for(int i=0; i>A[i]; 12 | sort(A, A + N); 13 | int ans = 2; 14 | for(int i = 0; i < N - 1; i++) 15 | { 16 | long long pt = A[i] + D; 17 | if(!((A[i + 1] - pt) < D))ans++; 18 | long long pt2 = A[i + 1] - D; 19 | if(pt2 != pt && ((pt2 - A[i])>=D)) 20 | ans++; 21 | } 22 | cout<>N>>M; 8 | for(int i=1; i<=M; i++) 9 | { 10 | int L,R; 11 | cin>>L>>R; 12 | } 13 | for(int i = 1; i <= N; i++) 14 | { 15 | cout << (i % 2); 16 | } 17 | cout< H; 5 | 6 | int main() 7 | { 8 | int N; 9 | cin>>N; 10 | long long A[N]; 11 | for(int i=0; i>A[i]; 14 | } 15 | set S; 16 | long long cnt[N]; 17 | for(int i = N-1; i>=0; i--) 18 | { 19 | S.insert(A[i]); 20 | cnt[i] = S.size(); 21 | } 22 | long long ret = 0; 23 | for(int i=0; i>N; 9 | for(int i = 0; i < N; i++) 10 | { 11 | long long tmp; 12 | cin>>tmp; 13 | if((tmp%2) == 0) 14 | cout<<(tmp - 1); 15 | else 16 | cout<<(tmp); 17 | cout<<" "; 18 | } 19 | cout<>N; 8 | long long A[N]; 9 | for(int i = 0; i < N; i++) 10 | { 11 | cin>>A[i]; 12 | } 13 | long long cum[N]; 14 | cum[0] = A[0]; 15 | for(int i = 1; i < N; i++){ 16 | cum[i] = cum[i - 1] + A[i]; 17 | } 18 | long long ans = 0; 19 | long long tmp = 0; 20 | for(int i = N - 1; i >= 0; i--){ 21 | tmp += A[i]; 22 | int low = 0; int high = i - 1; 23 | while(high >= low){ 24 | int mid = (high + low)/2; 25 | if(cum[mid] == tmp){ 26 | ans = tmp; 27 | break; 28 | } 29 | else if(cum[mid] >= tmp){ 30 | high = mid - 1; 31 | } 32 | else 33 | low = mid + 1; 34 | } 35 | } 36 | cout<>N; 10 | cin>>A; 11 | cin>>B; 12 | int ret = 0; 13 | for(int i = 0; i < N; i++) 14 | { 15 | if(i == (N/2) && (N % 2)==0)break; 16 | if(i == (N/2)){ 17 | ret = ret + 1 - (A[i] == B[i]); 18 | break; 19 | } 20 | int idx = N - 1 - i; 21 | if(A[idx] == A[i] && B[idx] == B[i])continue; 22 | if(((A[idx] == B[i]) && (A[i] == B[idx])) || ((A[idx] == B[idx]) && (A[i] == B[i])))continue; 23 | //A[idx] matches with B[idx] && A[i] mathes with B[i] 24 | int add = INT_MAX; 25 | if((A[idx] != A[i]) && (B[idx] == B[i])){ 26 | add = min(add, 1); 27 | //continue; 28 | } 29 | /*if((A[idx] == A[i]) && (B[idx] != B[i])){ 30 | 31 | continue; 32 | }*/ 33 | add = min(add, 1 - (A[idx] == B[idx]) + 1 - (A[i] == B[i])); 34 | add = min(add, 1 - (A[idx] == B[i]) + 1 - (A[i] == B[idx])); 35 | ret += add; 36 | } 37 | cout<>N; 15 | power[0] = 1; 16 | for(int i = 1; i < MAXN; i++) 17 | power[i] = (power[i - 1] * 2) % MOD; 18 | for(int i = 0; i < N; i++) 19 | cin>>A[i]; 20 | DP2[0] = (A[0] * power[N - 1]) % MOD; 21 | for(int i = 1; i < N; i++) 22 | DP2[i] = (DP2[i - 1] + (A[i] * power[N - 1 - i]) % MOD) % MOD; 23 | DP1[0] = 0; 24 | DP1[1] = (A[0] * power[N - 1]) % MOD; 25 | for(int i = 2; i <= N; i++) 26 | { 27 | DP1[i] = (((((2 * DP1[i - 1]) % MOD + DP2[i - 2]) % MOD + (A[i - 1] * power[N - 1 - (i - 2)]) % MOD) % MOD) * 499122177) % MOD; 28 | } 29 | cout<>N>>K; 10 | for(int i = 0; i < N; i++) 11 | cin>>A[i]; 12 | int cnt = 0; int last = -2; 13 | sort(A, A + N); 14 | int ret = 0; 15 | for(int i = 0; i < N; i++) 16 | { 17 | if((A[i] - 'a') - last >= 2) 18 | { 19 | last = A[i] - 'a'; 20 | cnt++; 21 | ret = ret + (A[i] - 'a') + 1; 22 | if(cnt == K) 23 | break; 24 | } 25 | } 26 | if(cnt == K) 27 | cout<= N) 18 | return true; 19 | return false; 20 | } 21 | 22 | int main() 23 | { 24 | cin>>N>>M; 25 | for(int i = 0; i < M; i++) 26 | cin>>A[i]; 27 | sort(A, A + M); 28 | //cout<= low) 31 | { 32 | int mid = (high + low)/2; 33 | if(chk(mid)){ 34 | ans = mid; 35 | low = mid + 1; 36 | } 37 | else 38 | high = mid - 1; 39 | } 40 | cout<>N>>K; 9 | long long g = 0; 10 | for(int i = 1; i <= N; i++) 11 | { 12 | long long x; 13 | cin>>x; 14 | g = __gcd(g, x); 15 | } 16 | set S; 17 | set :: iterator it; 18 | for(int i = 0; i < K; i++){ 19 | S.insert( (i * 1LL * g) % K); 20 | } 21 | cout<>N; 8 | int A[N]; 9 | int net = 0; 10 | for(int i = 0; i < N; i++) 11 | cin>>A[i]; 12 | for(int i = 0; i < N; i++) 13 | { 14 | int x; 15 | cin>>x; 16 | net = net + (x - A[i]); 17 | } 18 | if(net <= 0) 19 | cout<<"Yes\n"; 20 | else 21 | cout<<"No\n"; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1013C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 2e5 + 10; 4 | int N; 5 | long long A[MAXN]; 6 | 7 | int main() 8 | { 9 | cin>>N; 10 | for(int i = 0; i < 2 * N; i++) 11 | cin>>A[i]; 12 | sort(A, A + 2 * N); 13 | long long area = abs(A[0] - A[N - 1]) * abs(A[N] - A[2 * N - 1]); 14 | for(int i = N; i < 2 * N - 1; i++){ 15 | area = min(area, abs(A[0] - A[ 2 * N - 1]) * abs(A[i] - A[i - (N - 1)])); 16 | } 17 | cout<>N>>M; 10 | for(int i = 0; i < N; i++){ 11 | int l,r; 12 | cin>>l>>r; 13 | for(int j = l; j <= r; j++) 14 | A[j] = 1; 15 | } 16 | int ans = 0; 17 | for(int i = 1; i <= M; i++) 18 | ans = ans + (1 - A[i]); 19 | cout< Y.diff; 17 | } 18 | 19 | int main() 20 | { 21 | cin>>N>>M; 22 | long long mini = 0; 23 | long long curr = 0; 24 | for(int i = 0; i < N; i++){ 25 | cin>>A[i].a>>A[i].b; 26 | mini = mini + A[i].b; 27 | curr += A[i].a; 28 | A[i].diff = A[i].a - A[i].b; 29 | } 30 | sort(A, A + N, cmp); 31 | if(mini > M) 32 | { 33 | cout<<"-1\n"; 34 | return 0; 35 | } 36 | int ans = 0; 37 | for(int i = 0; i < N; i++){ 38 | if(curr > M){ 39 | ans++; 40 | curr -= A[i].diff; 41 | } 42 | } 43 | cout<>N>>K>>S; 9 | long long tmp = S / K; 10 | long long rem = S % K; 11 | int chk = (rem == 0)? 0 : 1; 12 | if((tmp + chk) > (N - 1) || (tmp <= 0)) 13 | { 14 | cout<<"NO\n"; 15 | return 0; 16 | } 17 | cout<<"YES\n"; 18 | long long curr = 1; 19 | for(long long i = 1; i <= rem; i++){ 20 | if((i % 2) == 0){ 21 | curr = curr - (tmp + 1); 22 | } 23 | else 24 | { 25 | curr = curr + (tmp + 1); 26 | } 27 | cout<> L; 10 | for(int i = 0; i < L; i++){ 11 | char x; 12 | cin >> x; 13 | A[x - 'a']++; 14 | } 15 | int flag = 0; 16 | if(L == 1){ 17 | cout << "Yes\n"; 18 | return 0; 19 | } 20 | for(int i = 0; i < 26; i++){ 21 | if(A[i] > 1) 22 | { 23 | flag = 1; 24 | break; 25 | } 26 | } 27 | if(flag) 28 | cout << "Yes\n"; 29 | else 30 | cout << "No\n"; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1027A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int L; 11 | cin >> L; 12 | string S; 13 | cin>>S; 14 | char A[L]; 15 | char B[L]; 16 | for(int i = 0; i < L; i++){ 17 | if(S[i] == 'a') 18 | { 19 | A[i] = 'b'; 20 | B[i] = 'b'; 21 | } 22 | else if(S[i] == 'z') 23 | { 24 | A[i] = 'y'; 25 | B[i] = 'y'; 26 | } 27 | else 28 | { 29 | A[i] = (char)(S[i] - 1); 30 | B[i] = (char)(S[i] + 1); 31 | } 32 | } 33 | int cnt = L - 1; 34 | int flag = 1; 35 | for(int i = 0; i < L; i++){ 36 | if(A[i] == A[cnt] || A[i] == B[cnt] || B[i] == A[cnt] || B[i] == B[cnt]) 37 | { 38 | cnt--; 39 | continue; 40 | } 41 | cnt--; 42 | flag = 0; 43 | break; 44 | } 45 | if(flag) 46 | cout<<"YES\n"; 47 | else 48 | cout<<"NO\n"; 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1029A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, K; 7 | string S; 8 | cin >> N >> K; 9 | cin >> S; 10 | int prefix = 0; 11 | for(int i = 1; i < N; i++){ 12 | int pt1 = 0; 13 | int pt2 = i; 14 | while(pt2 < N && S[pt1] == S[pt2]) 15 | { 16 | pt1++; 17 | pt2++; 18 | } 19 | if(pt2 == N) 20 | prefix = max(prefix, pt1); 21 | } 22 | //cout << prefix << endl; 23 | string ret = S; 24 | for(int i = 1; i < K; i++){ 25 | for(int j = prefix; j < N; j++) 26 | ret = ret + S[j]; 27 | } 28 | cout << ret << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1029B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | long long int A[N]; 9 | for(int i = 0; i < N; i++) 10 | cin >> A[i]; 11 | int DP[N]; 12 | for(int i = 0; i < N; i++) 13 | DP[i] = 0; 14 | DP[N - 1] = 1; 15 | for(int i = N - 2; i >= 0; i--) 16 | { 17 | long long val = A[i]; 18 | int low = i; int high = N - 1; 19 | int mid; int pos = i; 20 | while(high >= low) 21 | { 22 | mid = (high + low) / 2; 23 | if(A[mid] > (2 * val)) 24 | { 25 | high = mid - 1; 26 | } 27 | else 28 | { 29 | low = mid + 1; 30 | pos = mid; 31 | } 32 | } 33 | //cout << pos << endl; 34 | if(pos == i) 35 | DP[i] = 1; 36 | else 37 | DP[i] = (pos - i) + DP[pos]; 38 | } 39 | int ret = 0; 40 | for(int i = 0; i < N; i++) 41 | ret = max(ret, DP[i]); 42 | cout << ret << endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1047A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long N; 7 | cin >> N; 8 | if((N - 2) %3 != 0){ 9 | cout << "1 1 "<<(N - 2)<> N; 22 | for(int i = 0; i < N; i++) 23 | cin >> X[i] >> Y[i]; 24 | long long low = 0; long long high = 2e9; 25 | long long mid; 26 | long long ans = -1; 27 | while(high >= low) 28 | { 29 | mid = (high + low) / 2; 30 | if(chk(mid)){ 31 | ans = mid; 32 | high = mid - 1; 33 | } 34 | else 35 | low = mid + 1; 36 | } 37 | cout << ans << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1056A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 105; 4 | int A[MAXN][MAXN]; 5 | int N, T; 6 | int main() 7 | { 8 | cin >> N; 9 | for(int i = 1; i <= N; ++i){ 10 | cin >> T; 11 | for(int j = 1; j <= T; ++j) 12 | { 13 | int tmp; 14 | cin >> tmp; 15 | A[i][tmp] = 1; 16 | } 17 | } 18 | for(int i = 1; i < MAXN; ++i){ 19 | int flag = 1; 20 | for(int j = 1; j <= N; ++j){ 21 | if(A[j][i] == 0){ 22 | flag = 0; 23 | break; 24 | } 25 | } 26 | if(flag) 27 | cout << i << " "; 28 | } 29 | cout << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1056B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int N, M; 4 | int main() 5 | { 6 | cin >> N >> M; 7 | long long ret = 0; 8 | for(int i = 0; i < M; ++i){ 9 | for(int j = 0; j < M; ++j){ 10 | if(i > N || j > N) 11 | continue; 12 | if((((i * i) % M + (j * j) % M) % M) == 0) 13 | { 14 | long long k = (N - i) / M; 15 | long long l = (N - j) / M; 16 | if(i != 0) 17 | k++; 18 | if(j != 0) 19 | l++; 20 | ret = ret + (k * l); 21 | } 22 | } 23 | } 24 | cout << ret << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1081A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | long long int K; 9 | cin >> K; 10 | if(K == 2){ 11 | cout << "2\n"; 12 | } 13 | else 14 | cout << "1\n"; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1081C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const long long MOD = 998244353; 4 | const int MAXN = 2e3 + 5; 5 | long long int N, M, K; 6 | long long DP[MAXN][MAXN]; 7 | 8 | long long power(long long A, long long B) 9 | { 10 | long long p = 1; 11 | for(int i = 1; i <= B; ++i) 12 | p = (p * A) % MOD; 13 | return p; 14 | } 15 | 16 | int main() 17 | { 18 | ios_base::sync_with_stdio(false); 19 | cin.tie(NULL); 20 | for(int i = 0; i < MAXN; ++i) 21 | DP[i][0] = 1; 22 | cin >> N >> M >> K; 23 | 24 | for(int i = 1; i < MAXN; ++i) 25 | { 26 | for(int j = 1; j < MAXN; ++j) 27 | DP[i][j] = (DP[i - 1][j] + DP[i - 1][j - 1]) % MOD; 28 | } 29 | cout << (((M * power(M - 1, K)) % MOD) * DP[N - 1][K]) % MOD << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1082A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long N, X, Y, D; 11 | cin >> N >> X >> Y >> D; 12 | long long res = -1; 13 | if((Y - X) % D == 0) 14 | { 15 | res = abs((Y - X) / D); 16 | } 17 | if((Y - 1) % D == 0) 18 | { 19 | if(res == -1){ 20 | res = abs((long long)ceil((X - 1) * 1.0 / D)) + (Y - 1) / D; 21 | } 22 | else 23 | res = min(res, abs((long long)ceil((X - 1) * 1.0 / D)) + (Y - 1) / D); 24 | } 25 | if((N - Y) % D == 0) 26 | { 27 | if(res == -1) 28 | res = abs((long long)ceil((N - X) * 1.0 / D)) + (N - Y) / D; 29 | else 30 | res = min(res, abs((long long)ceil((N - X) * 1.0 / D)) + (N - Y) / D); 31 | } 32 | cout << res << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1084A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | long long int A[N + 5]; 9 | for(int i = 1; i <= N; ++i) 10 | cin >> A[i]; 11 | long long res = 100000000000000000; 12 | for(int i = 1; i <= N; ++i) 13 | { 14 | long long sum = 0; 15 | int x = i; 16 | for(int j = 1; j <= N; ++j) 17 | sum = sum + (A[j] * 2 * (abs(j - 1) + abs(x - j) + abs(x - 1))); 18 | res = min(res, sum); 19 | } 20 | cout << res << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1084B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | long long A[1005]; 4 | int N; 5 | long long int S; 6 | bool chk(long long K) 7 | { 8 | long long sum = 0; 9 | for(int i = 0; i < N; ++i) 10 | { 11 | if(A[i] < K) 12 | return false; 13 | else 14 | sum = sum + abs(A[i] - K); 15 | } 16 | if(sum >= S) 17 | return true; 18 | return false; 19 | } 20 | int main() 21 | { 22 | 23 | cin >> N >> S; 24 | 25 | for(int i = 0; i < N; ++i) 26 | cin >> A[i]; 27 | long long low = 0; 28 | long long high = 1000000000000; 29 | long long mid; 30 | long long ans = -1; 31 | while(high >= low) 32 | { 33 | mid = (high+ low) / 2; 34 | if(chk(mid)) 35 | { 36 | low = mid + 1; 37 | ans = mid; 38 | } 39 | else{ 40 | high = mid - 1; 41 | } 42 | } 43 | cout << ans << endl; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1087A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string S; 7 | cin >> S; 8 | int L = S.size(); 9 | string ans = ""; 10 | int left_ptr = 0; int right_ptr = L - 1; 11 | int cnt = 0; 12 | int mod = 0; 13 | if((L % 2) != 0) 14 | mod = 1; 15 | while(cnt != L) 16 | { 17 | if((cnt % 2) == mod){ 18 | ans = S[right_ptr--] + ans; 19 | } 20 | else 21 | { 22 | ans = S[left_ptr++] + ans; 23 | } 24 | cnt++; 25 | } 26 | cout << ans << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1087B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e6 + 5; 4 | int main() 5 | { 6 | long long int N, K; 7 | cin >> N >> K; 8 | long long int res = 1000000000000000000; 9 | for(int i = 1; i * i <= N; ++i){ 10 | if((N % i) == 0) 11 | { 12 | long long rem = i; 13 | long long q = N / i; 14 | 15 | long long x = q * K; 16 | for(long long int j = x; (j / K) == q; j++) 17 | { 18 | if((j / K) * (j % K) == N) 19 | res = min(res, j); 20 | } 21 | 22 | rem = N / i; 23 | q = i; 24 | x = q * K; 25 | for(long long int j = x; (j / K) == q; j++) 26 | { 27 | if((j / K) * (j % K) == N) 28 | res = min(res, j); 29 | } 30 | } 31 | } 32 | cout << res << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1087D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e5 + 5; 4 | int N; 5 | long double K; 6 | vector edges[MAXN]; 7 | int main() 8 | { 9 | cin >> N >> K; 10 | for(int i = 1; i < N; ++i){ 11 | int u, v; 12 | cin >> u >> v; 13 | edges[u].push_back(v); 14 | edges[v].push_back(u); 15 | } 16 | long double c = 0; 17 | for(int i = 1; i <= N; ++i){ 18 | if(edges[i].size() == 1){ 19 | c++; 20 | } 21 | } 22 | long double tmp = K / c; 23 | tmp *= 2; 24 | cout << fixed << setprecision(9) << tmp << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1088A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int X; 7 | cin >> X; 8 | 9 | for(int i = 1; i <= X; i++) 10 | { 11 | for(int j = 1; j <= X; j++){ 12 | int A = i; 13 | int B = j; 14 | 15 | if(((A * B) > X) && ((A / B) < X) && (A % B) == 0) 16 | { 17 | cout << A << " " << B << endl; 18 | return 0; 19 | } 20 | } 21 | } 22 | cout << "-1\n"; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1088B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e5 + 5; 4 | int N, K; 5 | long long A[MAXN]; 6 | int main() 7 | { 8 | cin >> N >> K; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | sort(A, A + N); 12 | long long int diff = 0; 13 | int done = 0; 14 | for(int i = 0; i < N; i++){ 15 | long long tmp = A[i]; 16 | tmp = tmp - diff; 17 | if(tmp == 0) 18 | continue; 19 | done++; 20 | diff = diff + tmp; 21 | cout << tmp << endl; 22 | if(done == K) 23 | break; 24 | } 25 | for(int i = done; i < K; ++i) 26 | cout << "0\n"; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1088C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | long long A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | cout << (N + 1) << endl; 12 | cout << "2 " << N << " " << "1"<> N; 12 | 13 | int global = 0; 14 | for(int i = 0; i < N; ++i){ 15 | cin >> A[i]; 16 | int tmp; 17 | for(int j = 1; j <= A[i]; ++j) 18 | { 19 | cin >> tmp; 20 | MAX[i] = max(MAX[i], tmp); 21 | } 22 | global = max(global, MAX[i]); 23 | } 24 | long long res = 0; 25 | for(int i = 0; i < N; ++i){ 26 | if(MAX[i] == global) 27 | continue; 28 | res = res + ((global - MAX[i]) * 1LL * A[i]); 29 | } 30 | cout << res << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1090M.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, K; 7 | cin >> N >> K; 8 | int DP[N]; 9 | DP[0] = 1; 10 | int prev; 11 | cin >> prev; 12 | for(int i = 1; i < N; ++i){ 13 | int tmp; 14 | cin >> tmp; 15 | if(tmp != prev) 16 | { 17 | DP[i] = DP[i - 1] + 1; 18 | } 19 | else{ 20 | DP[i] = 1; 21 | } 22 | prev = tmp; 23 | } 24 | int res = 0; 25 | for(int i = 0; i < N; ++i) 26 | res = max(res, DP[i]); 27 | cout << res << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1091A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long int A, B, C; 7 | cin >> A >> B >> C; 8 | long long ans = 0; 9 | // fix A 10 | if(B >= (A + 1) && C >= (A + 2)) 11 | ans = max(ans, A + A + 1 + A + 2); 12 | // fix B 13 | if(A >= (B - 1) && C >= (B + 1)) 14 | ans = max(ans, B - 1 + B + B + 1); 15 | // fix C 16 | if(B >= (C - 1) && A >= (C - 2)) 17 | ans = max(ans, C + C - 1 + C - 2); 18 | cout << ans << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1091B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e3 + 5; 4 | int N; 5 | map , int > H; 6 | long long int X[MAXN]; 7 | long long int Y[MAXN]; 8 | long long cx[MAXN]; 9 | long long cy[MAXN]; 10 | int main() 11 | { 12 | cin >> N; 13 | for(int i = 0; i < N; ++i){ 14 | int x, y; 15 | cin >> x >> y; 16 | cx[i] = x; 17 | cy[i] = y; 18 | H[make_pair(x, y)] = 1; 19 | } 20 | for(int i = 0; i < N; ++i){ 21 | cin >> X[i] >> Y[i]; 22 | } 23 | for(int i = 0; i < N; ++i){ 24 | long long Tx = X[0] + cx[i]; 25 | long long Ty = Y[0] + cy[i]; 26 | int fl = 1; 27 | for(int j = 0; j < N; ++j){ 28 | if(H.find(make_pair(Tx - X[j], Ty - Y[j])) == H.end()){ 29 | fl = 0; 30 | break; 31 | } 32 | } 33 | if(fl){ 34 | cout << Tx << " " << Ty << endl; 35 | return 0; 36 | } 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1091C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | long long int eval(long long N, long long D) 5 | { 6 | return N * (2 + (N - 1) * D) / 2; 7 | } 8 | 9 | int main() 10 | { 11 | long long int N; 12 | cin >> N; 13 | set S; 14 | for(long long int i = 1; i * i <= N; ++i){ 15 | if((N % i) == 0){ 16 | S.insert(eval(N / i, i)); 17 | S.insert(eval(i, N / i)); 18 | } 19 | } 20 | set :: iterator it; 21 | for(it = S.begin(); it != S.end(); ++it) 22 | cout << *it << " "; 23 | cout << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1091D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e6 + 5; 4 | const long long MOD = 998244353; 5 | long long N; 6 | long long DP[MAXN]; 7 | // Mod Inverse Code 8 | long long modExp(long long a, long long b, long long p) 9 | { 10 | if(b==0){ 11 | return 1; 12 | } 13 | if(b==1)return a; 14 | long long ttt=modExp(a,b/2,p); 15 | ttt=(ttt*ttt)%p; 16 | if((b%2)==0)return ttt; 17 | return (ttt*a)%p; 18 | } 19 | long long modInv(long long aaa, long long bbb) 20 | { 21 | return modExp(aaa,bbb-2,bbb); 22 | } 23 | /*****************************************/ 24 | int main() 25 | { 26 | cin >> N; 27 | DP[0] = 1; 28 | for(long long int i = 1; i < MAXN; ++i) 29 | DP[i] = (DP[i - 1] * i) % MOD; 30 | long long res = DP[N]; 31 | for(long long int i = 2; i < N; ++i){ 32 | res = (res + ((((DP[N] * (DP[i] - 1)) % MOD) * modInv(DP[i], MOD)) % MOD)) % MOD; 33 | } 34 | cout << res << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1092A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N, K; 11 | cin >> N >> K; 12 | int cnt = 0; 13 | char A[N]; 14 | for(int i = 0; i < N; ++i) 15 | { 16 | A[i] = (char)(97 + (cnt % K)); 17 | cnt++; 18 | } 19 | for(int i = 0; i < N; ++i) 20 | cout << A[i]; 21 | cout << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1092B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | sort(A, A + N); 12 | int res = 0; 13 | for(int i = 1; i < N; i += 2){ 14 | res = res + (A[i] - A[i - 1]); 15 | } 16 | cout << res << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1093B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | string S; 11 | cin >> S; 12 | int L = S.size(); 13 | char A[L]; 14 | int fine = 0; 15 | for(int i = 1; i < L; ++i) 16 | { 17 | if(S[i] != S[i - 1]) 18 | fine = 1; 19 | } 20 | if(fine == 0){ 21 | cout << "-1\n"; 22 | continue; 23 | } 24 | for(int i = 0; i < L; ++i) 25 | A[i] = S[i]; 26 | sort(A, A + L); 27 | for(int i = 0; i < L; ++i) 28 | cout << A[i]; 29 | cout << endl; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1093C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | long long int A[N]; 9 | int cnt = N - 1; 10 | long long int tmp; 11 | cin >> tmp; 12 | A[cnt] = tmp; 13 | A[0] = 0; 14 | cnt--; 15 | for(int i = 1; i < (N / 2); ++i) 16 | { 17 | cin >> tmp; 18 | A[cnt] = min(tmp, A[cnt + 1]); 19 | A[i] = tmp - A[cnt]; 20 | if(A[i] < A[i - 1]) 21 | { 22 | A[i] = A[i - 1]; 23 | A[cnt] = tmp - A[i]; 24 | } 25 | cnt--; 26 | } 27 | for(int i = 0; i < N; ++i) 28 | cout << A[i] << " "; 29 | cout << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1095A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | char A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | int cnt = 1; 12 | for(int i = 0; i < N; i += cnt){ 13 | cout << A[i]; 14 | cnt++; 15 | } 16 | cout << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1095B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | long long int A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | sort(A, A + N); 12 | if(N <= 2){ 13 | cout << "0\n"; 14 | return 0; 15 | } 16 | long long int ans = min(A[N - 1] - A[1], A[N - 2] - A[0]); 17 | cout << ans << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1096A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long int L, R; 11 | cin >> L >> R; 12 | cout << L << " " << (2 * L) << endl; 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1097A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char x, y; 7 | cin >> x >> y; 8 | int flag = 0; 9 | for(int i = 1; i <= 5; ++i){ 10 | char X, Y; 11 | cin >> X >> Y; 12 | if(x == X || y == Y){ 13 | flag = 1; 14 | } 15 | } 16 | if(flag) 17 | cout << "YES\n"; 18 | else 19 | cout << "NO\n"; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1097B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 20; 4 | int N; 5 | long long int A[MAXN]; 6 | int flag; 7 | void rec(int ind, long long int sum) 8 | { 9 | if(ind == N) 10 | { 11 | if(sum % 360 == 0) 12 | { 13 | flag = 1; 14 | return; 15 | } 16 | return; 17 | } 18 | rec(ind + 1, sum + A[ind]); 19 | rec(ind + 1, sum - A[ind]); 20 | } 21 | 22 | 23 | int main() 24 | { 25 | cin >> N; 26 | for(int i = 0; i < N; ++i) 27 | cin >> A[i]; 28 | rec(0, 0); 29 | if(flag) 30 | cout << "YES\n"; 31 | else 32 | cout << "NO\n"; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1099A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int W, H; 7 | cin >> W >> H; 8 | int u1, d1; 9 | int u2, d2; 10 | cin >> u1 >> d1; 11 | cin >> u2 >> d2; 12 | for(int i = H; i >= 0; i--) 13 | { 14 | W += i; 15 | if(i == d1) 16 | W -= u1; 17 | else if(i == d2) 18 | W -= u2; 19 | W = max(0, W); 20 | } 21 | cout << W << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1099B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long N; 7 | cin >> N; 8 | long long res = 0; 9 | for(long long i = 1; i * i <= N; ++i){ 10 | res = i; 11 | } 12 | long long int add = 0; 13 | if(res * res < N){ 14 | add = (N - (res * res)); 15 | add = ceil(add * 1.0/ res); 16 | add = int(add); 17 | } 18 | res *= 2; 19 | res += add; 20 | cout << res << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1100A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int N, K; 9 | cin >> N >> K; 10 | int A[N]; 11 | int test = 0; 12 | int social = 0; 13 | for(int i = 0; i < N; ++i){ 14 | cin >> A[i]; 15 | if(A[i] == 1) 16 | test++; 17 | else 18 | social++; 19 | } 20 | int res = 0; 21 | for(int i = 0; i < K; ++i){ 22 | int tmp_social = social; 23 | int tmp_test = test; 24 | for(int j = i; j < N; j += K){ 25 | if(A[j] == -1) 26 | tmp_social--; 27 | else 28 | tmp_test--; 29 | } 30 | res = max(res, abs(tmp_social - tmp_test)); 31 | } 32 | cout << res << "\n"; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1100B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e5 + 5; 4 | int N, M; 5 | int A[MAXN]; 6 | int freq[MAXN]; 7 | set S; 8 | int main() 9 | { 10 | ios_base::sync_with_stdio(false); 11 | cin.tie(NULL); 12 | cin >> N >> M; 13 | for(int i = 0; i < M; ++i) 14 | cin >> A[i]; 15 | for(int i = 0; i < M; ++i){ 16 | S.insert(A[i]); 17 | freq[A[i]]++; 18 | if(S.size() == N){ 19 | set :: iterator it; 20 | vector pres; 21 | for(it = S.begin(); it != S.end(); ++it){ 22 | pres.push_back(*it); 23 | } 24 | S.clear(); 25 | for(int j = 0; j < N; ++j){ 26 | freq[pres[j]]--; 27 | if(freq[pres[j]] > 0) 28 | S.insert(pres[j]); 29 | } 30 | cout << "1"; 31 | } 32 | else 33 | cout << "0"; 34 | } 35 | cout << "\n"; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1100C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | #define PI 3.14159265 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | long double n, r; 10 | cin >> n >> r; 11 | long double angle1 = 360 * 1.0 / n; 12 | long double angle2 = (180 - angle1) * 1.0 / 2; 13 | long double R= r * sin(angle1 * PI / 180) / (2 * sin(angle2 * PI / 180) - sin(angle1 * PI / 180)); 14 | cout << fixed << setprecision(9) << R << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1101A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int Q; 7 | cin >> Q; 8 | while(Q--) 9 | { 10 | long long int L, R, D; 11 | cin >> L >> R >> D; 12 | if(D < L) 13 | cout << D << endl; 14 | else{ 15 | long long tmp = ceil(R * 1.0 / D); 16 | if((R % D) == 0) 17 | tmp++; 18 | cout << tmp * D << endl; 19 | } 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1101E.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int x, y; 4 | int small = 0; 5 | int big = 0; 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int Q; 11 | cin >> Q; 12 | while(Q--) 13 | { 14 | char ch; 15 | cin >> ch; 16 | if(ch == '+'){ 17 | 18 | cin >> x >> y; 19 | if(x < y){ 20 | small = max(small, x); 21 | big = max(big, y); 22 | } 23 | else{ 24 | small = max(small, y); 25 | big = max(big, x); 26 | } 27 | } 28 | else{ 29 | cin >> x >> y; 30 | //cout << big << " " << small << endl; 31 | if(x < y) 32 | swap(x, y); 33 | if(x >= big && y >= small) 34 | cout << "YES\n"; 35 | else 36 | cout << "NO\n"; 37 | } 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1102A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long N; 7 | cin >> N; 8 | if(N == 1){ 9 | cout << "1\n"; 10 | return 0; 11 | } 12 | if(N == 2){ 13 | cout << "1\n"; 14 | return 0; 15 | } 16 | if(N == 3){ 17 | cout << "0\n"; 18 | return 0; 19 | } 20 | int res = 0; 21 | if((N % 2) != 0) 22 | res += 1; 23 | if((N / 2) % 2 != 0) 24 | res += 1; 25 | if(res == 2) 26 | res = 0; 27 | cout << res << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1102C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, X, Y; 7 | cin >> N >> X >> Y; 8 | long long A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | if(X > Y){ 12 | cout << N << endl; 13 | return 0; 14 | } 15 | else{ 16 | int cnt = 0; 17 | for(int i = 0; i < N; ++i){ 18 | if(A[i] <= X) 19 | cnt++; 20 | } 21 | cnt = ceil(cnt * 1.0 / 2); 22 | cout << cnt << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1104A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | cout << N << endl; 9 | for(int i = 1; i <= N; ++i) 10 | cout << "1 "; 11 | cout << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1104B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string S; 7 | cin >> S; 8 | int L = S.size(); 9 | stack stk; 10 | stk.push(S[0]); 11 | int res = 0; 12 | for(int i = 1; i < L; ++i){ 13 | if(stk.empty() == false && stk.top() == S[i]) 14 | { 15 | stk.pop(); 16 | res++; 17 | } 18 | else{ 19 | stk.push(S[i]); 20 | } 21 | } 22 | if((res % 2)) 23 | cout << "Yes\n"; 24 | else 25 | cout << "No\n"; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1105A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | long long int cost = 1000000000000000000; 12 | int t = -1; 13 | for(int i = 1; i <= 105; ++i){ 14 | int tmp = 0; 15 | for(int j = 0; j < N; ++j){ 16 | tmp = tmp + min(abs(i - A[j]), min(abs(i + 1 - A[j]), abs(i - 1 - A[j]))); 17 | } 18 | if(tmp < cost) 19 | { 20 | cost = tmp; 21 | t = i; 22 | } 23 | } 24 | cout << t << " " << cost << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1119A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int A[N]; 9 | for(int i = 0; i < N; ++i){ 10 | cin >> A[i]; 11 | } 12 | int res = 0; 13 | for(int i = 0; i < N; i++){ 14 | //cout << A[i] << endl; 15 | if(A[i] != A[N - 1]){ 16 | //cout << A[i] << endl; 17 | res = max(res, N - 1 - i); 18 | } 19 | if(A[0] != A[i]) 20 | res = max(res, i - 0); 21 | } 22 | cout << res << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1119B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e3 + 5; 4 | int N; 5 | long long H; 6 | long long A[MAXN]; 7 | bool chk(int idx){ 8 | long long tmp[idx]; 9 | for(int i = 0; i < idx; ++i) 10 | tmp[i] = A[i + 1]; 11 | sort(tmp, tmp + idx); 12 | long long ret = 0; 13 | for(int i = idx - 1; i >= 0; i -= 2) 14 | ret += tmp[i]; 15 | if(ret <= H) 16 | return true; 17 | return false; 18 | } 19 | 20 | int main() 21 | { 22 | cin >> N >> H; 23 | for(int i = 1; i <= N; ++i) 24 | cin >> A[i]; 25 | long long low = 1; 26 | long long high = N; 27 | long long mid; 28 | long long res = -1; 29 | while(low <= high){ 30 | mid = (high + low) / 2; 31 | if(chk(mid)){ 32 | res = mid; 33 | low = mid + 1; 34 | } 35 | else{ 36 | high = mid - 1; 37 | } 38 | } 39 | cout << res << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1119C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 505; 4 | int N, M; 5 | int A[MAXN][MAXN]; 6 | int B[MAXN][MAXN]; 7 | int main() 8 | { 9 | cin >> N >> M; 10 | for(int i = 0; i < N; ++i) 11 | for(int j = 0; j < M; ++j) 12 | cin >> A[i][j]; 13 | for(int i = 0; i < N; ++i) 14 | for(int j = 0; j < M; ++j) 15 | cin >> B[i][j]; 16 | int flag = 1; 17 | for(int i = 0; i < N; ++i) 18 | { 19 | int c = 0; 20 | for(int j = 0; j < M; ++j) 21 | { 22 | if(A[i][j] != B[i][j]) 23 | c++; 24 | } 25 | if((c % 2) != 0) 26 | flag = 0; 27 | } 28 | for(int j = 0; j < M; ++j){ 29 | int c = 0; 30 | for(int i = 0; i < N; ++i){ 31 | if(A[i][j] != B[i][j]) 32 | c++; 33 | } 34 | if((c % 2) != 0) 35 | flag = 0; 36 | } 37 | if(flag) 38 | cout << "Yes\n"; 39 | else 40 | cout << "No\n"; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1119E.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 3e5 + 5; 4 | int N; 5 | long long int A[MAXN]; 6 | long long int res; 7 | int main() 8 | { 9 | cin >> N; 10 | for(int i = 0; i < N; ++i) 11 | cin >> A[i]; 12 | res = 0; 13 | vector past; 14 | for(int i = 0; i < N; ++i){ 15 | int l = past.size(); 16 | for(int j = l - 1; j >= 0; --j){ 17 | if(A[i] < 2) 18 | break; 19 | A[i] -= 2; 20 | res++; 21 | past.pop_back(); 22 | } 23 | res = res + (A[i] / 3); 24 | for(int j = 1; j <= (A[i] % 3); ++j) 25 | past.push_back(i); 26 | } 27 | cout << res << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1139A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | char S[N]; 9 | long long res = 0; 10 | for(int i = 0; i < N; ++i) 11 | { 12 | cin >> S[i]; 13 | int num = S[i] - '0'; 14 | if((num % 2) == 0){ 15 | //cout << i << endl; 16 | res = (res + (i + 1)); 17 | } 18 | } 19 | cout << res << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1139B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | long long A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | long long ans = 0; 12 | for(int i = N - 2; i >= 0; i--) 13 | A[i] = min(A[i], A[i + 1] - 1); 14 | for(int i = 0; i < N; ++i) 15 | ans = ans + max(A[i], 0LL); 16 | cout << ans << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1140A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int N; 9 | cin >> N; 10 | int A[N + 1]; 11 | for(int i = 1; i <= N; ++i) 12 | cin >> A[i]; 13 | int limit = 0; 14 | int res = 0; 15 | for(int i = 1; i <= N; ++i){ 16 | limit = limit + 1; 17 | res++; 18 | while(i <= limit){ 19 | limit = max(limit, A[i]); 20 | i++; 21 | } 22 | i = limit; 23 | } 24 | cout << res << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1140B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int T; 9 | cin >> T; 10 | while(T--) 11 | { 12 | int L; 13 | cin >> L; 14 | char A[L]; 15 | for(int i = 0; i < L; ++i) 16 | cin >> A[i]; 17 | int idx1 = -1; 18 | int idx2 = -1; 19 | for(int i = 0; i < L; ++i){ 20 | if(A[i] == '>') 21 | { 22 | idx1 = i; 23 | break; 24 | } 25 | } 26 | for(int i = L - 1; i >= 0; i--){ 27 | if(A[i] == '<'){ 28 | idx2 = i; 29 | break; 30 | } 31 | } 32 | int res = 0; 33 | if(idx1 != -1 && idx2 != -1) 34 | res = min(idx1, L - 1 - idx2); 35 | else if(idx1 == -1) 36 | res = L - 1 - idx2; 37 | else 38 | res = idx1; 39 | cout << res << endl; 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1140D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int N; 9 | cin >> N; 10 | long long res = 0; 11 | for(int i = 2; i < N; ++i){ 12 | res = res + i * 1LL * (i + 1); 13 | } 14 | cout << res << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1143A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int A[N]; 9 | for(int i = 0; i < N; ++i){ 10 | cin >> A[i]; 11 | } 12 | int idx1 = INT_MAX; 13 | int idx2 = INT_MAX; 14 | for(int i = N - 1; i >= 0; i--){ 15 | if(A[i] == 1 && idx1 == INT_MAX) 16 | idx1 = i; 17 | if(A[i] == 0 && idx2 == INT_MAX) 18 | idx2 = i; 19 | } 20 | cout << min(idx1, idx2) + 1 << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1143D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | long long lcm(long long a, long long b){ 5 | return (a * b) / __gcd(a, b); 6 | } 7 | 8 | int main() 9 | { 10 | long long N, K, A, B; 11 | cin >> N >> K; 12 | cin >> A >> B; 13 | long long start = 1 + A; 14 | long long minimum = LONG_LONG_MAX; 15 | long long maximum = LONG_LONG_MIN; 16 | for(long long int i = 1; i <= (N * K); i += K){ 17 | long long e1 = i - B; 18 | while(e1 <= 0) 19 | e1 += (N * K); 20 | if(e1 <= start){ 21 | e1 = N*K + e1; 22 | } 23 | long long e2 = i + B; 24 | if(e2 <= start){ 25 | e2 = N*K + e2; 26 | } 27 | long long d1 = (N * K) / __gcd(N * K, (e1 - start)); 28 | long long d2 = (N * K) / __gcd(N * K, (e2 - start)); 29 | minimum = min(minimum, min(d1, d2)); 30 | maximum = max(maximum, max(d1, d2)); 31 | } 32 | cout << minimum << " " << maximum << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1144A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--){ 9 | string s; 10 | cin >> s; 11 | int l = s.size(); 12 | char A[l]; 13 | for(int i = 0; i < l; ++i) 14 | A[i] = s[i]; 15 | sort(A, A + l); 16 | int flag = 1; 17 | for(int i = 1; i < l; ++i){ 18 | if((A[i] - A[i - 1]) == 1) 19 | continue; 20 | flag = 0; 21 | break; 22 | } 23 | if(flag) 24 | cout << "YES\n"; 25 | else 26 | cout << "NO\n"; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1147C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Intuition: 3 | If a player is forced to move from the pile containing minimum number of stones, he loses. 4 | The intuition comes from the fact that what if some piles have 0 stones? That is the minimum number of stones. 5 | If he is forced to move that, he cannot and hence loses. 6 | */ 7 | #include "bits/stdc++.h" 8 | using namespace std; 9 | const int MAXN = 55; 10 | int N; 11 | int A[MAXN]; 12 | int main() 13 | { 14 | cin >> N; 15 | for(int i = 0; i < N; ++i) 16 | cin >> A[i]; 17 | int idx = N / 2; 18 | sort(A, A + N); 19 | if(A[0] == A[idx]) 20 | cout << "Bob\n"; 21 | else 22 | cout << "Alice\n"; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1150A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long int N, M, R; 7 | cin >> N >> M >> R; 8 | long long int res = LONG_LONG_MAX; 9 | for(int i = 1; i <= N; ++i){ 10 | long long int tmp; 11 | cin >> tmp; 12 | res = min(res, tmp); 13 | } 14 | long long int ret = LONG_LONG_MIN; 15 | for(int i = 1; i <= M; ++i){ 16 | long long int tmp; 17 | cin >> tmp; 18 | ret = max(ret, tmp); 19 | } 20 | long long int shares = R / res; 21 | long long ans = shares * ret; 22 | cout << max(R, ans + (R % res)) << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1150C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int one = 0;; 9 | int two = 0; 10 | for(int i = 1; i <= N; ++i) 11 | { 12 | int a; 13 | cin >> a; 14 | if(a == 1) 15 | one++; 16 | else 17 | two++; 18 | } 19 | if(one == 0){ 20 | for(int i = 1; i <= two; ++i) 21 | cout << "2 "; 22 | cout << endl; 23 | return 0; 24 | } 25 | if(two == 0){ 26 | for(int i = 1; i <= one; ++i) 27 | cout << "1 "; 28 | cout << endl; 29 | return 0; 30 | } 31 | cout << "2 1 "; 32 | for(int i = 1; i < two; ++i) 33 | cout << "2 "; 34 | for(int i = 1; i < one; ++i) 35 | cout << "1 "; 36 | cout << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1159A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int res = 0; 9 | int cnt = 0; 10 | for(int i = 0; i < N; ++i){ 11 | char c; 12 | cin >> c; 13 | if(c == '+') 14 | cnt++; 15 | else 16 | cnt--; 17 | res = min(res, cnt); 18 | } 19 | cnt = cnt + abs(res); 20 | cout << cnt << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1159B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | long long A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | long long res = LONG_LONG_MAX; 12 | for(int i = 0; i < N; ++i){ 13 | // check with 0th element 14 | if(i != 0){ 15 | res = min(res, min(A[i], A[0]) / i); 16 | } 17 | // check with N-1th element 18 | if(i != (N - 1)){ 19 | res = min(res, min(A[i], A[N - 1]) / abs(N - 1 - i)); 20 | } 21 | } 22 | cout << res << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1159C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e5 + 5; 4 | long long B[MAXN]; 5 | long long G[MAXN]; 6 | int N, M; 7 | map H; 8 | int main() 9 | { 10 | cin >> N >> M; 11 | for(int i = 0; i < N; ++i){ 12 | cin >> B[i]; 13 | H[B[i]] = 1; 14 | } 15 | for(int i = 0; i < M; ++i) 16 | cin >> G[i]; 17 | sort(B, B + N); 18 | sort(G, G + M); 19 | if(G[0] < B[N - 1]){ 20 | cout << "-1\n"; 21 | return 0; 22 | } 23 | long long res = 0; 24 | for(int i = 0; i < N; ++i) 25 | res = res + (B[i] * 1LL *M); 26 | int curr = N - 1; 27 | int cnt = 0; 28 | for(int i = M - 1; i >= 0; i--){ 29 | if(H.find(G[i]) == H.end()) 30 | res += (G[i] - B[curr]); 31 | cnt = cnt + 1; 32 | if(cnt == (M - 1)){ 33 | cnt = 0; 34 | curr = curr - 1; 35 | } 36 | } 37 | cout << res << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1163A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, M; 7 | cin >> N >> M; 8 | if(M == 0) 9 | cout << "1\n"; 10 | else if(M == N) 11 | cout << "0\n"; 12 | else{ 13 | int res = min(M, N - M); 14 | cout << res << endl; 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1163B(easy).cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e5 + 5; 4 | int N; 5 | int freq[15]; 6 | int main() 7 | { 8 | cin >> N; 9 | int res = 0; 10 | for(int i = 1; i <= N; ++i){ 11 | int tmp; 12 | cin >> tmp; 13 | freq[tmp]++; 14 | vector lol; 15 | for(int j = 1; j <= 10; ++j) 16 | { 17 | if(freq[j] > 0) 18 | lol.push_back(freq[j]); 19 | } 20 | sort(lol.begin(), lol.end()); 21 | if(lol.size() == 1) 22 | res = i; 23 | else{ 24 | if((lol[0] == 1 && (lol[1] == lol[lol.size() - 1])) || (lol[lol.size() - 1] == (lol[lol.size() - 2] + 1) && (lol[0] == lol[lol.size() - 2]))) 25 | res = i; 26 | } 27 | } 28 | cout << res << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1165A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, X, Y; 7 | cin >> N >> X >> Y; 8 | char A[N]; 9 | for(int i = 0; i < N; ++i){ 10 | cin >> A[i]; 11 | } 12 | int res = 0; 13 | for(int i = N - 1; i > (N - 1 - X); i--){ 14 | if(i == (N - 1 - Y) && A[i] == '0') 15 | res++; 16 | if(i != (N - 1 - Y) && A[i] != '0') 17 | res++; 18 | } 19 | cout << res << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1165B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | sort(A, A + N); 12 | int day = 0; 13 | for(int i = 0; i < N; ++i){ 14 | if(A[i] > day) 15 | day++; 16 | } 17 | cout << day << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1165C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | vector S; 9 | for(int i = 0; i < N; ++i){ 10 | char ch; 11 | cin >> ch; 12 | S.push_back(ch); 13 | } 14 | for(int i = 0; i < S.size(); i += 2){ 15 | if(i == (S.size() - 1)){ 16 | S.erase(S.begin() + i); 17 | continue; 18 | } 19 | if(S[i] != S[i + 1]) 20 | continue; 21 | while(S[i] == S[i + 1]){ 22 | S.erase(S.begin() + i); 23 | if(i == (S.size() - 1)){ 24 | S.erase(S.begin() + i); 25 | break; 26 | } 27 | } 28 | } 29 | cout << (N - S.size()) << endl; 30 | for(int i = 0; i < S.size(); ++i) 31 | cout << S[i]; 32 | cout << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1167A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int T; 9 | cin >> T; 10 | while(T--) 11 | { 12 | int N; 13 | cin >> N; 14 | char A[N]; 15 | for(int i = 0; i < N; ++i) 16 | cin >> A[i]; 17 | int idx = -1; 18 | for(int i = 0; i < N; ++i){ 19 | if(A[i] == '8') 20 | { 21 | idx = i; 22 | break; 23 | } 24 | } 25 | if(idx == -1) 26 | cout << "NO\n"; 27 | else{ 28 | if((N - idx) >= 11) 29 | cout << "YES\n"; 30 | else 31 | cout << "NO\n"; 32 | } 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1176A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const long long INF = 2000000000000000000; 4 | int T; 5 | long long N; 6 | map DP; 7 | long long rec(long long N){ 8 | if(N == 1) 9 | return 0; 10 | if(DP.find(N) != DP.end()) 11 | return DP[N]; 12 | DP[N] = INF; 13 | if((N % 2) == 0) 14 | DP[N] = 1 + rec(N / 2); 15 | else if((N % 3) == 0) 16 | DP[N] = min(DP[N], 2 + rec(N / 3)); 17 | else if((N % 5) == 0) 18 | DP[N] = min(DP[N], 3 + rec(N / 5)); 19 | if(DP[N] >= INF) 20 | DP[N] = INF; 21 | return DP[N]; 22 | } 23 | int main() 24 | { 25 | ios_base::sync_with_stdio(false); 26 | cin.tie(NULL); 27 | cin >> T; 28 | while(T--){ 29 | cin >> N; 30 | long long res = rec(N); 31 | if(res == INF) 32 | cout << "-1\n"; 33 | else 34 | cout << res << endl; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1176B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 105; 4 | int T, N; 5 | long long int A[MAXN]; 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | cin >> T; 11 | while(T--){ 12 | cin >> N; 13 | long long f[3]; 14 | f[0] = 0; f[1] = 0; f[2] = 0; 15 | for(int i = 0; i < N; ++i){ 16 | cin >> A[i]; 17 | f[A[i] % 3]++; 18 | } 19 | long long res = f[0] + min(f[2], f[1]) + ((f[2] - min(f[2], f[1])) / 3) + ((f[1] - min(f[2], f[1])) / 3); 20 | //res = max(res, min(f[1] % 3, f[2] % 3)+ (f[1] / 3) + (f[2] / 3) + f[0]); 21 | cout << res << endl; 22 | //cout << (f[0] + max(max(f[1], f[2]), max(f[1] / 3, f[2] / 3))) << endl; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1178A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | long long s = 0; 9 | long long a; 10 | cin >> a; 11 | s += a; 12 | long long c = a; 13 | vector v; 14 | v.push_back(1); 15 | for(int i = 2; i <= N; ++i){ 16 | long long tmp; 17 | cin >> tmp; 18 | s += tmp; 19 | if((a / 2) >= tmp){ 20 | c += tmp; 21 | v.push_back(i); 22 | } 23 | } 24 | if(((s / 2) + 1) <= c) 25 | { 26 | cout << v.size() << endl; 27 | for(int i = 0; i < v.size(); ++i) 28 | cout << v[i] << " "; 29 | cout << endl; 30 | } 31 | else 32 | cout << 0 << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1178B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string s; 7 | cin >> s; 8 | int L = s.size(); 9 | long long int cum1[L]; 10 | long long int cum2[L]; 11 | for(int i = 0; i < L; ++i) 12 | cum1[i] = 0, cum2[i] = 0; 13 | for(int i = 1; i < L; ++i){ 14 | if(s[i] == 'v' && s[i - 1] == 'v') 15 | cum1[i] = cum1[i - 1] + 1; 16 | else 17 | cum1[i] = cum1[i - 1]; 18 | } 19 | for(int i = (L - 2); i >= 0; --i) 20 | { 21 | if(s[i] == 'v' && s[i + 1] == 'v') 22 | cum2[i] = cum2[i + 1] + 1; 23 | else 24 | cum2[i] = cum2[i + 1]; 25 | } 26 | long long res = 0; 27 | for(int i = 0; i < L; ++i){ 28 | if(s[i] == 'o'){ 29 | res = res + (cum1[i] * cum2[i]); 30 | } 31 | } 32 | cout << res << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1178C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const long long MOD = 998244353; 4 | int main() 5 | { 6 | long long int R, C; 7 | cin >> R >> C; 8 | long long p = 2; 9 | for(int i = 1; i <= C; ++i) 10 | p = (p * 2) % MOD; 11 | for(int i = 1; i < R; ++i) 12 | p = (p * 2) % MOD; 13 | cout << p << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1178D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e6 + 5; 4 | int N; 5 | 6 | int isPrime[MAXN]; 7 | vector u; 8 | vector v; 9 | int main() 10 | { 11 | for(int i = 0; i < MAXN; ++i) 12 | isPrime[i] = 1; 13 | for(int i = 2; i < MAXN; ++i){ 14 | if(isPrime[i] == 1){ 15 | for(int j = i + i; j < MAXN; j += i) 16 | isPrime[j] = 0; 17 | } 18 | } 19 | cin >> N; 20 | for(int i = 1; i <= N; ++i) 21 | { 22 | if(i == N) 23 | u.push_back(i), v.push_back(1); 24 | else 25 | u.push_back(i), v.push_back(i + 1); 26 | } 27 | int edges = N; 28 | int lst = 1; 29 | while(isPrime[edges] == 0){ 30 | edges++; 31 | u.push_back(lst), v.push_back(lst + (N / 2)); 32 | lst++; 33 | } 34 | cout << u.size() << endl; 35 | for(int i = 0; i < u.size(); ++i) 36 | cout << u[i] << " " << v[i] << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1182A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | long long N; 9 | cin >> N; 10 | if((N % 2) != 0) 11 | cout << "0\n"; 12 | else{ 13 | int L = N / 2; 14 | long long p = 1; 15 | for(int i = 1; i <= L; ++i) 16 | p *= 2; 17 | cout << p << endl; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1184A1.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long int r; 7 | cin >> r; 8 | int done = 0; 9 | for(int i = 1; i <= 1000000; i++){ 10 | long long num = (r - (i * 1LL * i) - i - 1); 11 | long long den = 2 * i; 12 | if((num % den) == 0 && (num / den) > 0) 13 | { 14 | done = 1; 15 | cout << i << " " << (num / den) << endl; 16 | break; 17 | } 18 | } 19 | if(!done) 20 | cout << "NO\n"; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1184D1.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int N, K, M, T; 4 | int main() 5 | { 6 | cin >> N >> K >> M >> T; 7 | int curr_sz = N; 8 | int curr_idx = K; 9 | while(T--) 10 | { 11 | int key, idx; 12 | cin >> key >> idx; 13 | if(key == 0){ 14 | // Break Link 15 | if(idx < curr_idx){ 16 | curr_sz -= idx; 17 | curr_idx -= idx; 18 | } 19 | else{ 20 | curr_sz = idx; 21 | } 22 | } 23 | else{ 24 | // Add Link 25 | if(idx <= curr_idx){ 26 | curr_idx += 1; 27 | curr_sz += 1; 28 | } 29 | else{ 30 | curr_sz += 1; 31 | } 32 | } 33 | cout << curr_sz << " " << curr_idx << endl; 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1186D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | vector res; 9 | vector A; 10 | long long s = 0; 11 | for(int i = 0; i < N; ++i){ 12 | long double t; 13 | cin >> t; 14 | res.push_back(ceil(t)); 15 | //cout << res[i] << " "; 16 | A.push_back(t); 17 | s += res[i]; 18 | } 19 | if(s != 0){ 20 | for(int i = 0; i < N; ++i){ 21 | if(A[i] == res[i]) 22 | continue; 23 | res[i] = res[i] - 1; 24 | s -= 1; 25 | if(s == 0) 26 | break; 27 | } 28 | } 29 | for(int i = 0; i < N; ++i) 30 | cout << res[i] << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1194D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long N, K; 11 | cin >> N >> K; 12 | if((K % 3) == 0 && (N >= K)){ 13 | // Things get shifted 14 | long long lol = N % (K + 1); 15 | if(lol == K) 16 | cout << "Alice\n"; 17 | else if((lol % 3) == 0) 18 | cout << "Bob\n"; 19 | else 20 | cout << "Alice\n"; 21 | } 22 | else{ 23 | if((N % 3) == 0) 24 | cout << "Bob\n"; 25 | else 26 | cout << "Alice\n"; 27 | } 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1195A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, K; 7 | cin >> N >> K; 8 | int A[K + 1]; 9 | for(int i = 1; i <= K; ++i) 10 | A[i] = 0; 11 | for(int i = 1; i <= N; ++i){ 12 | int tmp; 13 | cin >> tmp; 14 | A[tmp]++; 15 | } 16 | int rem = 0; 17 | int res = 0; 18 | for(int i = 1; i <= K; ++i){ 19 | rem = rem + (A[i] % 2); 20 | if((A[i] % 2) == 0) 21 | res = res + A[i]; 22 | else 23 | res = res + (A[i] - 1); 24 | } 25 | res = res + ceil(rem * 1.0 / 2); 26 | cout << res << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1195B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long double N, K; 7 | cin >> N >> K; 8 | long double l = 9 - 4 * (-2 * N - 2 * K); 9 | l = sqrt(l); 10 | l = l - 3; 11 | l = l / 2; 12 | long long res = (N - l); 13 | cout << res << endl; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1195C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e5 + 5; 4 | int N; 5 | long long H1[MAXN]; 6 | long long H2[MAXN]; 7 | long long DP[MAXN][3]; 8 | long long rec(int idx, int lst){ 9 | if(idx == N) 10 | return 0; 11 | if(DP[idx][lst] != -1) 12 | return DP[idx][lst]; 13 | if(lst == 2) 14 | return DP[idx][lst] = max(rec(idx + 1, 1) + H2[idx], rec(idx + 1, 0) + H1[idx]); 15 | else if(lst == 1) 16 | return DP[idx][lst] = max(rec(idx + 1, 1 - lst) + H1[idx], rec(idx + 1, lst)); 17 | else if(lst == 0) 18 | return DP[idx][lst] = max(rec(idx + 1, 1 - lst) + H2[idx], rec(idx + 1, lst)); 19 | } 20 | 21 | int main() 22 | { 23 | cin >> N; 24 | for(int i = 0; i < N; ++i) 25 | cin >> H1[i]; 26 | for(int i = 0; i < N; ++i) 27 | cin >> H2[i]; 28 | for(int i = 0; i < MAXN; ++i) 29 | for(int j = 0; j <= 2; ++j) 30 | DP[i][j] = -1; 31 | cout << rec(0, 2) << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1197A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | int A[N]; 13 | for(int i = 0; i < N; ++i) 14 | cin >> A[i]; 15 | sort(A, A + N); 16 | int ans = 0; 17 | for(int i = 1; i <= N; ++i){ 18 | if(A[N - 1] >= (i + 1) && A[N - 2] >= (i + 1) && (N - 2) >= i) 19 | ans = i; 20 | } 21 | cout << ans << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1197B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int A[N]; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | int pos = -1; 12 | for(int i = 0; i < N; ++i){ 13 | if(A[i] == N){ 14 | pos = i; 15 | break; 16 | } 17 | } 18 | int no = 0; 19 | for(int i = 1; i <= pos; ++i){ 20 | if(A[i] > A[i - 1]) 21 | continue; 22 | no = 1; 23 | } 24 | for(int i = pos; i < (N - 1); ++i){ 25 | if(A[i] > A[i + 1]) 26 | continue; 27 | no = 1; 28 | } 29 | if(no) 30 | cout << "NO\n"; 31 | else 32 | cout << "YES\n"; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1197C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, K; 7 | cin >> N >> K; 8 | vector t; 9 | long long prev; 10 | cin >> prev; 11 | long long ans = -prev; 12 | for(int i = 1; i < N; ++i){ 13 | long long curr; 14 | cin >> curr; 15 | t.push_back(prev - curr); 16 | prev = curr; 17 | } 18 | ans += prev; 19 | sort(t.begin(), t.end()); 20 | for(int i = 0; i < (K - 1); ++i) 21 | ans += t[i]; 22 | cout << ans << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1197D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 3e5 + 5; 4 | const int MAXM = 15; 5 | long long int N, M, K; 6 | long long int A[MAXN]; 7 | long long DP[MAXN][MAXM]; 8 | long long int rec(int idx, int rem) 9 | { 10 | if(idx == N) 11 | return 0; 12 | if(DP[idx][rem] != -1) 13 | return DP[idx][rem]; 14 | long long tmp = A[idx]; 15 | if(rem == 0) 16 | DP[idx][rem] = max(0LL, rec(idx + 1, (rem + 1) % M) + tmp - K); 17 | else 18 | DP[idx][rem] = max(0LL, rec(idx + 1, (rem + 1) % M) + tmp); 19 | return DP[idx][rem]; 20 | } 21 | 22 | 23 | int main() 24 | { 25 | cin >> N >> M >> K; 26 | for(int i = 0; i < N; ++i) 27 | cin >> A[i]; 28 | for(int i = 0; i < MAXN; ++i) 29 | for(int j = 0; j < MAXM; ++j) 30 | DP[i][j] = -1; 31 | long long ans = 0; 32 | for(int i = 0; i < N; ++i) 33 | ans = max(ans, rec(i, 0)); 34 | cout << ans << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1202C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int L; 4 | string S; 5 | 6 | bool chk(int m){ 7 | int r = 0; int c = 0; 8 | vector R; 9 | vector C; 10 | int used = 0; 11 | for(int i = 0; i < L; ++i){ 12 | if(S[i] == 'D') 13 | r++; 14 | if(S[i] == 'U') 15 | r--; 16 | if(S[i] == 'L') 17 | c--; 18 | if(S[i] == 'R') 19 | c++; 20 | if(abs(r) > m){ 21 | if((f.size() - 2) >= 0){ 22 | g(f[f.size() - 2]); 23 | } 24 | } 25 | if(abs(c) > m){ 26 | 27 | } 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | int T; 34 | cin >> T; 35 | while(T--){ 36 | cin >> S; 37 | L = S.size(); 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1203E.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | map H; 5 | 6 | int main() 7 | { 8 | int N; 9 | cin >> N; 10 | int A[N]; 11 | for(int i = 0; i < N; ++i) 12 | cin >> A[i]; 13 | sort(A, A + N); 14 | int res = 0; 15 | for(int i = 0; i < N; ++i){ 16 | if((A[i] - 1) > 0 && H.find(A[i] - 1) == H.end()){ 17 | H[A[i] - 1] = 1; 18 | res++; 19 | } 20 | else if((A[i]) > 0 && H.find(A[i]) == H.end()){ 21 | H[A[i]] = 1; 22 | res++; 23 | } 24 | else if((A[i] + 1) > 0 && H.find(A[i] + 1) == H.end()){ 25 | H[A[i] + 1] = 1; 26 | res++; 27 | } 28 | } 29 | cout << res << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1215A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a1, a2, k1, k2, n; 7 | cin >> a1 >> a2 >> k1 >> k2 >> n; 8 | int calc1 = min(a1, (n / k1)) + min(a2, (n - min(a1, (n / k1)) * k1) / k2); 9 | int calc2 = min(a2, (n / k2)) + min(a1, (n - min(a2, (n / k2)) * k2) / k1); 10 | cout << min(calc1, calc2) << " " << max(calc1, calc2) << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1217A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | while(t--) 9 | { 10 | long long s, i, f; 11 | cin >> s >> i >> f; 12 | if(s >(i + f)) 13 | cout << f+1 << "\n"; 14 | else 15 | cout << (f - floor((i + f - s) * 1.0/ 2)) << endl; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1217B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | while(t--) 9 | { 10 | long long int n, x, d, h; 11 | cin >> n >> x; 12 | long long res = 1e18; 13 | for(int i = 1; i <= n; ++i){ 14 | cin >> d >> h; 15 | long long low = 1; long long high = 1e9; 16 | long long mid; long long pos = -1; 17 | while(high >= low) 18 | { 19 | mid = (high + low) / 2; 20 | if((x - mid * d + (mid - 1) * h) <= 0){ 21 | pos = mid; 22 | high = mid - 1; 23 | } 24 | else 25 | low = mid + 1; 26 | } 27 | if(pos!=-1) 28 | res = min(res, pos); 29 | } 30 | if(res == 1e18) 31 | cout << "-1\n"; 32 | else 33 | { 34 | cout << res << endl; 35 | } 36 | 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1230D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | return 0; 7 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1244A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | while(t--) 9 | { 10 | int a, b, c, d, e; 11 | cin >> a >> b >> c >> d >> e; 12 | int x = ceil(a * 1.0 / c); 13 | int y = ceil(b * 1.0 / d); 14 | if((x + y) <= e){ 15 | cout << x << " " << y << endl; 16 | } 17 | else 18 | cout << "-1\n"; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1244B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | while(t--) 9 | { 10 | string s; 11 | int n; 12 | cin >> n; 13 | cin >> s; 14 | int a = INT_MAX; 15 | for(int i = 0; i < n; ++i){ 16 | if(s[i] == '1') 17 | a = min(min(i, n - 1 - i), a); 18 | 19 | } 20 | if(a == INT_MAX) 21 | cout << n << endl; 22 | else 23 | cout << 2 * (n - a) << endl; 24 | 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1250B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXK = 8005; 4 | int A[MAXK]; 5 | int N, K; 6 | 7 | bool cmp(int a, int b) 8 | { 9 | return a > b; 10 | } 11 | 12 | int main() 13 | { 14 | cin >> N >> K; 15 | for(int i = 1; i <= N; ++i){ 16 | int u; 17 | cin >> u; 18 | A[u - 1]++; 19 | } 20 | sort(A, A + K, cmp); 21 | long long int res = 100000000000000000; 22 | for(int i = 1; i <= K; ++i){ 23 | // Fixing r. 24 | int idx = 2 * i - K; 25 | if(idx < 0) 26 | continue; 27 | int l = idx; int r = K - 1; 28 | int mx = A[0]; 29 | while(l <= r) 30 | { 31 | mx = max(mx, A[l] + A[r]); 32 | l++; 33 | r--; 34 | } 35 | //cout << mx << endl; 36 | res = min(res, mx * 1LL *i); 37 | } 38 | cout << res << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1250F.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | long long A; 4 | int main() 5 | { 6 | cin >> A; 7 | long long res = 1e18; 8 | for(int i = 1; i <= 100000; ++i){ 9 | long long int l = i; 10 | if((A % l) != 0) 11 | continue; 12 | long long int b = A / l; 13 | long long ar = 2 * (l + b); 14 | res = min(res, ar); 15 | } 16 | cout << res << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1266A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | string s; 11 | cin >> s; 12 | int l = s.size(); 13 | int zeros = 0; 14 | int sum = 0; 15 | int mod2 = 0; 16 | for(int i = 0; i < l; ++i){ 17 | sum = sum + (s[i] - '0'); 18 | if(s[i] == '0') 19 | zeros++; 20 | if(((s[i] - '0') % 2) == 0) 21 | mod2++; 22 | } 23 | if(zeros > 0 && (sum % 3) == 0 && mod2 >= 2) 24 | cout << "red\n"; 25 | else 26 | cout << "cyan\n"; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1266B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long int N; 11 | cin >> N; 12 | int mod = (N % 14); 13 | if(N <= 6) 14 | cout << "NO\n"; 15 | else if(mod >= 1 && mod <= 6) 16 | cout << "YES\n"; 17 | else 18 | cout << "NO\n"; 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1266C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N, M; 7 | cin >> N >> M; 8 | if(N == 1 && M == 1) 9 | { 10 | cout << "0\n"; 11 | return 0; 12 | } 13 | if(M == 1) 14 | { 15 | for(int i = 1; i <= N; ++i) 16 | { 17 | cout << (i + 1) << endl; 18 | } 19 | return 0; 20 | } 21 | long long int A[N][M]; 22 | int count = 1; 23 | for(int i = 0; i < N; ++i) 24 | { 25 | for(int j = 0; j < M; ++j) 26 | A[i][j] = (i + 1); 27 | count++; 28 | } 29 | for(int j = 0; j < M; ++j){ 30 | for(int i = 0; i < N; ++i) 31 | A[i][j] *= count; 32 | count++; 33 | } 34 | for(int i = 0; i < N; ++i) 35 | { 36 | for(int j = 0; j < M; ++j) 37 | cout << A[i][j] << " "; 38 | cout << endl; 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1270A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N, A, B; 11 | cin >> N >> A >> B; 12 | int flag = 0; 13 | for(int i = 1; i <= A; ++i){ 14 | int tmp; 15 | cin >> tmp; 16 | if(tmp == N) 17 | flag = 1; 18 | } 19 | for(int i = 1; i <= B; ++i){ 20 | int tmp; 21 | cin >> tmp; 22 | if(tmp == N) 23 | flag = 0; 24 | } 25 | if(flag) 26 | cout << "YES\n"; 27 | else 28 | cout << "NO\n"; 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1270B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | long long int A[N]; 13 | for(int i = 0; i < N; ++i){ 14 | cin >> A[i]; 15 | } 16 | int flag = 0; 17 | for(int i = 1; i < N; ++i){ 18 | if(abs(A[i] - A[i - 1]) == 1){ 19 | continue; 20 | } 21 | else if(abs(A[i] - A[i - 1]) == 0) 22 | continue; 23 | flag = 1; 24 | } 25 | if(flag == 0) 26 | cout << "NO\n"; 27 | else{ 28 | cout << "YES\n"; 29 | for(int i = 1; i < N; ++i){ 30 | if(abs(A[i] - A[i - 1]) >= 2){ 31 | cout << i << " " << (i + 1) << endl; 32 | break; 33 | } 34 | } 35 | } 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1278B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long a, b; 11 | cin >> a >> b; 12 | if(a > b) 13 | swap(a, b); 14 | for(long long int i = 0; ; ++i){ 15 | a += i; 16 | if(a >= b) 17 | { 18 | long long diff = a - b; 19 | if((diff%2) == 0) 20 | cout << i << endl; 21 | else if((i % 2) == 0) 22 | cout << (i + 1) << endl; 23 | else if((i % 2) != 0) 24 | cout << (i + 2) << endl; 25 | else 26 | assert(false); 27 | break; 28 | } 29 | } 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1279A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long A, B, C; 11 | cin >> A >> B >> C; 12 | long long sum = ceil((A + B + C) * 1.0 / 2); 13 | if(A > sum || B > sum || C > sum) 14 | cout << "No\n"; 15 | else 16 | cout << "Yes\n"; 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1279C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N, M; 11 | cin >> N >> M; 12 | int idx[N + 1]; 13 | int A[N + 1]; 14 | for(int i = 1; i <= N; ++i) 15 | { 16 | cin >> A[i]; 17 | idx[A[i]] = i; 18 | } 19 | int max_idx = -1; 20 | int tmp; 21 | long long ans = 0; 22 | for(int i = 1; i <= M; ++i){ 23 | cin >> tmp; 24 | int ind = idx[tmp]; 25 | // cout << ind << " " << max_idx << endl; 26 | if(ind <= max_idx){ 27 | ans += 1; 28 | } 29 | else{ 30 | max_idx = ind; 31 | // cout << i << endl; 32 | //cout << (- 1 + (2 * (ind - (i - 1)))) << endl; 33 | ans = ans - 1 + (2LL * (ind - (i - 1))); 34 | } 35 | } 36 | cout << ans << endl; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1283A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int h, m; 11 | cin >> h >> m; 12 | if(h == 0 && m == 0) 13 | { 14 | cout << 0 << endl; 15 | continue; 16 | } 17 | long long int rem = 23 - h; 18 | long long int min = 60 - m; 19 | cout << (rem * 60 + min) << endl; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1283B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long int N, K; 11 | cin >> N >> K; 12 | long long thresh = K / 2; 13 | long long rem = N % K; 14 | if(rem <= thresh){ 15 | cout << N << endl; 16 | } 17 | else{ 18 | cout << ((N / K) * K + thresh) << endl; 19 | } 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1287A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | char S[N]; 13 | for(int i = 0; i < N; ++i) 14 | cin >> S[i]; 15 | int res = 0; 16 | int st = N; 17 | for(int i = 0; i < N; ++i){ 18 | if(S[i] == 'A') 19 | { 20 | st = i; 21 | break; 22 | } 23 | } 24 | int curr = -1; 25 | for(int i = st; i < N; ++i){ 26 | if(S[i] == 'A'){ 27 | curr = i; 28 | continue; 29 | } 30 | res = max(res, abs(curr - i)); 31 | } 32 | cout << res << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1293B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long double res = 0; 7 | int N; 8 | cin >> N; 9 | for(int i = 1; i <= N; ++i) 10 | res = res + (1.0 / i); 11 | cout << fixed << setprecision(10) << res << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1345A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N, M; 11 | cin >> N >> M; 12 | if(N == 1 || M == 1){ 13 | cout << "YES\n"; 14 | } 15 | else if(N == 2 && M == 2){ 16 | cout << "YES\n"; 17 | } 18 | else{ 19 | cout << "NO\n"; 20 | } 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1345C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | long long A[N]; 13 | for(int i = 0; i < N; ++i) 14 | cin >> A[i]; 15 | int mod[N]; 16 | for(int i = 0; i < N; ++i){ 17 | mod[i] = 0; 18 | A[i] += i; 19 | } 20 | for(int i = 0; i < N; ++i){ 21 | // long long tmp1 = ceil(A[i] * 1.0 / N); 22 | long long tmp2 = floor(A[i] * 1.0 / N); 23 | long long M = A[i] - (N * tmp2); 24 | // cout << M << endl; 25 | mod[M]++; 26 | } 27 | int got = 0; 28 | for(int i = 0; i < N; ++i){ 29 | if(mod[i] > 1){ 30 | got = 1; 31 | } 32 | } 33 | if(got == 0) 34 | cout << "YES\n"; 35 | else 36 | cout << "NO\n"; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1352G.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | if(N == 2){ 13 | cout << "-1\n"; 14 | } 15 | else if(N == 3){ 16 | cout << "-1\n"; 17 | } 18 | else if((N % 2) == 0){ 19 | for(int i = 2; i <= N; i += 2) 20 | cout << i << " "; 21 | cout << (N - 3) << " " << (N - 1) << " "; 22 | for(int i = (N - 5); i >= 1; i -= 2) 23 | cout << i << " "; 24 | cout << endl; 25 | } 26 | else{ 27 | for(int i = 1; i <= N; i += 2) 28 | cout << i << " "; 29 | cout << (N - 3) << " " << (N - 1) << " "; 30 | for(int i = (N - 5); i >= 1; i -= 2) 31 | cout << i << " "; 32 | cout << endl; 33 | } 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1358A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long int N, M; 11 | cin >> N >> M; 12 | if((N % 2) == 0){ 13 | cout << (M * N / 2) << endl; 14 | } 15 | else if((M % 2) == 0){ 16 | cout << (M * N / 2) << endl; 17 | } 18 | else{ 19 | cout << (long long int)(min((N - 1) * M / 2 + ceil(M * 1.0 / 2), (M - 1) * N / 2 + ceil(N * 1.0 / 2))) << endl; 20 | } 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1358B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | int A[N]; 13 | for(int i = 0; i < N; ++i) 14 | cin >> A[i]; 15 | sort(A, A + N); 16 | int ans = 1; 17 | for(int i = 0; i < N; ++i){ 18 | if(A[i] <= (1 + i)) 19 | ans = max(ans, i + 2); 20 | } 21 | cout << ans << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1362B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | cin >> N; 12 | int A[N]; 13 | for(int i = 0; i < N; ++i) 14 | cin >> A[i]; 15 | long long int sol = -1; 16 | for(int i = 1; i <= 1024; ++i){ 17 | map H; 18 | for(int j = 0; j < N; ++j){ 19 | // cout << (A[j] ^ i) << endl; 20 | H[A[j] ^ i] = 1; 21 | } 22 | int exists = 0; 23 | for(int j = 0; j < N; ++j){ 24 | if(H.find(A[j]) != H.end()) 25 | exists++; 26 | } 27 | if(exists == N){ 28 | sol = i; 29 | break; 30 | } 31 | } 32 | cout << sol << endl; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1362C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long int N; 11 | cin >> N; 12 | long long sum = 0; 13 | for(long long int i = 1; i <= 61; ++i){ 14 | // cout << i << endl; 15 | long long int diff = N - (1LL << (i - 1)); 16 | if(diff < 0) 17 | break; 18 | 19 | long long int occ = (diff / (1LL << i)) + 1; 20 | // cout << i << " " << occ << endl; 21 | sum = sum + (i * 1LL * occ); 22 | } 23 | cout << sum << endl; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1363A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N, X; 11 | cin >> N >> X; 12 | int even = 0; 13 | int odd = 0; 14 | for(int i = 1; i <= N; ++i){ 15 | int tmp; 16 | cin >> tmp; 17 | if((tmp % 2) == 0) 18 | even++; 19 | else 20 | odd++; 21 | } 22 | if(odd == 0){ 23 | cout << "No\n"; 24 | continue; 25 | } 26 | int e = 0; 27 | while(X >= 2 && even > 0){ 28 | X--; 29 | e = 1; 30 | even--; 31 | } 32 | while(X >= 3 && odd >= 2){ 33 | X -= 2; 34 | odd -= 2; 35 | } 36 | if((X == 1 && odd > 0) || (X == 2 && e == 1 && odd > 2)) 37 | cout << "Yes\n"; 38 | else 39 | cout << "No\n"; 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1363B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | int N; 11 | string S; 12 | cin >> S; 13 | N = S.size(); 14 | int a1 = 0; 15 | int cum0[N]; 16 | int cum1[N]; 17 | for(int i = 0; i < N; ++i) 18 | cum0[i] = cum1[i] = 0; 19 | if(S[0] == '1') 20 | cum1[0] = 1; 21 | else 22 | cum0[0] = 1; 23 | for(int i = 1; i < N; ++i){ 24 | cum0[i] = cum0[i - 1] + (S[i] == '0' ? 1 : 0); 25 | cum1[i] = cum1[i - 1] + (S[i] == '1' ? 1 : 0); 26 | } 27 | int res = N; 28 | for(int i = 0; i < N; ++i){ 29 | // Till here it is 0. 30 | res = min(res, cum1[i] + cum0[N - 1] - cum0[i]); 31 | // Till here it is 1. 32 | res = min(res, cum0[i] + cum1[N - 1] - cum1[i]); 33 | } 34 | cout << res << endl; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1365C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cin >> N; 8 | int A[N]; 9 | int B[N]; 10 | for(int i = 0; i < N; ++i) 11 | cin >> A[i]; 12 | for(int i = 0; i < N; ++i) 13 | cin >> B[i]; 14 | int idx1[N + 1]; 15 | int idx2[N + 1]; 16 | for(int i = 0; i < N; ++i){ 17 | idx1[A[i]] = i; 18 | idx2[B[i]] = i; 19 | } 20 | int freq[N + 1]; 21 | for(int i = 0; i <= N; ++i) 22 | freq[i] = 0; 23 | for(int i = 1; i <= N; ++i){ 24 | if(idx1[i] <= idx2[i]) 25 | freq[idx2[i] - idx1[i]]++; 26 | else 27 | freq[N - 1 - idx1[i] + idx2[i] + 1]++; 28 | } 29 | int res = 0; 30 | for(int i = 0; i <= N; ++i) 31 | res = max(res, freq[i]); 32 | cout << res << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/1369D.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const long long MOD = 1e9 + 7; 4 | const int MAXN = 2e6 + 5; 5 | long long int Z[MAXN]; 6 | long long int O[MAXN]; 7 | long long int G[MAXN]; 8 | int T, N; 9 | int main() 10 | { 11 | Z[1] = 1; O[1] = 0; G[1] = 0; 12 | Z[2] = 1; O[2] = 1; G[2] = 0; 13 | Z[3] = 3; O[3] = 1; G[3] = 1; 14 | Z[4] = 5; O[4] = 3; G[4] = 1; 15 | for(int i = 5; i < MAXN; ++i){ 16 | Z[i] = (Z[i - 1] + ((2 * O[i - 1])%MOD) )%MOD; 17 | O[i] = Z[i - 1]; 18 | G[i] = (G[i - 3] + O[i - 1])%MOD; 19 | } 20 | cin >> T; 21 | while(T--) 22 | { 23 | cin >> N; 24 | cout << (4*G[N])%MOD << endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/320A.py: -------------------------------------------------------------------------------- 1 | N=input() 2 | cnt=0 3 | flag=1 4 | last=-1 5 | while N>0: 6 | dig=N%10 7 | if dig!=4 and dig!=1: 8 | flag=0 9 | break 10 | if dig==4: 11 | cnt=cnt+1 12 | else: 13 | cnt=0 14 | if cnt==3: 15 | flag=0 16 | break 17 | last=dig 18 | N=N/10 19 | if flag==1 and last==1: 20 | print "YES" 21 | else: 22 | print "NO" -------------------------------------------------------------------------------- /Codeforces/Solved Problems/456B.c: -------------------------------------------------------------------------------- 1 | /* I am a n00b at Math :P */ 2 | #include 3 | int main() 4 | { 5 | char S[1000005]; 6 | scanf("%s",S); 7 | int i=0; 8 | int rem=0; 9 | for(i=0; ; i++) 10 | { 11 | if(S[i]=='\0')break; 12 | int dig=S[i]-48; 13 | int num=(10*rem)+dig; 14 | rem=num%4; 15 | } 16 | 17 | int two[]={1,2,4,3}; 18 | int three[]={1,3,4,2}; 19 | int four[]={1,4,1,4}; 20 | int sum=1+two[rem]+three[rem]+four[rem]; 21 | sum%=5; 22 | printf("%d\n", sum); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/478C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long int A[3]; 7 | cin >> A[0] >> A[1] >> A[2]; 8 | sort(A, A + 3); 9 | long long res = A[0]; 10 | long long reqd = 2 * A[0]; 11 | reqd = reqd - max(reqd, (A[2] - A[1])); 12 | if(reqd > 0) 13 | { 14 | A[2] = A[1]; 15 | res = res + min(A[1], (A[2] + A[1] - reqd) / 3); 16 | } 17 | else{ 18 | A[2] -= (2 * A[0]); 19 | res = res + min(A[1], (A[1] + A[2]) / 3); 20 | } 21 | 22 | cout << res << endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/489C.cpp: -------------------------------------------------------------------------------- 1 | //http://codeforces.com/problemset/problem/489/C 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | int main () 9 | { 10 | int m,s,i,k; 11 | 12 | cin>>m>>s; 13 | // check if possible or not 14 | if(s<1&&m>1||s>m*9) 15 | cout<<-1<<' '<<-1<=0;i--) 19 | { 20 | int j=max(0,k-9*i); 21 | if(j==0&&i==m-1&&k) 22 | j=1; 23 | cout<=0;i--) 29 | { 30 | int j=min(9,k); 31 | cout< 2 | 3 | int main() 4 | { 5 | int N; 6 | scanf("%d",&N); 7 | int A[N],B[N]; 8 | int i; 9 | for(i=0; i 2 | int P,X,Y; 3 | int chk(int S) 4 | { 5 | int i=S/50; 6 | int cnt; 7 | for(cnt=1; cnt<=25; cnt++) 8 | { 9 | i=((i*96)+42)%475; 10 | if((26+i)==P)return 1; 11 | } 12 | return 0; 13 | } 14 | int main() 15 | { 16 | scanf("%d %d %d", &P, &X, &Y); 17 | int i; int ANS=0; 18 | int copy=X; 19 | while(copy>=Y) 20 | { 21 | if(chk(copy)) 22 | { 23 | printf("0\n"); 24 | return 0; 25 | } 26 | copy-=50; 27 | } 28 | for(i=0; i<=500; i++) 29 | { 30 | if(chk(X+(50*i))) 31 | { 32 | ANS=(i/2)+(i%2); 33 | break; 34 | } 35 | } 36 | printf("%d\n", ANS); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/807C.c: -------------------------------------------------------------------------------- 1 | #include 2 | long long x,y,p,q; 3 | int T; 4 | long long _max(long long a, long long b) 5 | { 6 | if(a>b) 7 | return a; 8 | return b; 9 | } 10 | int main() 11 | { 12 | scanf("%d",&T); 13 | while(T--) 14 | { 15 | scanf("%lld %lld %lld %lld",&x,&y,&p,&q); 16 | if((x*1.0/y)==(p*1.0/q)) 17 | { 18 | printf("0\n"); 19 | continue; 20 | } 21 | if(p==0) 22 | { 23 | if(x==0) 24 | printf("0\n"); 25 | else printf("-1\n"); 26 | continue; 27 | } 28 | if(q==p) 29 | { 30 | printf("-1\n"); 31 | continue; 32 | } 33 | long long x_prime=_max((x/p)+((x%p)==0?0:1),_max((y/q)+((y%q)==0?0:1),((y-x)/(q-p))+((y-x)%(q-p)==0?0:1))); 34 | long long ans=(q*x_prime)-y; 35 | printf("%lld\n", ans); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/897A.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int N,M; 6 | scanf("%d%d",&N,&M); 7 | char S[N+5]; 8 | scanf("%s",S); 9 | while(M--) 10 | { 11 | int l,r; 12 | char c1,c2; 13 | scanf("%d %d ",&l,&r); 14 | scanf("%c %c",&c1,&c2); 15 | int i; 16 | for(i=0; i=(l-1) && i<=(r-1) && S[i]==c1) 19 | S[i]=c2; 20 | } 21 | } 22 | printf("%s\n", S); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/898A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | long long N; 6 | cin>>N; 7 | int iiith=(N%10); 8 | if(iiith<=5)cout< 2 | using namespace std; 3 | int main() 4 | { 5 | long long n,a,b; 6 | cin>>n>>a>>b; 7 | for(int x=0; ; x++) 8 | { 9 | long long lol=x*a; 10 | if(lol>n)break; 11 | long long bol=(n-lol); 12 | if((bol%b)==0) 13 | { 14 | cout<<"YES\n"; 15 | cout< 2 | using namespace std; 3 | int A[3]; 4 | int main() 5 | { 6 | int N; 7 | cin>>N; 8 | int tmp; 9 | for(int i=1; i<=N; i++) 10 | { 11 | cin>>tmp; 12 | A[tmp]++; 13 | } 14 | int ANS=min(A[1],A[2]); 15 | A[1]-=ANS; 16 | A[2]-=ANS; 17 | ANS=ANS+(A[1]/3); 18 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | long long N; 6 | cin>>N; 7 | long long sum=(N*(N+1))/2; 8 | if((sum%2)==0) 9 | { 10 | cout<<"0\n"; 11 | long long T=sum/2; 12 | //cout< F; 14 | int r=N; 15 | while(T>r) 16 | { 17 | F.push_back(r); 18 | T-=r; 19 | r--; 20 | } 21 | if(T!=0) 22 | F.push_back(T); 23 | int L=F.size(); 24 | cout< F; 33 | int r=N; 34 | while(T>r) 35 | { 36 | F.push_back(r); 37 | T-=r; 38 | r--; 39 | } 40 | if(T!=0) 41 | F.push_back(T); 42 | int L=F.size(); 43 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int N; 8 | cin>>N; 9 | int neg=0; int pos=0; 10 | while(N--) 11 | { 12 | long long X,Y; 13 | cin>>X>>Y; 14 | if(X<0)neg++; 15 | else pos++; 16 | } 17 | if(neg<=1 || pos<=1)cout<<"Yes\n"; 18 | else cout<<"No\n"; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/900B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int c; 6 | long long a,b; 7 | cin>>a>>b>>c; 8 | int i; 9 | long long carry=(a%b); 10 | for(i=1; i<=10000000; i++) 11 | { 12 | carry*=10; 13 | int dig=(carry/b)%10; 14 | if(dig==c) 15 | { 16 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int N,M; 6 | cin>>N>>M; 7 | int X[N],Y[N]; 8 | for(int i=0; i>X[i]>>Y[i]; 11 | } 12 | if(X[0]!=0){ 13 | cout<<"NO\n"; 14 | return 0; 15 | } 16 | int pt=Y[0]; 17 | for(int i=1; i=X[i-1] && X[i]<=pt) 20 | { 21 | pt=max(pt,Y[i]); 22 | } 23 | } 24 | if(pt>=M)cout<<"YES\n"; 25 | else cout<<"NO\n"; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/902B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector edges[100005]; 4 | int N,u,v; 5 | int C[100005]; 6 | bool visited[100005]; 7 | int ANS=0; 8 | int dfs(int node, int color) 9 | { 10 | if(visited[node])return 0; 11 | visited[node]=true; 12 | int tmp=0; 13 | if(color!=C[node])tmp++; 14 | int L=edges[node].size(); 15 | for(int i=0; i>N; 24 | for(int i=2; i<=N; i++) 25 | { 26 | cin>>u; 27 | edges[u].push_back(i); 28 | edges[i].push_back(u); 29 | } 30 | for(int i=1; i<=N; i++) 31 | { 32 | cin>>C[i]; 33 | } 34 | for(int i=1; i<=N; i++)visited[i]=false; 35 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int T; 6 | cin>>T; 7 | while(T--) 8 | { 9 | int X; 10 | cin>>X; 11 | int flag=0; 12 | for(int i=0; i<=100; i++) 13 | { 14 | for(int j=0; j<=100; j++) 15 | { 16 | if(((3*i)+(7*j))==X) 17 | { 18 | flag=1; 19 | } 20 | } 21 | } 22 | if(flag) 23 | cout<<"YES\n"; 24 | else 25 | cout<<"NO\n"; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/903B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int h1,a1,c; 6 | cin>>h1>>a1>>c; 7 | int h2,a2; 8 | cin>>h2>>a2; 9 | vector ans; 10 | while(h2>0) 11 | { 12 | if((h2-a1)<=0) 13 | { 14 | ans.push_back("STRIKE"); 15 | h2-=a1; 16 | break; 17 | } 18 | if((h1-a2)>0) 19 | { 20 | ans.push_back("STRIKE"); 21 | h2-=a1; 22 | h1-=a2; 23 | } 24 | else 25 | { 26 | ans.push_back("HEAL"); 27 | h1+=c; 28 | h1-=a2; 29 | } 30 | } 31 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int N; 6 | cin>>N; 7 | long long tmp; 8 | map H; 9 | map :: iterator it; 10 | for(int i=1; i<=N; i++) 11 | { 12 | cin>>tmp; 13 | if(H.find(tmp)==H.end())H[tmp]=1; 14 | else H[tmp]++; 15 | } 16 | int MAX=0; 17 | for(it=H.begin(); it!=H.end(); ++it) 18 | { 19 | MAX=max(MAX,it->second); 20 | } 21 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int A,B,C,D; 6 | cin>>A>>B>>C>>D; 7 | int smaller_end=min(2*C,2*D); 8 | int bigger_start=max(C,D); 9 | if(D>=A || D>=B || bigger_start>smaller_end){ 10 | cout<<"-1\n"; 11 | } 12 | else 13 | { 14 | if(bigger_start>=(2*B) || bigger_start>=(2*A)) 15 | { 16 | cout<<"-1\n"; 17 | } 18 | else cout<<(2*A)<<"\n"<<(2*B)<<"\n"<0) 30 | { 31 | String tmp=W[j]; 32 | W[j]=W[j+1]; 33 | W[j+1]=tmp; 34 | } 35 | } 36 | } 37 | System.out.println(W[0]); 38 | } 39 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/909B.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int N; 4 | vector X; 5 | vector Y; 6 | int main() 7 | { 8 | cin>>N; 9 | for(int i=0; i=PT && visited[j]==false){ 31 | visited[j]=true; 32 | PT=Y[j]; 33 | //cout<>N; 10 | for(int i=0; i>A[i]; 11 | dp[0][0]=1; 12 | for(int i=1; i>N>>M; 8 | two[0]=1; int cnt; 9 | for(int i=1; ; i++) 10 | { 11 | two[i]=two[i-1]*2; 12 | if(two[i]>=100000000) 13 | { 14 | cnt=i; 15 | break; 16 | } 17 | } 18 | if(N<=cnt){ 19 | cout<<(M%two[N])< edges[100005]; 6 | int leaf[100005]; 7 | void dfs(int node) 8 | { 9 | int L=edges[node].size(); 10 | if(L==0)return; 11 | int cnt=0; 12 | for(int i=0; i>N; 27 | for(int i=2; i<=N; i++) 28 | { 29 | int tmp; 30 | cin>>tmp; 31 | edges[tmp].push_back(i); 32 | } 33 | for(int i=1; i<=N; i++) 34 | { 35 | if(edges[i].size()==0)leaf[i]=1; 36 | } 37 | dfs(1); 38 | if(flag)cout<<"Yes\n"; 39 | else cout<<"No\n"; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/914A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN=1005; 4 | int N; 5 | long long A[MAXN]; 6 | int main() 7 | { 8 | cin>>N; 9 | long long ANS=LONG_LONG_MIN; 10 | for(int i=0; i>TMP; 14 | double sqrT=sqrt(TMP); 15 | long long ch=(long long)sqrT; 16 | if((ch*ch)==TMP){ 17 | continue; 18 | } 19 | else{ 20 | ANS=max(ANS,TMP); 21 | } 22 | } 23 | cout<>N; 7 | long long A[N]; 8 | for(int i=0; i>A[i]; 10 | sort(A,A+N); 11 | int even[N]; 12 | int odd[N]; 13 | for(int i=0; i> N >> K; 9 | for(int i = 0; i < N; ++i) 10 | cin >> A[i]; 11 | int l = 0; 12 | int r = 0; 13 | long long p = A[0]; 14 | long long s = A[0]; 15 | long long res = 0; 16 | while(l <= r && r < N){ 17 | while(r < (N - 1) && (p * 1.0/ s) < K){ 18 | r++; 19 | s += A[r]; 20 | p *= A[r]; 21 | cout << (p *1.0/ s) << endl; 22 | } 23 | //cout << (p/s) << endl; 24 | //cout << l << " " << r << endl; 25 | if((p * 1.0 / s) == K) 26 | res++; 27 | l++; 28 | p = p / A[l - 1]; 29 | s = s - A[l - 1]; 30 | } 31 | cout << res << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/932C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N,A,B; 7 | cin>>N>>A>>B; 8 | int cycle1=-1; 9 | int cycle2=-1; 10 | for(int i=0; i<=(N/A); i++){ 11 | if((N-(A*i))%B == 0){ 12 | cycle1=i; 13 | cycle2=(N-(A*i))/B; 14 | break; 15 | } 16 | } 17 | //cout<<(N%A)%B << " "<<(N%B)%A <>N>>M; 8 | long long A[N], B[M]; 9 | for(int i=0; i>A[i]; 10 | for(int i=0; i>B[i]; 11 | sort(A,A+N); 12 | sort(B,B+M); 13 | long long ans=LONG_LONG_MAX; 14 | for(int i=0; i>K; 8 | if((K/2)<18){ 9 | 10 | while((K/2)>0){ 11 | cout<<"8"; 12 | K-=2; 13 | } 14 | if((K%2)==1)cout<<"4"; 15 | cout<0){ 19 | cout<<"8"; 20 | K-=2; 21 | } 22 | cout< 3 | #include 4 | 5 | using namespace std; 6 | 7 | int l, w; // l < w range 10^5 8 | int a[100001]; // size w-1 9 | int sum[100001]; // size w-l 10 | 11 | // e.g: if l=1, then then max no of frogs that reach = min a[i] 12 | // logic: find th smallest sum segment of size l 13 | 14 | 15 | int main() 16 | { 17 | cin >> w >> l; 18 | for(int i = 1; i <= w-1; i++) 19 | cin >> a[i]; 20 | 21 | sum[1] = 0; 22 | 23 | for(int i=1; i <= l; i++) 24 | sum[1] += a[i]; 25 | 26 | for(int i=2; i <= w-l; i++) 27 | { 28 | // filling in the sum segments in sum array 29 | sum[i] = sum[i-1] + a[l+i-1] - a[i-1]; 30 | // is this correct ? 31 | } 32 | int min = sum[1]; 33 | for(int i=2; i <= w-l; i++) 34 | { 35 | if(sum[i] < min) 36 | min = sum[i]; 37 | } 38 | // find the min of sum array 39 | cout << min << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Codeforces/Solved Problems/977A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long N; 7 | int K; 8 | cin>>N>>K; 9 | for(int i=1; i<=K; i++){ 10 | if((N%10)==0)N/=10; 11 | else N--; 12 | } 13 | cout<>L; 10 | cin>>S; 11 | 12 | for(char i='A'; i<='Z'; i++){ 13 | for(char j='A'; j<='Z'; j++){ 14 | int f=0; 15 | for(int k=0; k<(L-1); k++){ 16 | if(S[k] == i && S[k+1] == j)f++; 17 | } 18 | if(f>MAXA){ 19 | MAXA = f; 20 | x = i; 21 | y = j; 22 | } 23 | } 24 | } 25 | cout<<(char)x<<(char)y<>N>>K; 8 | long long A[N]; 9 | for(int i=0; i>A[i]; 10 | sort(A,A+N); 11 | if(K == 0 && A[0]>1){ 12 | cout<<"1\n"; 13 | return 0; 14 | } 15 | if(K == 0){ 16 | cout<<"-1\n"; 17 | return 0; 18 | } 19 | if(K == N || A[K]!=A[K-1]){ 20 | cout<>S; 7 | int L=S.size(); 8 | 9 | int dash = 0; 10 | for(int i=0; i>N>>K; 23 | for(int i=0; i<256; i++)A[i] = -1; 24 | for(int i=0; i>T; 26 | if(A[T]!=-1){ 27 | cout<=0; j--) 33 | { 34 | if(A[j]!=-1) 35 | { 36 | end_idx = j; 37 | break; 38 | } 39 | } 40 | if(end_idx == -1) 41 | { 42 | upd(max(0, T-K+1), T); 43 | } 44 | else 45 | { 46 | int st_idx = st(A[end_idx]); 47 | if((T-st_idx+1) <=K) 48 | upd(st_idx,T); 49 | else 50 | upd(max(T-K+1 , end_idx + 1), T); 51 | } 52 | cout< N: 18 | return 1 19 | elif N > high_limit: 20 | return 0 21 | 22 | ans = -1 23 | low = 0 24 | high = 1000000000000000000 25 | #Guessing the minimum number of positions 26 | while high >= low: 27 | mid = (high + low)//2; 28 | if chk(mid) == 1: 29 | ans = mid 30 | high = mid - 1 31 | else: 32 | low = mid + 1 33 | 34 | print(ans) -------------------------------------------------------------------------------- /Codeforces/Solved Problems/987A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | int N; 4 | string A[105]; 5 | bool chk(string col) 6 | { 7 | for(int i=0; i>N; 18 | for(int i=0; i>A[i]; 20 | int cnt = 0; 21 | vector ret; 22 | if(chk("purple") == false) 23 | { 24 | cnt++; 25 | ret.push_back("Power"); 26 | } 27 | if(chk("green") == false) 28 | { 29 | cnt++; 30 | ret.push_back("Time"); 31 | } 32 | if(chk("blue") == false) 33 | { 34 | cnt++; 35 | ret.push_back("Space"); 36 | } 37 | if(chk("orange") == false) 38 | { 39 | cnt++; 40 | ret.push_back("Soul"); 41 | } 42 | if(chk("red") == false) 43 | { 44 | cnt++; 45 | ret.push_back("Reality"); 46 | } 47 | if(chk("yellow") == false) 48 | { 49 | cnt++; 50 | ret.push_back("Mind"); 51 | } 52 | cout<" 13 | else: 14 | print "<" -------------------------------------------------------------------------------- /Codeforces/Solved Problems/987C.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 3001; 4 | const long long inf = 1e14; 5 | long long S[MAXN]; 6 | long long C[MAXN]; 7 | long long dp[MAXN][MAXN][3]; 8 | int N; 9 | long long rec(int ind, int last, int cnt) 10 | { 11 | if(cnt == 3) 12 | return 0; 13 | if(ind == (N+1)) 14 | { 15 | if(cnt == 3) 16 | return 0; 17 | else 18 | return inf; 19 | } 20 | if(dp[ind][last][cnt] != -1) 21 | return dp[ind][last][cnt]; 22 | 23 | dp[ind][last][cnt] = rec(ind + 1, last, cnt); 24 | if(S[last] < S[ind]) 25 | dp[ind][last][cnt] = min(dp[ind][last][cnt], C[ind]*1LL + rec(ind + 1, ind, cnt + 1)); 26 | return dp[ind][last][cnt]; 27 | } 28 | 29 | int main() 30 | { 31 | cin>>N; 32 | for(int i=1; i<=N; i++) 33 | cin>>S[i]; 34 | for(int i=1; i<=N; i++) 35 | cin>>C[i]; 36 | for(int i=0; i>N; 8 | int A[N+1]; 9 | int idx[N+1]; 10 | for(int i=1; i<=N; i++) 11 | { 12 | cin>>A[i]; 13 | idx[A[i]] = i; 14 | } 15 | int cnt = 0; 16 | for(int i=1; i<=N; i++) 17 | { 18 | if(A[i] == i)continue; 19 | //Get i here 20 | int tmp_idx = idx[i]; 21 | int tmp_val = A[i]; 22 | swap(A[i], A[idx[i]]); 23 | idx[i] = i; 24 | idx[tmp_val] = tmp_idx; 25 | cnt++; 26 | } 27 | if((cnt%2) == (3*N)%2) 28 | cout<<"Petr\n"; 29 | else 30 | cout<<"Um_nik\n"; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/991A.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | long long A,B,C,N; 7 | cin>>A>>B>>C>>N; 8 | long long pass = A + B - C; 9 | long long fail = N - pass; 10 | if( (A < C) || (B < C) || fail <= 0 || pass < 0 || (A == 0 && C!=0) || (B == 0 && C!=0) ) 11 | cout<<"-1\n"; 12 | else 13 | cout<>N; 8 | long long A[N]; 9 | for(int i=0; i>A[i]; 10 | long long cum[N]; 11 | sort(A, A+N); 12 | cum[0] = A[0]; 13 | for(int i=1; i= 5) 18 | { 19 | cout<<"0\n"; 20 | return 0; 21 | } 22 | for(int i=0; i= 5) 26 | { 27 | cout<<(i+1)<>N; 11 | for(int i=0; i>t; 15 | exists[t + 100000] = 1; 16 | } 17 | int cnt = 0; 18 | for(int i=0; i<=200000; i++) 19 | { 20 | if(i == 100000)continue; 21 | cnt+=exists[i]; 22 | } 23 | cout<>X>>K; 19 | if(X == 0){ 20 | cout<<"0\n"; 21 | return 0; 22 | } 23 | long long two_power_k = fastmod(2, K); 24 | X = X%MOD; 25 | long long aaa = ((((two_power_k*two_power_k)%MOD)*X)%MOD) - ((((two_power_k*(two_power_k-1+MOD))%MOD)*fastmod(2, MOD - 2))%MOD); 26 | if(aaa < 0 ) 27 | aaa += MOD; 28 | aaa*=2; 29 | aaa = aaa%MOD; 30 | aaa = (aaa * fastmod(two_power_k, MOD - 2))%MOD; 31 | cout<>N>>M; 10 | for(int i=0; i>A[i]; 12 | for(int i=0; i>x; 16 | exists[x] = 1; 17 | } 18 | for(int i=0; i>N; 8 | long long den = N/100; 9 | N = N % 100; 10 | den += (N/20); 11 | N = N % 20; 12 | den += (N/10); 13 | N = N % 10; 14 | den += (N/5); 15 | N = N % 5; 16 | den += N; 17 | N = 0; 18 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int N; 6 | cin>>N; 7 | long long A[N]; 8 | long long minimum = LONG_LONG_MAX; 9 | for(int i=0; i>A[i]; 11 | minimum = min(minimum, A[i]); 12 | } 13 | int moves = minimum; 14 | int start = minimum % N; 15 | int pos = -1; 16 | for(int i = start; i>N; 8 | int A[2*N]; 9 | for(int i=0; i<(2 * N); i++) 10 | { 11 | cin>>A[i]; 12 | } 13 | int ret = 0; 14 | for(int i=0; i<(2 * N); i+=2) 15 | { 16 | for(int j = i + 1; j<(2 * N); j++) 17 | { 18 | if(A[i] == A[j]) 19 | { 20 | for(int k = j - 1; k > i; k--){ 21 | swap(A[k], A[k + 1]); 22 | ret++; 23 | } 24 | break; 25 | } 26 | } 27 | 28 | } 29 | cout<>N>>X>>Y; 11 | int cnt = 0; 12 | for(int i=0; i>x; 16 | A[cnt] = x - '0'; 17 | if(i == 0 || A[cnt] != A[cnt - 1]) 18 | cnt++; 19 | } 20 | int zeroes = 0; 21 | for(int i=0; i A; 6 | 7 | int main() 8 | { 9 | cin>>N>>K; 10 | for (int i = 1; i <=N; ++i) 11 | { 12 | long long tmp; 13 | cin>>tmp; 14 | A.push_back(tmp); 15 | } 16 | int ret = 0; 17 | while(A.empty() == false && (A.front() <= K || A.back() <= K)) 18 | { 19 | if(A.front() <= K) 20 | { 21 | A.pop_front(); 22 | ret++; 23 | } 24 | else 25 | { 26 | A.pop_back(); 27 | ret++; 28 | } 29 | } 30 | cout<>N; 22 | cin>>S; 23 | for(int i=1; i<=N; i++) 24 | { 25 | if((N%i) == 0) 26 | rev(0, i-1); 27 | } 28 | for(int i=0; i>N>>K; 11 | for(int i=0; i>S[i]; 14 | freq[S[i] - 'a']++; 15 | } 16 | int cnt = 0; 17 | while(K > 0) 18 | { 19 | if(freq[cnt] < K) 20 | { 21 | take[cnt] = freq[cnt]; 22 | K -= freq[cnt]; 23 | cnt++; 24 | } 25 | else 26 | { 27 | take[cnt] = K; 28 | K = 0; 29 | 30 | } 31 | } 32 | for(int i=0; i 0) 35 | { 36 | take[S[i]-'a']--; 37 | S[i]=' '; 38 | } 39 | } 40 | for(int i=0; i> S; 7 | int ans = 0; 8 | int L = S.size(); 9 | for(int i = 0; i < L; i += 3){ 10 | if(S[i] != 'P') 11 | ans++; 12 | } 13 | for(int i = 1; i < L; i += 3){ 14 | if(S[i] != 'E') 15 | ans++; 16 | } 17 | for(int i = 2; i < L; i += 3){ 18 | if(S[i] != 'R') 19 | ans++; 20 | } 21 | cout << ans << endl; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/Count The Blocks.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const long long MOD = 998244353; 4 | const int MAXN = 2e5 + 5; 5 | long long int P[MAXN]; 6 | int main() 7 | { 8 | int N; 9 | cin >> N; 10 | if(N == 1){ 11 | cout << "10\n"; 12 | return 0; 13 | } 14 | P[0] = 1; 15 | for(int i = 1; i < MAXN; ++i) 16 | P[i] = (P[i - 1] * 10) % MOD; 17 | 18 | for(int i = 1; i <= N; ++i){ 19 | long long a = 0; long long b; 20 | if((N - i - 1) >= 0) 21 | a = (((9LL * 9LL * P[N - i - 1]) % MOD) * 1LL * (N - i - 1)) % MOD; 22 | else 23 | { 24 | cout << "10 "; 25 | continue; 26 | } 27 | b = (9LL * P[N - i]) % MOD; 28 | if((N - i + 1) > 1) 29 | b = (b * 2) % MOD; 30 | cout << ((a + b) % MOD) << " "; 31 | } 32 | cout << "\n"; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Codeforces/Solved Problems/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/Codeforces/Solved Problems/a.out -------------------------------------------------------------------------------- /Facebook HackerCup/Facebook HackerCup 2018/Qualification/Interception.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | const int MAXN = 55; 5 | 6 | int N; 7 | int P[MAXN]; 8 | 9 | int main() 10 | { 11 | ifstream cin("interception.txt"); 12 | ofstream cout("Interception.out"); 13 | int T; 14 | cin>>T; 15 | for(int cases = 1; cases <= T; cases++) 16 | { 17 | cin>>N; 18 | for(int i = 0; i <= N; i++) 19 | { 20 | cin>>P[i]; 21 | } 22 | if((N%2) == 0) 23 | { 24 | cout<<"Case #"<>T; 13 | int test = 1; 14 | while(T--){ 15 | cout<<"Case #"<>N>>K>>V; 17 | for(int i = 0; i < N; i++){ 18 | cin>>A[i]; 19 | print[i] = 0; 20 | } 21 | long long idx = (V - 1) * 1LL * K; 22 | idx = (idx - 1)%N; 23 | idx++; 24 | if(idx == N) 25 | idx = 0; 26 | for(int i = 1; i <= K; i++) 27 | { 28 | print[idx++] = 1; 29 | if(idx >= N) 30 | idx = 0; 31 | } 32 | for(int i = 0 ; i < N; i++) 33 | if(print[i])cout<> T; 10 | int cases = 1; 11 | while(T--){ 12 | string S; 13 | cin >> S; 14 | int L = S.size(); 15 | int c = 0; 16 | for(int i = 0; i < L; ++i) 17 | { 18 | if(S[i] == 'B') 19 | c++; 20 | } 21 | if((c >= (L / 2)) && (c <= (L - 2))) 22 | cout << "Case #" << cases << ": Y\n"; 23 | else 24 | cout << "Case #" << cases << ": N\n"; 25 | cases++; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Facebook HackerCup/Facebook HackerCup 2019/Qualification Round/Leapfrog:Ch.2/main.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ifstream cin("leapfrog_ch_.txt"); 7 | ofstream cout("1.out"); 8 | int T; 9 | cin >> T; 10 | int cases = 1; 11 | while(T--){ 12 | string S; 13 | cin >> S; 14 | int L = S.size(); 15 | int c = 0; 16 | for(int i = 0; i < L; ++i) 17 | { 18 | if(S[i] == 'B') 19 | c++; 20 | } 21 | if((c >= min(2, (L / 2)) && (c <= (L - 2)))) 22 | cout << "Case #" << cases << ": Y\n"; 23 | else 24 | cout << "Case #" << cases << ": N\n"; 25 | cases++; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Facebook HackerCup/Facebook HackerCup 2019/Round1/Class Treasurer/main.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const long long MOD = 1000000007; 4 | const int MAXN = 1000005; 5 | long long p[MAXN]; 6 | int T, N, K; 7 | string S; 8 | int main() 9 | { 10 | p[0] = 1; 11 | for(int i = 1; i < MAXN; ++i) 12 | p[i] = (p[i - 1] * 2) % MOD; 13 | cin >> T; 14 | int cases = 1; 15 | while(T--){ 16 | cin >> N >> K; 17 | int cum = 0; 18 | long long res = 0; 19 | cin >> S; 20 | for(int i = (N - 1); i >= 0; --i){ 21 | if(S[i] == 'A') 22 | cum--; 23 | else if(S[i] == 'B') 24 | cum++; 25 | if(cum == (K + 1)){ 26 | cum -= 2; 27 | res = (res + p[i + 1]) % MOD; 28 | } 29 | cum = max(0, cum); 30 | } 31 | cout << "Case #" << cases<<": " << res << endl; 32 | cases++; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Google Code Jam/GCJ 2020/Nesting Depth.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | for(int cases = 1; cases <= T; ++cases){ 9 | string s; 10 | cin >> s; 11 | int l = s.size(); 12 | int curr_depth = 0; 13 | cout << "Case #" << cases << ": "; 14 | for(int i = 0; i < l; ++i){ 15 | int c = s[i] - '0'; 16 | while(c > curr_depth){ 17 | cout << "("; 18 | curr_depth++; 19 | } 20 | while(curr_depth > c){ 21 | cout << ")"; 22 | curr_depth--; 23 | } 24 | cout << s[i]; 25 | } 26 | while(curr_depth > 0) 27 | { 28 | cout << ")"; 29 | curr_depth--; 30 | } 31 | cout << endl; 32 | } 33 | } -------------------------------------------------------------------------------- /Google Code Jam/GCJ 2020/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/Google Code Jam/GCJ 2020/a.out -------------------------------------------------------------------------------- /Google Kickstart/Google KickStart 2019/Practice_Round/Number_Guessing.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | cin >> T; 8 | while(T--) 9 | { 10 | long long int A, B; 11 | cin >> A >> B; 12 | int N; 13 | cin >> N; 14 | long long low = A; long long high = B; 15 | long long mid; 16 | while(high >= low) 17 | { 18 | mid = (high + low) / 2; 19 | cout << mid << endl; 20 | string s; 21 | cin >> s; 22 | if(s == "CORRECT") 23 | break; 24 | if(s == "TOO_SMALL") 25 | low = mid + 1; 26 | else 27 | high = mid - 1; 28 | } 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Google Kickstart/Google KickStart 2019/RoundA/Training.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | const int MAXN = 1e5 + 5; 4 | long long A[MAXN]; 5 | long long B[MAXN]; 6 | long long DP[MAXN]; 7 | int N, P, T; 8 | int main() 9 | { 10 | cin >> T; 11 | int cases = 1; 12 | while(T--) 13 | { 14 | cin >> N >> P; 15 | for(int i = 0; i < N; ++i) 16 | cin >> B[i]; 17 | sort(B, B + N); 18 | for(int i = 1; i <= N; ++i) 19 | A[i] = B[i - 1]; 20 | DP[0] = 0; 21 | for(int i = 1; i < P; ++i) 22 | DP[i] = DP[i - 1] + A[i]; 23 | long long ans = LONG_LONG_MAX; 24 | for(int i = P; i <= N; ++i) 25 | { 26 | DP[i] = DP[i - 1] + A[i]; 27 | ans = min(ans, (A[i] * (P - 1)) - (DP[i] - DP[i - P] - A[i])); 28 | } 29 | cout << "Case #" << cases << ": " << ans << endl; 30 | cases++; 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Google Kickstart/Google KickStart 2019/RoundA/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/Google Kickstart/Google KickStart 2019/RoundA/a.out -------------------------------------------------------------------------------- /Google Kickstart/Google KickStart 2020/Round A/Allocation.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | using namespace std; 3 | 4 | int main(){ 5 | int T; 6 | cin >> T; 7 | for(int cases = 1; cases <= T; cases++){ 8 | int N, B; 9 | cin >> N >> B; 10 | int A[N]; 11 | for(int i = 0; i < N; ++i) 12 | cin >> A[i]; 13 | sort(A, A + N); 14 | int res = 0; 15 | for(int i = 0; i < N; ++i){ 16 | if(B >= A[i]){ 17 | res++; 18 | B -= A[i]; 19 | } 20 | } 21 | cout << "Case #" << cases << ": " << res << endl; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Google Kickstart/Google KickStart 2020/Round A/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swetanjal/Competitive-Programming/3954fb09d7527de91cf4d999cdfbbc23c0de65a3/Google Kickstart/Google KickStart 2020/Round A/a.out -------------------------------------------------------------------------------- /IOI/India/IARCS OPC Judge/01TILES/0_1 Tiles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int dp[1000005]; 4 | int N; 5 | int main() 6 | { 7 | cin>>N; 8 | dp[1]=1; 9 | dp[2]=2; 10 | for(int i=3; i<=N; i++) 11 | dp[i]=(dp[i-1]+dp[i-2])%15746; 12 | cout< 2 | using namespace std; 3 | int A,B,N,tmp; 4 | int main() 5 | { 6 | priority_queue > P; 7 | cin>>A>>B; 8 | N=A+B; 9 | while(N--) 10 | { 11 | cin>>tmp; 12 | if(tmp==-1){ 13 | cout< 2 | using namespace std; 3 | int N,T; 4 | int A[1005]; 5 | int main() 6 | { 7 | cin>>N>>T; 8 | for(int i=1; i<=T; i++) 9 | { 10 | for(int j=0; j>A[j]; 11 | next_permutation(A,A+N); 12 | for(int j=0; j 2 | using namespace std; 3 | int N,K,A[10005]; 4 | long long dp[10005][105]; 5 | long long rec(int ind, int cnt) 6 | { 7 | if(cnt>K)return 0; 8 | if(ind==N)return 0; 9 | if(dp[ind][cnt]!=-1)return dp[ind][cnt]; 10 | dp[ind][cnt]=max(A[ind]+rec(ind+1,cnt),rec(ind+1,cnt+1)); 11 | return dp[ind][cnt]; 12 | } 13 | int main() 14 | { 15 | cin>>N>>K; 16 | for(int i=0; i>A[i]; 17 | for(int i=0; i<=N; i++)for(int j=0; j<=K; j++)dp[i][j]=-1; 18 | cout< 2 | using namespace std; 3 | int N,sum=0; 4 | int A[5005],dp[5005][5005]; 5 | int rec(int ind, int last) 6 | { 7 | if(ind==N)return 0; 8 | if(dp[ind][last]!=-1)return dp[ind][last]; 9 | if(A[ind]>=last) 10 | { 11 | dp[ind][last]=max(rec(ind+1,last),A[ind]+rec(ind+1,A[ind])); 12 | } 13 | else 14 | { 15 | dp[ind][last]=rec(ind+1,last); 16 | } 17 | return dp[ind][last]; 18 | } 19 | int main() 20 | { 21 | cin>>N; 22 | for(int i=0; i>A[i]; 25 | sum+=A[i]; 26 | } 27 | for(int i=0; i<=N; i++)for(int j=0; j<=5000; j++)dp[i][j]=-1; 28 | int LIS=rec(0,0); 29 | cout<<(sum-LIS)< 2 | using namespace std; 3 | int T,N; 4 | int A[100005]; 5 | int dp[100005][2]; 6 | int rec(int ind, int prev) 7 | { 8 | if(ind==N)return 0; 9 | if(dp[ind][prev]!=-1)return dp[ind][prev]; 10 | if(prev==0) 11 | { 12 | dp[ind][prev]=max(A[ind]-1+rec(ind+1,1),rec(ind+1,0)); 13 | } 14 | else 15 | { 16 | dp[ind][prev]=max(abs(A[ind]-A[ind-1])+rec(ind+1,1),abs(A[ind-1]-1)+rec(ind+1,0)); 17 | } 18 | return dp[ind][prev]; 19 | } 20 | int main() 21 | { 22 | cin>>T; 23 | while(T--) 24 | { 25 | cin>>N; 26 | for(int i=0; i>A[i]; 27 | for(int i=0; i 2 | using namespace std; 3 | int T,N,r; 4 | long long t0,s; 5 | priority_queue P[105]; 6 | char c; 7 | int main() 8 | { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(0); 11 | cin>>T; 12 | while(T--) 13 | { 14 | cin>>N; 15 | for(int i=0; i<=100; i++) 16 | { 17 | while(P[i].empty()==false) 18 | P[i].pop(); 19 | } 20 | while(N--) 21 | { 22 | cin>>c; 23 | if(c=='P') 24 | { 25 | cin>>t0>>s>>r; 26 | P[r].push(s-(t0*r)); 27 | } 28 | else 29 | { 30 | long long U; 31 | cin>>U; 32 | int pos=-1; long long val=0; 33 | for(int i=0; i<=100; i++) 34 | { 35 | if(P[i].empty()==false) 36 | { 37 | 38 | long long x=P[i].top()+(U*i); 39 | if(x>=val) 40 | { 41 | val=x; 42 | pos=i; 43 | } 44 | } 45 | } 46 | P[pos].pop(); 47 | cout< 2 | using namespace std; 3 | const long long MOD=2012; 4 | string S; int N; 5 | map < pair > , long long > hash; 6 | long long rec(int ind, int op1, int op2) 7 | { 8 | if(ind==N){ 9 | if(op1==0 && op2==0)return 1; 10 | return 0; 11 | } 12 | pair p1=make_pair(ind,op1); 13 | pair < int , pair > p2=make_pair(op2,p1); 14 | if(hash.find(p2)!=hash.end())return hash[p2]; 15 | if(S[ind]=='(')return hash[p2]=(rec(ind+1,op1+1,op2)+rec(ind+1,op1,op2+1))%MOD; 16 | else 17 | { 18 | hash[p2]=0; 19 | if(op1>=1)hash[p2]+=rec(ind+1,op1-1,op2)%MOD; 20 | if(op2>=1)hash[p2]+=rec(ind+1,op1,op2-1)%MOD; 21 | } 22 | return hash[p2]; 23 | } 24 | int main() 25 | { 26 | ifstream cin("bbreeds.in"); 27 | ofstream cout("bbreeds.out"); 28 | cin>>S; 29 | N=S.size(); 30 | cout< 2 | using namespace std; 3 | int N,K,A[1005]; 4 | int search(int S) 5 | { 6 | int l=0; int u=N-1; int m; int pos=-1; 7 | while(u>=l) 8 | { 9 | m=(u+l)/2; 10 | if(A[m]<=S) 11 | { 12 | pos=m; 13 | l=m+1; 14 | } 15 | else 16 | { 17 | u=m-1; 18 | } 19 | } 20 | return pos; 21 | } 22 | int main() 23 | { 24 | ifstream cin("diamond.in"); ofstream cout("diamond.out"); 25 | cin>>N>>K; 26 | for(int i=0; i>A[i]; 27 | sort(A,A+N); 28 | int ret=0; 29 | for(int i=0; i 6 | using namespace std; 7 | int N,M; long long C[100005]; int A[5005]; int X[5005]; 8 | long long cum_min[100005]; long long dp[5005]; 9 | int main() 10 | { 11 | //ifstream cin("I.10"); 12 | cin>>N>>M; 13 | for(int i=0; i>A[i]; 14 | sort(A,A+N); 15 | for(int i=1; i<=N; i++)X[i]=A[i-1]; 16 | for(int i=1; i<=M; i++)cin>>C[i]; 17 | cum_min[M]=C[M]; 18 | for(int i=M-1; i>=1; i--)cum_min[i]=min(cum_min[i+1],C[i]); 19 | dp[0]=0; 20 | for(int i=1; i<=N; i++) 21 | { 22 | dp[i]=LONG_LONG_MAX; 23 | for(int j=1; j<=i; j++) 24 | { 25 | dp[i]=min(dp[i],cum_min[X[i]-X[j]+1]+dp[j-1]); 26 | } 27 | } 28 | cout< 7 | using namespace std; 8 | const int MOD=9901; 9 | int dp[105][205]; 10 | int N,K; 11 | int main() 12 | { 13 | ifstream cin("nocows.in"); 14 | ofstream cout("nocows.out"); 15 | cin>>N>>K; 16 | dp[0][1]=0; 17 | for(int i=1; i<=K; i++) 18 | dp[i][1]=1; 19 | for(int i=1; i<=K; i++) 20 | { 21 | for(int j=1; j<=N; j++) 22 | { 23 | for(int cnt=1; cnt 7 | using namespace std; 8 | int N,V; 9 | int A[25]; 10 | long long dp[30][10005]; 11 | long long rec(int ind, int val) 12 | { 13 | if(val<0)return 0; 14 | if(ind>=V)return 0; 15 | //cout<>V>>N; 35 | for(int i=0; i>A[i]; 36 | for(int j=0; j<25; j++)for(int i=0; i<10005; i++)dp[j][i]=-1; 37 | cout< 7 | using namespace std; 8 | int N,n; 9 | int dp[50][(50*51)/2]; 10 | int rec(int ind, int sum) 11 | { 12 | if(sum==0){ 13 | return 1; 14 | } 15 | if(ind==n)return 0; 16 | if(sum<0)return 0; 17 | if(dp[ind][sum]!=-1)return dp[ind][sum]; 18 | return dp[ind][sum]=rec(ind+1,sum)+rec(ind+1,sum-ind); 19 | } 20 | int main() 21 | { 22 | for(int i=0; i<50; i++) 23 | for(int j=0; j<(50*51)/2; j++)dp[i][j]=-1; 24 | ifstream cin("subset.in"); 25 | ofstream cout("subset.out"); 26 | cin>>N; 27 | n=N; 28 | N=(N*(N+1))/4; 29 | if(N*4!=n*(n+1)){ 30 | cout<<"0"< 2 | using namespace std; 3 | int N ; long long K; 4 | long long A[100005]; 5 | bool chk(long long X) 6 | { 7 | long long curr=0; 8 | for(int i=0; iK)return false; 14 | } 15 | return true; 16 | } 17 | long long search() 18 | { 19 | long long pos=-1; long long l=0; long long u=1000000000; long long m; 20 | while(u>=l) 21 | { 22 | m=(u+l)/2; 23 | if(chk(m)){ 24 | l=m+1; 25 | pos=m; 26 | } 27 | else 28 | { 29 | u=m-1; 30 | } 31 | } 32 | if(pos==-1)return 0; 33 | return pos; 34 | } 35 | int main() 36 | { 37 | ifstream cin("fence.in"); 38 | ofstream cout("fence.out"); 39 | cin>>N>>K; 40 | for(int i=0; i>A[i]; 41 | sort(A,A+N); 42 | cout< 2 | using namespace std; 3 | long long eval(long long A, long long B, char x) 4 | { 5 | if(x=='+')return (A+B); 6 | else if(x=='-')return (A-B); 7 | return (A*B); 8 | } 9 | int main() 10 | { 11 | ifstream cin("postfix.in"); 12 | ofstream cout("postfix.out"); 13 | stack S; 14 | string s; 15 | getline(cin,s); 16 | s=s+" "; 17 | int l=s.size(); long long curr=0; 18 | for(int i=0; i='0' && x<='9')curr=(curr*10)+(x-'0'); 22 | else if(x==' '){ 23 | //if(curr!=0) 24 | S.push(curr); 25 | curr=0; 26 | } 27 | else 28 | { 29 | long long X=S.top(); S.pop(); long long Y=S.top(); 30 | long long lol=eval(Y,X,x); 31 | S.pop(); 32 | curr=lol; 33 | //S.push(lol); 34 | } 35 | } 36 | cout<> T; 8 | while(T--){ 9 | int N; 10 | cin >> N; 11 | int A[N]; 12 | int B[N]; 13 | for(int i = 0; i < N; ++i) 14 | cin >> A[i]; 15 | for(int i = 0; i < N; ++i) 16 | cin >> B[i]; 17 | sort(A, A + N); 18 | sort(B, B + N); 19 | int flag = 1; 20 | for(int i = 0; i < N; ++i){ 21 | if(A[i] >= B[i]){ 22 | flag = 0; 23 | break; 24 | } 25 | } 26 | if(flag) 27 | cout << "WIN\n"; 28 | else 29 | cout << "LOSE\n"; 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive Programming 2 | 3 | This repository contains a bunch of interesting problems I have solved in various Programming contests as well as a Codebook which contains Object Oriented implementation of standard Algorithms and Data Structures 4 | 5 | In most of the directories, you will find two subdirectories 6 | - 'Solved Problems' : Contains problems which I could solve myself 7 | - 'Upsolved Problems' : Contains problems which I solved myself after referring to the editorial/solution 8 | 9 | In most Competitive Programming platforms, I go with the username 'swetanjal'. Take a look at my profiles for solutions to more problems solved by me! -------------------------------------------------------------------------------- /Topcoder/SRM 698/RepeatStringEasy_SRM698_Google.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int N; 4 | string S; 5 | int rec(int i, int j) 6 | { 7 | if(i>j)return 0; 8 | if(S[i]==S[j]){ 9 | return rec(i+1,j-1); 10 | } 11 | return 1+rec(i+1,j); 12 | } 13 | class ThePalindrome{ 14 | public: 15 | int find(string lol) 16 | { 17 | S=lol; 18 | N=S.size(); 19 | return (N+rec(0,N-1)); 20 | } 21 | }; 22 | --------------------------------------------------------------------------------