├── ACM_OOP_classes ├── AHU_algorithm.cpp ├── Graph.cpp ├── MaximumFlow.cpp ├── MaximumMatching.cpp ├── MaximumWeightedMatching.cpp ├── MinCostMaxFlow.cpp ├── PushRelabel.cpp ├── SparseTableAncestors.cpp └── StronglyConnectedComponents.cpp ├── Ad-hoc-algorithms ├── Floyd’s Cycle-Finding Algorithm.cpp ├── Mersenne Twister.cpp ├── Timer (simple implementation).cpp └── Timer C++11.cpp ├── BigInteger.cpp ├── Codechef ├── A Story with Strings.cpp ├── A String Game.cpp ├── And Operation.cpp ├── CAO Stage-3.cpp ├── Candidate walk.cpp ├── Chef Anup.cpp ├── Coprime Triples.cpp ├── Delivery Man.cpp ├── Dynamic Inversion.cpp ├── Earthquake.cpp ├── Equality.cpp ├── Exponential Game.cpp ├── Game of Divisors.cpp ├── Harrenhal.cpp ├── Iron Islands.cpp ├── Jack - The Gentleman.cpp ├── Just multiply.cpp ├── Lowest Common Ancestor.cpp ├── Lucky Fives.cpp ├── Many bananas.cpp ├── Most Distant Points.cpp ├── No Unpaired Chefs.cpp ├── Number Game Revisited.cpp ├── Painting the Boxes.cpp ├── Palindromeness.cpp ├── Permutation Shuffle.cpp ├── Piece of cake.cpp ├── Queries On Tree.cpp ├── Rich Rhyme.cpp ├── Saving a gift of love.cpp ├── Shift The String.cpp ├── Shooting.cpp ├── Shortest Path in Binary Trees.cpp ├── Simple Editor.cpp ├── Socializing Game around Pizza.cpp ├── String Matching.cpp ├── Sum Of Binomial Coefficent.cpp ├── Taxi Driver.cpp ├── Tree Game.cpp ├── Trees and Subtrees.cpp ├── VCS.cpp ├── Wall.cpp ├── XOR with Subset.cpp └── Younger Brother.cpp ├── Codeforces ├── A. Alice and Bob.cpp ├── A. Appleman and Easy Task.cpp ├── A. Caisa and Sugar.cpp ├── A. Ice Skating.cpp ├── A. Keyboard.cpp ├── A. LLPS.cpp ├── A. Laptops.cpp ├── A. Learning Languages.cpp ├── A. Little Pony and Crystal Mine.cpp ├── A. Maximum in Table.cpp ├── A. New Year Transportation.cpp ├── A. Vasya and Socks.cpp ├── B. Appleman and Card Game.cpp ├── B. Array.cpp ├── B. Caisa and Pylons.cpp ├── B. DZY Loves Chemistry.cpp ├── B. Fedya and Maths.cpp ├── B. Little Dima and Equation.cpp ├── B. Little Pony and Sort by Shift.cpp ├── B. Mike and Feet.cpp ├── B. New Year Permutation.cpp ├── B. PFAST Inc..cpp ├── B. Painting Pebbles.cpp ├── B. Password.cpp ├── B. String.cpp ├── B. Tournament.cpp ├── B. Worms.cpp ├── C. Appleman and Toastman.cpp ├── C. Boredom.cpp ├── C. Captain Marmot.cpp ├── C. Cthulhu.cpp ├── C. Deletion of Repeats.cpp ├── C. Gargari and Bishops.cpp ├── C. Little Girl and Maximum Sum.cpp ├── C. Little Pony and Expected Maximum.cpp ├── C. Memory for Arrays.cpp ├── C. New Year Book Reading.cpp ├── C. Present.cpp ├── C. Sereja and Brackets.cpp ├── C. The Brand New Function.cpp ├── D. A Lot of Games.cpp ├── D. Appleman and Tree.cpp ├── D. Common Divisors.cpp ├── D. Connected Components.cpp ├── D. Design Tutorial: Inverse the Problem.cpp ├── D. Flowers.cpp ├── D. Gargari and Permutations.cpp ├── D. Good Substrings.cpp ├── D. Little Girl and Maximum XOR.cpp ├── D. Little Pony and Harmony Chest.cpp ├── D. MUH and Cube Walls.cpp ├── D. Match & Catch.cpp ├── D. Maximum Xor Secondary.cpp ├── D. New Year Santa Network.cpp ├── D. Om Nom and Necklace.cpp ├── D. Palindrome Degree.cpp ├── D. Palindrome pairs.cpp ├── D. Prefixes and Suffixes.cpp ├── D. Roads not only in Berland.cpp ├── D. Tavas and Malekas.cpp ├── D. Tricky Function.cpp ├── D. Vessels.cpp ├── E. Caisa and Tree.cpp ├── E. Civilization.cpp ├── E. Exposition.cpp ├── E. Information Graph.cpp ├── E. Palisection.cpp ├── E. Roland and Rose.cpp ├── E. The Road to Berland is Paved With Good Intentions.cpp ├── E. Tree-String Problem.cpp ├── F. Ant Colony.cpp └── H. Queries for Number of Palindromes.cpp ├── Contest-Classes ├── Bitset.cpp ├── Bloom filter.cpp ├── Chinese remainder theorem.cpp ├── Closest pair of points.cpp ├── Complex.cpp ├── Fast Fourier Transform.cpp ├── Fibonacci.cpp ├── Graph.cpp ├── Input Reader.cpp ├── Link Cut Tree.cpp ├── Matrix.cpp ├── ModInteger.cpp ├── Modular Inverse.cpp ├── Output writer.cpp ├── Palindrome Substrings.cpp ├── Persistent Disjoint-Set-Union.cpp ├── Polynomial Multiplication.cpp ├── Popcount.cpp ├── Radix Sort.cpp ├── SegmentTree.cpp ├── Simple parser.cpp ├── Skiplist indexable dynamic implementation.cpp ├── Skiplist.cpp ├── Splay Tree.cpp ├── Treap-with-Merge-Split.cpp └── WeightedGraph.cpp ├── Data-Structures ├── Binary Indexed Tree (Fenwick Tree).cpp ├── Binomial heap.cpp ├── Fibonacci heap.cpp ├── Hash-Table (std::unordered_map).cpp ├── HashSegmentTree (substring hashes).cpp ├── Indexable Treap (dynamic array).cpp ├── RangeMinimumQuery.cpp ├── Segment Tree (non-recursive, fast implementation).cpp ├── Segment Tree 2D (dynamic implementation).cpp ├── Treap (B.S.T) dynamic implementation.cpp ├── Union-Find (persistent implementation).cpp └── Union-Find.cpp ├── Flows-and-Matchings-Algorithms ├── Maximum flow - Dinic O(N^2*M).cpp ├── Maximum flow - Edmonds Karp O(N*M^2).cpp ├── Maximum flow - Push-Relabel O(N^3) (with gap heuristic).cpp ├── Maximum flow - Push-Relabel O(N^3) (without heuristics).cpp ├── Maximum independent set (bipartite graph).cpp ├── Maximum matching - Hopcroft-Karp O(M*sqrtN) (bipartite graph).cpp ├── Maximum matching in general graph (randomized algorithm - only existence).cpp ├── Minimum cost Maximum flow O(N*M^2*logN)) (alternative implementation).cpp ├── Minimum cost Maximum flow O(N*M^2*logN)).cpp ├── Minimum cost Maximum flow O(N^2*M^2).cpp ├── Minimum cost Maximum matching O(N*M^2*logN).cpp └── Minimum vertex cover (bipartite graph).cpp ├── Game-Theory ├── Acrostic Twins.cpp ├── Euclid's Game.cpp ├── Grundy's game.cpp ├── Grunt.cpp ├── Kayles.cpp ├── Miseré Nim.cpp ├── Mock Turtles.cpp ├── Nim Multiplication.cpp ├── Nim.cpp ├── Nimble.cpp ├── Northcott's game.cpp ├── Rugs (Ruler x Ruler).cpp ├── Ruler.cpp ├── The 100 game.cpp ├── The 21 game.cpp ├── The subtraction game S(1,2,...,k).cpp ├── Turning Corners.cpp ├── Twins.cpp └── Wythoff's game.cpp ├── Geometry-Algorithms ├── Area of a reunion of rectangle O(N*MAX_C) - sweep line.cpp ├── Area of a reunion of rectangle O(N*logMAX_C) - sweep line + inplicit segment tree.cpp ├── Area of a reunion of rectangle O(N*logMAX_C) - sweep line.cpp ├── Area of a simple polygon O(N).cpp ├── Circle from 3 points.cpp ├── Closest pair of points O(NlogN).cpp ├── Convex hull trick.cpp ├── Diameter of a convex polygon O(NlogN) - using rotating callipers.cpp ├── Finding a pair of intersected segments - O(N * logN).cpp ├── Graham scan O(NlogN).cpp ├── Line-Line, Segment-Segment, Line-Segment intersection.cpp ├── Monotone Chain Convex Hull O(NlogN).cpp └── Point Class.cpp ├── Graph-Algorithms ├── Biconnected components + Cut Points + Bridges - Tarjan.cpp ├── Eulerian Cycle STL implementation.cpp ├── Eulerian Cycle Static Implementation.cpp ├── Graph implementation.cpp ├── Maximal Clique in Planar graph.cpp ├── Stable Marriage Problem.cpp ├── Strongly connected component - Kosaraju.cpp └── Strongly connected component - Tarjan.cpp ├── HackerRank ├── A stones game.cpp ├── Chocolate Game.cpp ├── Chocolate in Box.cpp ├── Lexicographic paths.cpp ├── Permutation game.cpp ├── Sansa and XOR.cpp ├── Stone Piles.cpp ├── A Super Hero.cpp ├── Circle City.cpp ├── Mixing proteins.cpp ├── Pangrams.cpp ├── Play on benders.cpp ├── Team Formation.cpp └── Vertical Rooks.cpp ├── Hash-Tables ├── Bloom filter.cpp ├── Double Hashing.cpp ├── Linear probing (CLRS implementation).cpp ├── Linear probing (best implementation).cpp ├── Linear probing (simple implementation).cpp └── Quadratic probing.cpp ├── Infoarena ├── 2sah.cpp ├── 2sec.cpp ├── ADN.cpp ├── APDM.cpp ├── Abc2.cpp ├── Ai.cpp ├── Alinuta.cpp ├── Amedie.cpp ├── Amlei.cpp ├── Antobroasca.cpp ├── Antocod.cpp ├── Arbfind.cpp ├── Arbvalmax.cpp ├── Atac.cpp ├── Biscuiti.cpp ├── Bleach.cpp ├── Boom.cpp ├── Bowling.cpp ├── Carti.cpp ├── Cartonase.cpp ├── Cc.cpp ├── Charlie.cpp ├── ChatNoir.cpp ├── Cifra lipsa.cpp ├── Coco Chanel.cpp ├── Coins.cpp ├── Colier.cpp ├── Color2.cpp ├── Color5.cpp ├── Concurs.cpp ├── Congr.cpp ├── Count.cpp ├── Cri.cpp ├── Cuburi2.cpp ├── Culori.cpp ├── Desc2.cpp ├── Disconnect.cpp ├── Domino.cpp ├── Dragoni.cpp ├── DreptPal.cpp ├── Dtcsu.cpp ├── Easy Choice.cpp ├── Ecotraseu.cpp ├── Ecuatii.cpp ├── Egal.cpp ├── Eliminare.cpp ├── Examene.cpp ├── ExpectedPos.cpp ├── Expresie2.cpp ├── Expresie3.cpp ├── Felinare.cpp ├── Fenrir.cpp ├── G.cpp ├── G2mm.cpp ├── Game.cpp ├── Gardieni.cpp ├── Gossips.cpp ├── Grid.cpp ├── Grozavesti.cpp ├── Hacker3.cpp ├── Hanoi.cpp ├── Harta2.cpp ├── Hotel.cpp ├── Indep.cpp ├── Inversari.cpp ├── Invsort.cpp ├── Ismquery.cpp ├── Joc5.cpp ├── Karb2.cpp ├── Log.cpp ├── Magic Sequence.cpp ├── Map.cpp ├── Matrice2.cpp ├── Medie.cpp ├── Melodii.cpp ├── Mere.cpp ├── Mese.cpp ├── Mine.cpp ├── Mins.cpp ├── Multiplu.cpp ├── Narbi.cpp ├── Noname 3.cpp ├── Noname2.cpp ├── Numar3.cpp ├── Numere 7.cpp ├── Nums.cpp ├── PScPld.cpp ├── Pairs.cpp ├── Panda.cpp ├── Panouri.cpp ├── Parpal.cpp ├── Partial Match.cpp ├── Partitie.cpp ├── Pawns.cpp ├── Paznici.cpp ├── Per.cpp ├── Perioada2.cpp ├── Permlcs.cpp ├── Permutare2.cpp ├── PetSoft.cpp ├── Pikachu.cpp ├── Pirati.cpp ├── Planificare.cpp ├── Popandai2.cpp ├── Por Costel si Azerah.cpp ├── Por Costel si Invazia Extraterestra.cpp ├── Por Costel si Perechile.cpp ├── Por Costel si Pocnitoarea.cpp ├── Por Costel si Semipalindroamele.cpp ├── Posta2.cpp ├── Pp.cpp ├── Prefixe.cpp ├── Progresii2.cpp ├── Rangemode.cpp ├── Reguli.cpp ├── Retea.cpp ├── Rubarba.cpp ├── Secv8.cpp ├── Secvmax.cpp ├── Secvmin.cpp ├── Segmente2.cpp ├── Siruri.cpp ├── Sobo.cpp ├── Something.cpp ├── Spion.cpp ├── String.cpp ├── Subset2.cpp ├── Suma4.cpp ├── Suma5.cpp ├── Sume2.cpp ├── Super Mario.cpp ├── Tabara2.cpp ├── Tester.cpp ├── Tree.cpp ├── Tribute.cpp ├── Triunghi.cpp ├── Ubuntzei.cpp ├── Vase.cpp ├── Vila.cpp ├── Viteza.cpp ├── Viteza2.cpp ├── Vmin.cpp ├── X.cpp ├── Xcmmdc.cpp ├── Xor.cpp ├── Zip.cpp └── Zoo.cpp ├── Java ├── AncestorMatrix.java ├── BinaryIndexedTree.java ├── BreadthFirstSearch.java ├── CountingSort.java ├── DijkstraAlgorithm.java ├── DisjointSet.java ├── Graph.java ├── HeavyPathDecomposition.java ├── HungarianAlgorithm.java ├── InputReader.java ├── KMP.java ├── KruskalMST.java ├── LSD_RadixSort.java ├── MSD_RadixSort.java ├── MaximumFlow.java ├── MaximumMatching.java ├── MinCostMaxFlow.java ├── MultiHashTable.java ├── PrimMST.java ├── PushRelabel.java ├── RangeMinimumQuery.java ├── RunningMedian.java ├── SegmentTree.java ├── SparseTable.java ├── StableMarriageProblem.java ├── StronglyConnectedComponents.java ├── TarjanStronglyConnected.java ├── ThreeWayQuicksort.java ├── TopologicalSorting.java ├── TreeIsomorphism.java └── WeightedGraph.java ├── LICENSE ├── LSD_RadixSort.java ├── Mathematics-Algorithms ├── 2SAT solver.cpp ├── Determinant of a matrix.cpp ├── Extended Euclidean algorithm.cpp ├── Gaussian elimination.cpp ├── Inverse of a matrix.cpp ├── Lucas' theorem.cpp ├── Rank of a matrix.cpp └── Strassen algorithm.cpp ├── Mindcoding └── Palarb.cpp ├── Number-Theory ├── Binary GCD algorithm.cpp ├── Fast Fourier Transform (iterative implementation).cpp ├── Gaussian elimination (namespace).cpp ├── Linear sieve.cpp ├── Miller-Rabin primality test.cpp ├── ModInteger.cpp ├── Modular Exponentiation.cpp ├── Modular Inverse.cpp ├── Pollard's Rho algorithm (factorization).cpp ├── Sieve of Eratosthenes.cpp └── Sum and number of divisors.cpp ├── Python-Algorithms └── Sorting │ ├── Bubble Sort │ └── QuickSort ├── README.md ├── SPOJ ├── 263. Period.cpp ├── 1028. Hubulullu.cpp ├── 1419. A Game with Numbers.cpp ├── 14932. Lowest Common Ancestor.cpp ├── 1684. Frequent values.cpp ├── 21347. Tulip And Numbers.cpp ├── 235. Very Fast Multiplication.cpp ├── 31. Fast Multiplication.cpp ├── 32. A Needle in the Haystack.cpp ├── 3267. D-query.cpp ├── 3273. Order statistic set.cpp ├── 3866. Finding Palindromes.cpp ├── 3885. Coins Game.cpp ├── 3969. M&M Game.cpp ├── 439. Not So Fast Multiplication.cpp ├── 4474. Longest Palindromic Substring.cpp ├── 5676. STONE GAME.cpp ├── 5980. Matrix Game.cpp ├── 6043. The Game.cpp ├── 694. Distinct Substrings.cpp ├── 705. New Distinct Substrings.cpp ├── 7155. Test.cpp ├── 7212. Find String Roots.cpp ├── 7423. File Recover Testing.cpp ├── 8217. XOR Maximization.cpp ├── 9055. Most Frequent Value.cpp ├── Fishmonger.cpp └── Taxi.cpp ├── Sorting-and-Order-Statistics ├── 3-way quicksort (strings sorting).cpp ├── Bogosort.cpp ├── Bubble sort.cpp ├── Cocktail sort.cpp ├── Comb sort.cpp ├── Counting sort.cpp ├── Heapsort.cpp ├── Insertion sort.cpp ├── Merge sort.cpp ├── Order Statistics.cpp ├── Quicksort.cpp ├── Radix sort (LSD).cpp ├── Selection sort.cpp ├── Shell sort (James A. Sellers' sequence).cpp ├── Shell sort (M.Ciura's gap sequence).cpp └── Stooge sort.cpp ├── String-Algorithms ├── 3-way quicksort (strings sorting).cpp ├── Aho-Corasick dynamic implementation.cpp ├── Aho-Corasick static implementation.cpp ├── All periods of a string.cpp ├── Expression Evaluation.cpp ├── KMP Algorithm.cpp ├── Manacher's Algorithm.cpp ├── Palindromic tree.cpp ├── Rabin-Karp Algorithm.cpp ├── Rolling hash.cpp ├── Shortest period of a string.cpp ├── String Matching - Substrings of variable length.cpp ├── Suffix Array + LCP Array O(NlogN) (faster implementation).cpp ├── Suffix Array + LCP Array O(Nlog^2N + N).cpp ├── Suffix Array O(N^2).cpp ├── Suffix Array O(NlogN) (Prefix Doubling).cpp ├── Suffix Array O(Nlog^2N) (using Prefix Doubling).cpp ├── Suffix Array O(Nlog^2N).cpp ├── Trie.cpp └── Z Algorithm.cpp ├── Template ├── Timus ├── 1102. Strange Dialog.cpp ├── 1195. Ouths and Crosses.cpp ├── 1297. Palindrome.cpp ├── 1465. Pawn Game.cpp ├── 1487. Chinese Football.cpp ├── 1540. Battle for the Ring.cpp ├── 1664. Pipeline Transportation.cpp ├── 1716. Alternative Solution.cpp ├── 1791. Uncle Styopa and Buses.cpp ├── 1960. Palindromes and Super Abilities.cpp └── 2040. Palindromes and Super Abilities 2.cpp ├── TopCoder ├── SRM-310-DIV2 │ ├── FloatingMedian │ ├── InputBoxChecker │ └── MeasuringTemperature ├── SRM-612-DIV-2 │ ├── EmoticonsDiv2 │ ├── LeftAndRightHandedDiv2 │ └── PowersOfTwo ├── SRM-621-DIV-2 │ ├── MixingColors │ ├── NumbersChallenge │ └── TwoWaysSorting └── SRM-626-DIV-2 │ ├── FixedDiceGameDiv2 │ └── NegativeGraphDiv2 ├── Tree-Algorithms ├── Centroid decomposition.cpp ├── Heavy Path Decomposition.cpp ├── Lowest Common Ancestor in O(sqrtN).cpp ├── Lowest Common Ancestor with Ancestors' Matrix.cpp ├── Lowest Common Ancestor with HPD.cpp ├── Minimum spanning tree - Kruskal's algorithm.cpp ├── Minimum spanning tree - Prim's algorithm.cpp └── Tree's diameter.cpp ├── USACO └── February-2015 │ ├── Cow Hopscotch (Silver) │ └── Superbull ├── UVA ├── 10298 - Power Strings.cpp ├── 10679 - I Love Strings!!.cpp ├── 11888 - Abnormal 89's.cpp └── 12604 - Caesar Cipher.cpp ├── Unordered-Map killer ├── Varena ├── 2i3j5k.cpp ├── 5div.cpp ├── Abwords.cpp ├── Alpine.cpp ├── Antitir.cpp ├── Ants.cpp ├── Bart.cpp ├── Beculete.cpp ├── Bizar.cpp ├── Canibali.cpp ├── Coborâre.cpp ├── Cooler.cpp ├── Costume.cpp ├── Cotoroanța.cpp ├── Diametru.cpp ├── Evacuare.cpp ├── Fazan.cpp ├── Fotografie.cpp ├── GPS.cpp ├── Habarnam.cpp ├── Hex.cpp ├── Hot Dogs.cpp ├── La coadă.cpp ├── Latin.cpp ├── Livada.cpp ├── Magazin2.cpp ├── Matroid.cpp ├── Maxxor2.cpp ├── Mincut.cpp ├── Parc.cpp ├── Pasiență.cpp ├── Paznici.cpp ├── Pointeri.cpp ├── Points2.cpp ├── Poligon.cpp ├── Push-Relabel.cpp ├── Păsări.cpp ├── Re-search.cpp ├── Rocker.cpp ├── Romb2.cpp ├── SCV.cpp ├── Sir3.cpp ├── Sqrt.cpp ├── Subsecvențe 2.cpp ├── Subunic.cpp ├── Tensor.cpp ├── Traseu.cpp ├── Zugrăveală.cpp └── Înfășurătoare convexă.cpp └── script.sh /ACM_OOP_classes/Graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Graph 6 | { 7 | private: 8 | 9 | class Edge 10 | { 11 | public: 12 | 13 | int node; 14 | size_t next; 15 | 16 | Edge(int _node, size_t _next) : node(_node), next(_next){ 17 | } 18 | }; 19 | 20 | vector graph; 21 | vector head; 22 | int N; 23 | 24 | public: 25 | 26 | static const int NIL; 27 | 28 | Graph(int _N) : graph(), head(), N(_N){ 29 | head.resize(N, NIL); 30 | } 31 | 32 | void addEdge(int x, int y) 33 | { 34 | assert(1 <= x && x <= N); 35 | x--; 36 | 37 | graph.push_back(Edge(y, head[x])); 38 | head[x] = graph.size() - 1; 39 | } 40 | 41 | void addDoubleEdge(int x, int y) 42 | { 43 | addEdge(x, y); 44 | addEdge(y, x); 45 | } 46 | 47 | int getHead(int node) const 48 | { 49 | assert(1 <= node && node <= N); 50 | return head[node - 1]; 51 | } 52 | 53 | int getNeighbour(size_t p) const 54 | { 55 | assert(p < graph.size()); 56 | return graph[p].node; 57 | } 58 | 59 | int getNext(size_t p) const 60 | { 61 | assert(p < graph.size()); 62 | return graph[p].next; 63 | } 64 | }; 65 | 66 | const int Graph::NIL = -1; 67 | 68 | int main() 69 | { 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Ad-hoc-algorithms/Floyd’s Cycle-Finding Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int f(int x) 6 | { 7 | } 8 | 9 | pair floyd_cycle_detection(const int x0) 10 | { 11 | int slow = f(x0); 12 | int fast = f(f(x0)); 13 | 14 | while (slow != fast) 15 | { 16 | slow = f(slow); 17 | fast = f(f(fast)); 18 | } 19 | 20 | int start = 0; 21 | fast = x0; 22 | 23 | while (slow != fast) 24 | { 25 | slow = f(slow); 26 | fast = f(fast); 27 | } 28 | 29 | int length = 1; 30 | fast = f(slow); 31 | 32 | while (slow != fast) 33 | { 34 | fast = f(fast); 35 | length++; 36 | } 37 | 38 | return {start, length}; 39 | } 40 | 41 | int main() 42 | { 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Ad-hoc-algorithms/Timer (simple implementation).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Timer 6 | { 7 | public: 8 | 9 | double start; 10 | 11 | Timer() : start(clock()) { 12 | } 13 | 14 | void reset() 15 | { 16 | start = clock(); 17 | } 18 | 19 | double getTime() const 20 | { 21 | return (clock() - start) / CLOCKS_PER_SEC; 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Ad-hoc-algorithms/Timer C++11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | using namespace std::chrono; 5 | 6 | class Timer 7 | { 8 | public: 9 | 10 | high_resolution_clock::time_point start; 11 | 12 | Timer() : start(high_resolution_clock::now()) { 13 | } 14 | 15 | void reset() 16 | { 17 | start = high_resolution_clock::now(); 18 | } 19 | 20 | double getTime() const 21 | { 22 | high_resolution_clock::time_point finish = high_resolution_clock::now(); 23 | duration time_span = duration_cast>(finish - start); 24 | 25 | return time_span.count(); 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Codechef/Candidate walk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int LIMIT = ( 1 << 16 ); 6 | 7 | long long dp[LIMIT]; 8 | 9 | int N, D; 10 | 11 | int codifica(vector& stare) 12 | { 13 | int sol = 0; 14 | 15 | for ( int i = 0; i < stare.size(); ++i ) 16 | sol = sol * D + stare[i]; 17 | 18 | return sol; 19 | } 20 | 21 | long long getCost(vector& stare) 22 | { 23 | long long sum = 0; 24 | long long xorsum = 0; 25 | 26 | for ( int x: stare ) 27 | { 28 | sum += x; 29 | xorsum ^= x; 30 | } 31 | 32 | return sum * xorsum; 33 | } 34 | 35 | long long memo(vector& v) 36 | { 37 | int id = codifica(v); 38 | 39 | if ( dp[id] != -1 ) 40 | return dp[id]; 41 | 42 | if ( id == 0 ) 43 | return 0; 44 | 45 | long long cost = ( 1LL << 60 ); 46 | 47 | for ( int i = 0; i < v.size(); ++i ) 48 | { 49 | if ( v[i] > 0 ) 50 | { 51 | vector g = v; 52 | g[i]--; 53 | 54 | cost = min( cost, memo(g) ); 55 | } 56 | } 57 | 58 | return dp[id] = cost + getCost(v); 59 | } 60 | 61 | int main() 62 | { 63 | ///ifstream cin("data.in"); 64 | 65 | int T; 66 | 67 | cin >> T; 68 | 69 | while ( T-- ) 70 | { 71 | cin >> N >> D; 72 | 73 | vector v; 74 | 75 | for ( int i = 0; i < N; ++i ) 76 | v.push_back( D - 1 ); 77 | 78 | memset( dp, -1, sizeof( dp ) ); 79 | 80 | cout << memo(v) << "\n"; 81 | } 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Codechef/Chef Anup.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 2000; 6 | 7 | int sol[Nmax]; 8 | 9 | int T; 10 | unsigned long long N, K; 11 | unsigned long long L; 12 | 13 | unsigned long long cateMaiMici(unsigned long long i, unsigned long long k) 14 | { 15 | if (k - 1 == 0) 16 | return 0; 17 | 18 | unsigned long long cate = (k - 1); 19 | 20 | for (int j = 1; j <= N - i; ++j) 21 | { 22 | if (cate > L / K) 23 | return (L + 1); 24 | 25 | cate *= K; 26 | } 27 | 28 | return cate; 29 | } 30 | 31 | int main() 32 | { 33 | ///ifstream cin("data.in"); 34 | 35 | ios_base::sync_with_stdio(false); 36 | 37 | cin >> T; 38 | 39 | while (T--) 40 | { 41 | cin >> N >> K >> L; 42 | L--; 43 | 44 | memset(sol, 0, sizeof(sol)); 45 | 46 | for (int i = 1; i <= N; ++i) 47 | { 48 | for (int j = K; j >= 1; j--) 49 | { 50 | if (cateMaiMici(i, j) <= L) 51 | { 52 | sol[i] = j; 53 | L -= cateMaiMici(i, j); 54 | break; 55 | } 56 | } 57 | } 58 | 59 | for (int i = 1; i <= N; ++i) 60 | cout << sol[i] << " "; 61 | 62 | cout << "\n"; 63 | } 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Codechef/Coprime Triples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int Nmax = 1e5 + 2; 8 | const int Vmax = 1e6 + 2; 9 | 10 | int D[Vmax]; 11 | bool square_free[Vmax]; 12 | int sieve[Vmax]; 13 | int N; 14 | 15 | int Mobius( int x ) 16 | { 17 | if ( square_free[x] ) 18 | return 0; 19 | 20 | if ( sieve[x] % 2 == 0) 21 | return 1; 22 | else 23 | return -1; 24 | } 25 | 26 | long long C( long long n, long long k ) 27 | { 28 | if ( n < k ) 29 | return 0; 30 | 31 | return n * ( n - 1 ) * ( n - 2 ) / 6LL; 32 | } 33 | 34 | int main() 35 | { 36 | cin.sync_with_stdio( false ); 37 | 38 | cin >> N; 39 | 40 | for ( int i = 1, A; i <= N; ++i ) 41 | { 42 | cin >> A; 43 | 44 | for ( int j = 1; j * j <= A; ++j ) 45 | { 46 | if ( A % j == 0 ) 47 | { 48 | D[j]++; 49 | 50 | if ( A / j != j ) 51 | D[A / j]++; 52 | } 53 | } 54 | } 55 | 56 | for ( int i = 2; i * i < Vmax; ++i ) 57 | { 58 | for ( int j = 1; j * i * i < Vmax; ++j ) 59 | square_free[j * i * i] = 1; 60 | } 61 | 62 | for ( int i = 2; i < Vmax; ++i ) 63 | if ( sieve[i] == 0 ) 64 | for ( int j = i; j < Vmax; j += i ) 65 | sieve[j]++; 66 | 67 | long long sol = 0; 68 | 69 | for ( int i = 1; i < Vmax; ++i ) 70 | sol += 1LL * Mobius( i ) * C( D[i], 3 ); 71 | 72 | cout << sol; 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Codechef/Dynamic Inversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int Nmax = 1e5 + 2; 10 | 11 | int N, Q; 12 | 13 | int a[Nmax]; 14 | int b[Nmax]; 15 | int answer; 16 | 17 | void MergeSort( int l, int r ) 18 | { 19 | int m = (l + r)/2, k, i, j; 20 | 21 | if ( l == r ) 22 | return; 23 | 24 | MergeSort( l, m ); 25 | MergeSort( m + 1, r ); 26 | 27 | for( i = l, j = m + 1, k = l; i <= m || j <= r; ) 28 | if( j > r || ( i <= m && a[i] <= a[j] ) ) 29 | b[ k++ ] = a[ i++ ]; 30 | else 31 | { 32 | b[ k++ ] = a[ j++ ]; 33 | 34 | answer = ( answer + m - i + 1 ) % 2; 35 | } 36 | 37 | for( k = l; k <= r; k++ ) 38 | a[k] = b[k]; 39 | } 40 | 41 | 42 | int main() 43 | { 44 | cin.sync_with_stdio( false ); 45 | 46 | cin >> N >> Q; 47 | 48 | for ( int i = 1; i <= N; ++i ) 49 | cin >> a[i]; 50 | 51 | MergeSort( 1, N ); 52 | 53 | while ( Q-- ) 54 | { 55 | int X, Y; 56 | 57 | cin >> X >> Y; 58 | cout << ( answer ^= 1 ) << "\n"; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Codechef/Equality.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 50000 + 1; 6 | long long A[Nmax]; 7 | int N, T; 8 | 9 | int main() 10 | { 11 | ///ifstream cin("data.in"); 12 | 13 | ios_base::sync_with_stdio(false); 14 | 15 | cin >> T; 16 | 17 | while (T--) 18 | { 19 | cin >> N; 20 | long long sum = 0; 21 | 22 | for (int i = 1; i <= N; ++i) 23 | { 24 | cin >> A[i]; 25 | sum += (long long)A[i]; 26 | } 27 | 28 | sum /= (N - 1); 29 | 30 | for (int i = 1; i <= N; ++i) 31 | cout << sum - (long long)A[i] << " "; 32 | 33 | cout << "\n"; 34 | } 35 | 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Codechef/Piece of cake.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ///ifstream cin("data.in"); 8 | 9 | ios_base::sync_with_stdio( false ); 10 | 11 | int T; 12 | 13 | cin >> T; 14 | 15 | while ( T-- ) 16 | { 17 | string a; 18 | 19 | cin >> a; 20 | 21 | vector v( 100, 0 ); 22 | 23 | for ( char x: a ) 24 | v[x - 'a']++; 25 | 26 | int valid = 0; 27 | int sum = a.size(); 28 | 29 | for ( int i = 0; i < 30; ++i ) 30 | { 31 | if ( v[i] != 0 ) 32 | { 33 | if ( v[i] == sum - v[i] ) 34 | valid = 1; 35 | } 36 | } 37 | 38 | if ( valid ) 39 | cout << "YES\n"; 40 | else 41 | cout << "NO\n"; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Codechef/Rich Rhyme.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int Lmax = 4e6 + 2; 7 | 8 | char S[Lmax]; 9 | int pi[Lmax]; 10 | int N; 11 | 12 | void build_prefix() 13 | { 14 | pi[1] = 0; 15 | 16 | int lgprefix = 0; 17 | 18 | for ( int i = 2; i <= N; ++i ) 19 | { 20 | while ( lgprefix && S[i] != S[lgprefix + 1] ) 21 | lgprefix = pi[lgprefix]; 22 | 23 | if ( S[i] == S[lgprefix + 1] ) 24 | lgprefix++; 25 | 26 | pi[i] = lgprefix; 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | while ( cin >> ( S + 1 ) ) 33 | { 34 | N = strlen( S + 1 ); 35 | 36 | if ( N == 3 && S[1] == 'E' ) break; 37 | 38 | build_prefix(); 39 | 40 | for ( int i = 1; i <= N; ++i ) 41 | cout << pi[i] << " "; 42 | 43 | cout << "\n"; 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Codechef/Shortest Path in Binary Trees.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int N; 6 | 7 | inline void fastRead( int &a ) 8 | { 9 | register char ch; 10 | a = 0; 11 | 12 | do 13 | { 14 | ch = getchar_unlocked(); 15 | 16 | } while ( !isdigit( ch ) ); 17 | 18 | do 19 | { 20 | a = a * 10 + ch - '0'; 21 | ch = getchar_unlocked(); 22 | 23 | } while( isdigit( ch ) ); 24 | } 25 | 26 | inline void fastWrite( int a ) 27 | { 28 | char s[20]; 29 | int i = 0; 30 | 31 | do 32 | { 33 | s[ i++ ] = a % 10 + '0'; 34 | a /= 10; 35 | 36 | } while ( a ); 37 | 38 | i--; 39 | 40 | while ( i >= 0 ) putchar_unlocked( s[ i-- ] ); 41 | 42 | putchar_unlocked( '\n' ); 43 | } 44 | 45 | int main() 46 | { 47 | fastRead( N ); 48 | 49 | while ( N-- ) 50 | { 51 | int x, y, sol = 0; 52 | 53 | fastRead( x ); fastRead( y ); 54 | 55 | while ( x != y ) 56 | { 57 | if ( x > y ) 58 | x >>= 1; 59 | else 60 | y >>= 1; 61 | 62 | sol++; 63 | } 64 | 65 | fastWrite( sol ); 66 | } 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Codechef/Taxi Driver.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 200000; 6 | 7 | int X[Nmax], Y[Nmax]; 8 | int T, N, A, B; 9 | 10 | long long solve(int v[]) 11 | { 12 | sort(v, v + N); 13 | 14 | long long sol = 0; 15 | long long sum_prev = 0; 16 | 17 | for (int i = 0; i < N; ++i) 18 | { 19 | sol += 1LL * v[i] * i - sum_prev; 20 | sum_prev += (long long)v[i]; 21 | } 22 | 23 | return sol; 24 | } 25 | 26 | int main() 27 | { 28 | ///freopen("data.in", "r", stdin); 29 | 30 | scanf("%d ", &T); 31 | 32 | while (T--) 33 | { 34 | scanf("%d %d %d ", &N, &A, &B); 35 | 36 | for (int i = 0; i < N; ++i) 37 | { 38 | scanf("%d ", &X[i]); 39 | scanf("%d ", &Y[i]); 40 | 41 | int new_x = A * X[i] + B * Y[i]; 42 | int new_y = A * X[i] - B * Y[i]; 43 | 44 | X[i] = new_x; 45 | Y[i] = new_y; 46 | } 47 | 48 | printf("%lld\n", (solve(X) + solve(Y)) / 2); 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Codechef/VCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100; 6 | 7 | int A[Nmax], B[Nmax]; 8 | int T, N, M, K; 9 | 10 | int main() 11 | { 12 | // ifstream cin("data.in"); 13 | 14 | ios_base::sync_with_stdio(false); 15 | 16 | cin >> T; 17 | 18 | while (T--) 19 | { 20 | cin >> N >> M >> K; 21 | 22 | for (int i = 0; i < M; ++i) 23 | cin >> A[i]; 24 | 25 | for (int i = 0; i < K; ++i) 26 | cin >> B[i]; 27 | 28 | sort(A, A + M); 29 | sort(B, B + K); 30 | 31 | int s1 = 0, s2 = 0; 32 | 33 | for (int i = 0; i < M; ++i) 34 | s1 += binary_search(B, B + K, A[i]); 35 | 36 | for (int i = 1; i <= N; ++i) 37 | { 38 | int a = binary_search(A, A + M, i); 39 | int b = binary_search(B, B + K, i); 40 | 41 | if (!a && !b) 42 | s2++; 43 | } 44 | 45 | cout << s1 << " " << s2 << "\n"; 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Codechef/XOR with Subset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1024; 6 | 7 | int dp[Nmax][Nmax]; 8 | int T, N, K, A; 9 | 10 | int main() 11 | { 12 | ios_base::sync_with_stdio( false ); 13 | 14 | cin >> T; 15 | 16 | while ( T-- ) 17 | { 18 | memset( dp, 0, sizeof( dp ) ); 19 | 20 | dp[0][0] = 1; 21 | 22 | cin >> N >> K; 23 | 24 | for ( int i = 1; i <= N; ++i ) 25 | { 26 | cin >> A; 27 | 28 | for ( int j = 0; j < Nmax; ++j ) 29 | dp[i][j] = dp[i - 1][j] | dp[i - 1][j ^ A]; 30 | } 31 | 32 | int maxim = 0; 33 | 34 | for ( int i = 0; i < Nmax; ++i ) 35 | maxim = max( maxim, ( K ^ i ) * dp[N][i] ); 36 | 37 | cout << maxim << "\n"; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Codeforces/A. Alice and Bob.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int N, maxim, a, x; 6 | 7 | int main() 8 | { 9 | cin >> N; 10 | 11 | for ( int k = 1; k <= N; ++k ) 12 | { 13 | cin >> a; 14 | x = __gcd( x, a ); 15 | maxim = max( maxim, a ); 16 | } 17 | 18 | int nrMoves = maxim / x - N; 19 | 20 | if ( nrMoves % 2 ) 21 | cout << "Alice"; 22 | else 23 | cout << "Bob"; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Codeforces/A. LLPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string s; 6 | int vis[300]; 7 | 8 | int main() 9 | { 10 | cin >> s; 11 | 12 | for ( auto x: s ) 13 | vis[x]++; 14 | 15 | for ( int i = 250; i >= 0; i-- ) 16 | { 17 | if ( vis[i] ) 18 | { 19 | while ( vis[i] ) 20 | { 21 | cout << char( i ); 22 | vis[i]--; 23 | } 24 | 25 | break; 26 | } 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Codeforces/A. Maximum in Table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int dp[11][11]; 6 | int n; 7 | 8 | int main() 9 | { 10 | cin >> n; 11 | 12 | for ( int i = 1; i <= n; ++i ) 13 | dp[i][1] = dp[1][i] = 1; 14 | 15 | for ( int i = 2; i <= n; ++i ) 16 | for ( int j = 2; j <= n; ++j ) 17 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 18 | 19 | cout << dp[n][n]; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Codeforces/B. Mike and Feet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MAX_N = 2e5 + 1; 6 | 7 | int st[MAX_N], dr[MAX_N]; 8 | int len[MAX_N]; 9 | int A[MAX_N]; 10 | 11 | int stiva[MAX_N], top; 12 | 13 | int N; 14 | 15 | int main() 16 | { 17 | ///ifstream cin("data.in"); 18 | 19 | ios_base::sync_with_stdio(false); 20 | cin.tie(nullptr); 21 | 22 | cin >> N; 23 | 24 | for (int i = 1; i <= N; ++i) 25 | cin >> A[i]; 26 | 27 | for (int i = 1; i <= N; ++i) 28 | { 29 | while (top >= 1 && A[ stiva[top] ] >= A[i]) 30 | top--; 31 | 32 | if (top == 0) 33 | st[i] = 1; 34 | else 35 | st[i] = stiva[top] + 1; 36 | 37 | stiva[ ++top ] = i; 38 | } 39 | 40 | top = 0; 41 | 42 | for (int i = N; i >= 1; i--) 43 | { 44 | while (top >= 1 && A[ stiva[top] ] >= A[i]) 45 | top--; 46 | 47 | if (top == 0) 48 | dr[i] = N; 49 | else 50 | dr[i] = stiva[top] - 1; 51 | 52 | stiva[ ++top ] = i; 53 | } 54 | 55 | for (int i = 1; i <= N; ++i) 56 | { 57 | int l = dr[i] - st[i] + 1; 58 | 59 | len[l] = max(len[l], A[i]); 60 | } 61 | 62 | for (int i = N; i >= 1; --i) 63 | len[i] = max(len[i], len[i + 1]); 64 | 65 | for (int i = 1; i <= N; ++i) 66 | cout << len[i] << " "; 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Codeforces/B. New Year Permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 300 + 2; 6 | 7 | char A[Nmax]; 8 | int D[Nmax][Nmax]; 9 | int P[Nmax]; 10 | int N; 11 | 12 | int main() 13 | { 14 | ios_base::sync_with_stdio( false ); 15 | 16 | cin >> N; 17 | 18 | for ( int i = 1; i <= N; ++i ) 19 | cin >> P[i]; 20 | 21 | for ( int i = 1; i <= N; ++i ) 22 | { 23 | cin >> ( A + 1 ); 24 | 25 | for ( int j = 1; j <= N; ++j ) 26 | D[i][j] = A[j] - '0'; 27 | } 28 | 29 | for ( int k = 1; k <= N; ++k ) 30 | for ( int i = 1; i <= N; ++i ) 31 | for ( int j = 1; j <= N; ++j ) 32 | D[i][j] |= ( D[i][k] & D[k][j] ); 33 | 34 | for ( int k = 1; k <= N; ++k ) 35 | for ( int i = 1; i <= N; ++i ) 36 | for ( int j = i + 1; j <= N; ++j ) 37 | if ( D[i][j] && P[i] > P[j] ) 38 | swap( P[i], P[j] ); 39 | 40 | for ( int i = 1; i <= N; ++i ) 41 | cout << P[i] << " "; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Codeforces/B. Painting Pebbles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100 + 1; 6 | 7 | int A[Nmax][Nmax]; 8 | int N, K; 9 | 10 | int verif(int culoare) 11 | { 12 | int m = +1e9; 13 | int M = -1e9; 14 | 15 | for ( int i = 1; i <= N; ++i ) 16 | { 17 | int c = 0; 18 | 19 | for ( int j = 1; j <= A[i][0]; ++j ) 20 | c += (A[i][j] == culoare); 21 | 22 | m = min(m, c); 23 | M = max(M, c); 24 | } 25 | 26 | return abs(M - m) <= 1; 27 | } 28 | 29 | int main() 30 | { 31 | cin >> N >> K; 32 | 33 | for ( int i = 1; i <= N; ++i ) 34 | cin >> A[i][0]; 35 | 36 | for ( int i = 1; i <= N; ++i ) 37 | { 38 | int culoare = 1; 39 | 40 | for ( int j = 1; j <= A[i][0]; ++j ) 41 | { 42 | A[i][j] = culoare; 43 | 44 | if ( ++culoare > K ) culoare = 1; 45 | } 46 | } 47 | 48 | for ( int i = 1; i <= K; ++i ) 49 | if ( verif(i) == 0 ) 50 | { 51 | cout << "NO\n"; 52 | return 0; 53 | } 54 | 55 | cout << "YES\n"; 56 | 57 | for ( int i = 1; i <= N; ++i ) 58 | { 59 | for ( int j = 1; j <= A[i][0]; ++j ) 60 | cout << A[i][j] << " "; 61 | 62 | cout << "\n"; 63 | } 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Codeforces/B. Password.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int Lmax = 1e6 + 2; 8 | const int BASE = 137; 9 | 10 | char S[Lmax]; 11 | int H[Lmax], Hpow[Lmax]; 12 | int N; 13 | 14 | void pregen() 15 | { 16 | Hpow[0] = 1; 17 | 18 | for ( int i = 1; i <= N; ++i ) 19 | Hpow[i] = Hpow[i - 1] * BASE; 20 | 21 | for ( int i = 1; i <= N; ++i ) 22 | H[i] = H[i - 1] * BASE + S[i]; 23 | } 24 | 25 | int getHash( int i, int j ) 26 | { 27 | return H[j] - H[i - 1] * Hpow[j - i + 1]; 28 | } 29 | 30 | int bsearch( int pos ) 31 | { 32 | int l = 1, r = N - pos, sol = 0; 33 | 34 | while ( l <= r ) 35 | { 36 | int m = ( l + r ) / 2; 37 | 38 | if ( getHash( 1, m ) == getHash( pos, pos + m - 1 ) ) 39 | { 40 | sol = m; 41 | l = m + 1; 42 | } 43 | else 44 | { 45 | r = m - 1; 46 | } 47 | } 48 | 49 | return sol; 50 | } 51 | 52 | int main() 53 | { 54 | ///ifstream cin("data.in"); 55 | 56 | cin >> ( S + 1 ); 57 | 58 | N = strlen( S + 1 ); 59 | 60 | pregen(); 61 | 62 | int dim = 0; 63 | 64 | for ( int i = 2; i <= N; ++i ) 65 | dim = max( dim, bsearch( i ) ); 66 | 67 | for ( int i = dim; i >= 1; --i ) 68 | { 69 | if ( getHash( 1, i ) == getHash( N - i + 1, N ) ) 70 | { 71 | for ( int j = 1; j <= i; ++j ) 72 | cout << S[j]; 73 | 74 | return 0; 75 | } 76 | } 77 | 78 | cout << "Just a legend\n"; 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Codeforces/C. Memory for Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | inline int readNumber() 6 | { 7 | register int a = 0; 8 | char ch; 9 | 10 | do 11 | { 12 | ch = getchar(); 13 | 14 | } while ( !isdigit(ch) ); 15 | 16 | do 17 | { 18 | a = a * 10 + ch - '0'; 19 | ch = getchar(); 20 | 21 | } while ( isdigit(ch) ); 22 | 23 | return a; 24 | } 25 | 26 | const int Nmax = 1e6 + 1; 27 | 28 | int a[31], b[Nmax]; 29 | int N, M; 30 | 31 | int main() 32 | { 33 | ///freopen("data.in", "r", stdin); 34 | 35 | N = readNumber(); M = readNumber(); 36 | 37 | for ( int i = 1; i <= N; ++i ) 38 | { 39 | int val = readNumber(); 40 | 41 | for ( int j = 0; j < 30; ++j ) 42 | if ( val & ( 1 << j ) ) 43 | a[j]++; 44 | } 45 | 46 | for ( int i = 1; i <= M; ++i ) 47 | b[i] = readNumber(); 48 | 49 | sort( b + 1, b + M + 1 ); 50 | 51 | int sol = 0; 52 | 53 | for ( int i = 1; i <= M; ++i ) 54 | { 55 | int j = b[i]; 56 | 57 | while ( j < 30 && !a[j] ) ++j; 58 | 59 | if ( j < 30 ) 60 | { 61 | if ( j == b[i] ) 62 | a[ b[i] ]--; 63 | else 64 | { 65 | for ( int k = b[i]; k < j; ++k ) 66 | a[k]++; 67 | 68 | a[j]--; 69 | } 70 | 71 | sol++; 72 | } 73 | } 74 | 75 | cout << sol << "\n"; 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Codeforces/D. Good Substrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | typedef unsigned long long ull; 10 | 11 | const ull BASE = 666013; 12 | const int Lmax = 15e2 + 2; 13 | const int SIGMA = 26 + 2; 14 | 15 | unordered_set Set; 16 | 17 | int good[SIGMA]; 18 | int sum_bad[Lmax]; 19 | char alpha[SIGMA]; 20 | 21 | char S[Lmax]; 22 | int N, nr_bad; 23 | 24 | ull H[Lmax], Hpow[Lmax]; 25 | 26 | void pregen() 27 | { 28 | Hpow[0] = 1; 29 | 30 | for ( int i = 1; i <= N; ++i ) 31 | Hpow[i] = Hpow[i - 1] * BASE; 32 | 33 | for ( int i = 1; i <= N; ++i ) 34 | H[i] = H[i - 1] * BASE + S[i]; 35 | } 36 | 37 | ull getHash( int i, int j ) 38 | { 39 | return H[j] - H[i - 1] * Hpow[j - i + 1]; 40 | } 41 | 42 | int main() 43 | { 44 | ///ifstream cin("data.in"); 45 | 46 | cin.sync_with_stdio( false ); 47 | 48 | cin >> ( S + 1 ); 49 | N = strlen( S + 1 ); 50 | 51 | cin >> alpha; 52 | 53 | cin >> nr_bad; 54 | 55 | pregen(); 56 | 57 | for ( int i = 0; i < SIGMA - 2; ++i ) 58 | good[i] = alpha[i] - '0'; 59 | 60 | for ( int i = 1; i <= N; ++i ) 61 | sum_bad[i] = sum_bad[i - 1] + ( good[ S[i] - 'a' ] == 0 ); 62 | 63 | for ( int i = 1; i <= N; ++i ) 64 | { 65 | for ( int j = i; j <= N; ++j ) 66 | { 67 | if ( sum_bad[j] - sum_bad[i - 1] <= nr_bad ) 68 | Set.insert( getHash( i, j ) ); 69 | } 70 | } 71 | 72 | cout << Set.size() << "\n"; 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Codeforces/D. Maximum Xor Secondary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1e5 + 1; 6 | 7 | int stiva[Nmax], top; 8 | int maxim; 9 | int N; 10 | 11 | int main() 12 | { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(false); 15 | 16 | cin >> N; 17 | 18 | for (int i = 0, v; i < N; ++i) 19 | { 20 | cin >> v; 21 | 22 | while (top > 0) 23 | { 24 | maxim = max(maxim, stiva[top] ^ v); 25 | 26 | if (stiva[top] > v) 27 | break; 28 | else 29 | top--; 30 | } 31 | 32 | stiva[ ++top ] = v; 33 | } 34 | 35 | cout << maxim << "\n"; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Codeforces/D. Om Nom and Necklace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector Z_Algorithm(const string &str) 6 | { 7 | const int N = str.size(); 8 | vector Z(N, 0); 9 | 10 | int L = 0, R = 0; 11 | 12 | for (int i = 1; i < N; ++i) 13 | { 14 | if (i <= R) 15 | Z[i] = min(R - i + 1, Z[i - L]); 16 | 17 | while (i + Z[i] < N && str[ Z[i] ] == str[ i + Z[i] ]) 18 | Z[i]++; 19 | 20 | if (i + Z[i] >= R) 21 | { 22 | L = i; 23 | R = i + Z[i] - 1; 24 | } 25 | } 26 | 27 | return Z; 28 | } 29 | 30 | int main() 31 | { 32 | ///ifstream cin("data.in"); 33 | 34 | int N, K; 35 | string str; 36 | 37 | cin >> N >> K >> str; 38 | 39 | vector z = Z_Algorithm(str); 40 | vector cnt(N, 0); 41 | 42 | for (int prefLen = 1; K * prefLen <= N; ++prefLen) 43 | { 44 | int nr = z[prefLen] / prefLen + 1; 45 | 46 | if (nr < K) 47 | continue; 48 | 49 | int lenA = min(prefLen, z[K * prefLen]); 50 | 51 | cnt[K * prefLen - 1]++; 52 | 53 | if (K * prefLen + lenA < N) 54 | cnt[K * prefLen + lenA]--; 55 | } 56 | 57 | for (int i = 0; i < N; ++i) 58 | { 59 | if (i) 60 | cnt[i] += cnt[i - 1]; 61 | 62 | if (cnt[i]) 63 | cout << "1"; 64 | else 65 | cout << "0"; 66 | } 67 | 68 | cout << "\n"; 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Codeforces/D. Palindrome Degree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef unsigned long long ull; 8 | const ull BASE = 666013; 9 | 10 | const int Lmax = 5e6 + 2; 11 | 12 | int pal[Lmax]; 13 | char S[Lmax]; 14 | int N, solution; 15 | 16 | ull H1[Lmax], H2[Lmax], Hpow[Lmax]; 17 | 18 | void pregen() 19 | { 20 | Hpow[0] = 1; 21 | 22 | for ( int i = 1; i <= N; ++i ) 23 | Hpow[i] = Hpow[i - 1] * BASE; 24 | 25 | for ( int i = 1; i <= N; ++i ) 26 | { 27 | H1[i] = H1[i - 1] * BASE + S[i]; 28 | H2[i] = H2[i - 1] * BASE + S[N - i + 1]; 29 | } 30 | } 31 | 32 | ull getHash( ull H[], int i, int j ) 33 | { 34 | return H[j] - H[i - 1] * Hpow[j - i + 1]; 35 | } 36 | 37 | int main() 38 | { 39 | ///ifstream cin("data.in"); 40 | 41 | cin.sync_with_stdio( false ); 42 | 43 | cin >> ( S + 1 ); 44 | N = strlen( S + 1 ); 45 | 46 | pregen(); 47 | 48 | pal[1] = 1; 49 | 50 | for ( int i = 2; i <= N; ++i ) 51 | { 52 | if ( getHash( H1, 1, i ) == getHash( H2, N - i + 1, N ) ) 53 | { 54 | pal[i] = pal[i / 2] + 1; 55 | } 56 | 57 | solution += pal[i]; 58 | } 59 | 60 | cout << solution + 1 << "\n"; 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Codeforces/D. Palindrome pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef unsigned long long ull; 8 | 9 | const ull BASE = 666013; 10 | const int Lmax = 2e3 + 2; 11 | 12 | char S[Lmax]; 13 | int N; 14 | 15 | int start[Lmax]; 16 | int finish[Lmax]; 17 | 18 | ull H1[Lmax]; 19 | ull H2[Lmax]; 20 | ull Hpow[Lmax]; 21 | 22 | void pregen() 23 | { 24 | Hpow[0] = 1; 25 | 26 | for ( int i = 1; i <= N; ++i ) 27 | Hpow[i] = Hpow[i - 1] * BASE; 28 | 29 | for ( int i = 1; i <= N; ++i ) 30 | { 31 | H1[i] = H1[i - 1] * BASE + S[i]; 32 | H2[i] = H2[i - 1] * BASE + S[N - i + 1]; 33 | } 34 | } 35 | 36 | ull getHash( ull H[], int i, int j ) 37 | { 38 | return H[j] - H[i - 1] * Hpow[j - i + 1]; 39 | } 40 | 41 | int main() 42 | { 43 | ///ifstream cin("data.in"); 44 | 45 | cin.sync_with_stdio( false ); 46 | 47 | cin >> ( S + 1 ); 48 | N = strlen( S + 1 ); 49 | 50 | pregen(); 51 | 52 | for ( int i = 1; i <= N; ++i ) 53 | for ( int j = i; j <= N; ++j ) 54 | if ( getHash( H1, i, j ) == getHash( H2, N - j + 1, N - i + 1 ) ) 55 | { 56 | start[i]++; 57 | finish[j]++; 58 | } 59 | 60 | long long sol = 0; 61 | long long nr_end = 0; 62 | 63 | for ( int i = 1; i <= N; ++i ) 64 | { 65 | nr_end += finish[i - 1]; 66 | sol += 1LL * nr_end * start[i]; 67 | } 68 | 69 | cout << sol << "\n"; 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Codeforces/D. Vessels.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 2 * 1e5 + 1; 6 | 7 | int capacity[Nmax]; 8 | int flow[Nmax]; 9 | 10 | set Set; 11 | 12 | int N, Q; 13 | 14 | int main() 15 | { 16 | ///ifstream cin("data.in"); 17 | 18 | ios_base::sync_with_stdio(false); 19 | 20 | cin >> N; 21 | 22 | for (int i = 1; i <= N; ++i) 23 | cin >> capacity[i]; 24 | 25 | for (int i = 1; i <= N; ++i) 26 | Set.insert(i); 27 | 28 | cin >> Q; 29 | 30 | while (Q--) 31 | { 32 | int tip, p, x; 33 | 34 | cin >> tip; 35 | 36 | if (tip == 1) 37 | { 38 | cin >> p >> x; 39 | 40 | auto it = Set.lower_bound(p); 41 | vector toErase; 42 | 43 | if (it == Set.end()) 44 | continue; 45 | 46 | while (true) 47 | { 48 | int delta = min(capacity[*it] - flow[*it], x); 49 | 50 | flow[*it] += delta; 51 | x -= delta; 52 | 53 | if (flow[*it] == capacity[*it]) 54 | toErase.push_back(*it); 55 | 56 | it++; 57 | 58 | if (it == Set.end() || !x) break; 59 | } 60 | 61 | for (auto x: toErase) 62 | Set.erase(x); 63 | } 64 | else 65 | { 66 | cin >> p; 67 | cout << flow[p] << "\n"; 68 | } 69 | } 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Codeforces/H. Queries for Number of Palindromes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef unsigned long long ull; 8 | const ull BASE = 666013; 9 | 10 | const int Lmax = 5e3 + 2; 11 | 12 | char S[Lmax]; 13 | int N, Q; 14 | 15 | int D[Lmax][Lmax]; 16 | 17 | ull H1[Lmax]; 18 | ull H2[Lmax]; 19 | ull Hpow[Lmax]; 20 | 21 | void pregen() 22 | { 23 | Hpow[0] = 1; 24 | 25 | for ( int i = 1; i <= N; ++i ) 26 | Hpow[i] = Hpow[i - 1] * BASE; 27 | 28 | for ( int i = 1; i <= N; ++i ) 29 | { 30 | H1[i] = H1[i - 1] * BASE + S[i]; 31 | H2[i] = H2[i - 1] * BASE + S[N - i + 1]; 32 | } 33 | } 34 | 35 | ull getHash( ull H[], int i, int j ) 36 | { 37 | return H[j] - H[i - 1] * Hpow[j - i + 1]; 38 | } 39 | 40 | int isPal( int i, int j ) 41 | { 42 | return getHash( H1, i, j ) == getHash( H2, N - j + 1, N - i + 1 ); 43 | } 44 | 45 | void compute_dp() 46 | { 47 | for ( int i = N; i >= 1; i-- ) 48 | { 49 | D[i][i] = 1; 50 | 51 | for ( int j = i + 1; j <= N; ++j ) 52 | { 53 | D[i][j] = D[i + 1][j] + D[i][j - 1] - D[i + 1][j - 1] + isPal( i, j ); 54 | } 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | ///ifstream cin("data.in"); 61 | 62 | cin.sync_with_stdio( false ); 63 | 64 | cin >> ( S + 1 ); 65 | N = strlen( S + 1 ); 66 | 67 | pregen(); 68 | compute_dp(); 69 | 70 | cin >> Q; 71 | 72 | while ( Q-- ) 73 | { 74 | int x, y; 75 | 76 | cin >> x >> y; 77 | cout << D[x][y] << "\n"; 78 | } 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Contest-Classes/Bloom filter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct BloomFilter 6 | { 7 | static const int NumberOfPrimes = 4; 8 | static const int Primes[NumberOfPrimes]; 9 | 10 | struct BitSet 11 | { 12 | int N; 13 | unsigned long long *b; 14 | 15 | explicit BitSet() : N(0), b(nullptr) 16 | { 17 | } 18 | 19 | BitSet(const int _N) : N(_N), b(new unsigned long long[N / 64 + 1]) 20 | { 21 | for ( int i = 0; i < N / 64 + 1; ++i ) 22 | b[i] = 0; 23 | } 24 | 25 | void set(const int pos) 26 | { 27 | b[pos >> 6] |= ( 1ULL << ( pos & 63 ) ); 28 | } 29 | 30 | bool test(const int pos) 31 | { 32 | return b[pos >> 6] & ( 1ULL << ( pos & 63 ) ); 33 | } 34 | }; 35 | 36 | BitSet filter[NumberOfPrimes]; 37 | 38 | BloomFilter() 39 | { 40 | for ( int i = 0; i < NumberOfPrimes; ++i ) 41 | filter[i] = BitSet( Primes[i] ); 42 | } 43 | 44 | inline void insert(const long long key) 45 | { 46 | for ( int i = 0; i < NumberOfPrimes; ++i ) 47 | filter[i].set( key % Primes[i] ); 48 | } 49 | 50 | inline bool find(const long long key) 51 | { 52 | for ( int i = 0; i < NumberOfPrimes; ++i ) 53 | if ( filter[i].test( key % Primes[i] ) == 0 ) 54 | return false; 55 | 56 | return true; 57 | } 58 | }; 59 | 60 | const int BloomFilter::Primes[] = { 666013, 100003, 826663, 797593 }; 61 | 62 | int main() 63 | { 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Contest-Classes/Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | class FibonacciSolver 7 | { 8 | public: 9 | 10 | FibonacciSolver() 11 | { 12 | F.clear(); 13 | F[0] = F[1] = 1; 14 | } 15 | 16 | Type2 solve( Type1 n ) 17 | { 18 | return f(n); 19 | } 20 | 21 | private: 22 | 23 | map F; 24 | 25 | Type2 f( Type1 n ) 26 | { 27 | if ( F.count( n ) ) 28 | return F[n]; 29 | 30 | Type1 k = n >> 1; 31 | 32 | if ( n % 2 == 0 ) 33 | return F[n] = ( 1LL * f(k - 1) * f(k - 1) + 1LL * f(k) * f(k) ) % MOD; 34 | else 35 | return F[n] = ( 1LL * f(k - 1) * f(k) + 1LL * f(k) * f(k + 1) ) % MOD; 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Contest-Classes/Graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Graph 6 | { 7 | private: 8 | 9 | class Edge 10 | { 11 | public: 12 | 13 | int node; 14 | size_t next; 15 | 16 | Edge(int _node, size_t _next) : node(_node), next(_next){ 17 | } 18 | }; 19 | 20 | vector graph; 21 | vector head; 22 | int N; 23 | 24 | public: 25 | 26 | static const int NIL = -1; 27 | 28 | Graph(int _N) : graph(), head(), N(_N){ 29 | head.resize(N, NIL); 30 | } 31 | 32 | void addEdge(int x, int y) 33 | { 34 | assert(1 <= x && x <= N); 35 | x--; 36 | 37 | graph.push_back(Edge(y, head[x])); 38 | head[x] = graph.size() - 1; 39 | } 40 | 41 | void addDoubleEdge(int x, int y) 42 | { 43 | addEdge(x, y); 44 | addEdge(y, x); 45 | } 46 | 47 | int getHead(int node) const 48 | { 49 | assert(1 <= node && node <= N); 50 | return head[node - 1]; 51 | } 52 | 53 | int getNeighbour(size_t p) const 54 | { 55 | assert(p < graph.size()); 56 | return graph[p].node; 57 | } 58 | 59 | int getNext(size_t p) const 60 | { 61 | assert(p < graph.size()); 62 | return graph[p].next; 63 | } 64 | }; 65 | 66 | int main() 67 | { 68 | cout << "Hello world!" << endl; 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Contest-Classes/Modular Inverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class ModularInverse 6 | { 7 | public: 8 | 9 | ModularInverse(){} 10 | 11 | int modInv( int A, int N ) 12 | { 13 | int x, y; 14 | euclid( A, N, x, y ); 15 | 16 | if ( x <= 0 ) 17 | x = N + x % N; 18 | 19 | return x; 20 | } 21 | 22 | private: 23 | 24 | void euclid( int a, int b, int &x, int &y ) 25 | { 26 | if ( !b ) 27 | { 28 | x = 1; 29 | y = 0; 30 | } 31 | else 32 | { 33 | int x0, y0; 34 | 35 | euclid( b, a % b, x0, y0 ); 36 | 37 | x = y0; 38 | y = x0 - ( a / b ) * y0; 39 | } 40 | } 41 | 42 | }; 43 | 44 | int main() 45 | { 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Contest-Classes/Popcount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class CountBits 6 | { 7 | public: 8 | 9 | CountBits() 10 | { 11 | memset( table, 0, sizeof( table ) ); 12 | 13 | for ( int i = 0; i < 256; ++i ) 14 | table[i] = table[i / 2] + ( i & 1 ); 15 | } 16 | 17 | template 18 | int popcount( const T x ) const 19 | { 20 | try 21 | { 22 | if ( x < 0 ) 23 | throw -1; 24 | 25 | } catch( int err ) 26 | { 27 | cerr << "An exception occurred. The value is negative\n"; 28 | return err; 29 | } 30 | 31 | int sum = 0; 32 | int c = 32; 33 | 34 | if ( x >= ( 1LL << 31 ) ) 35 | c = 64; 36 | 37 | for ( int i = 0; i < c; i += 8 ) 38 | sum += table[ ( x >> i ) & 0xff ]; 39 | 40 | return sum; 41 | } 42 | private: 43 | 44 | int table[256]; 45 | }; 46 | 47 | int main() 48 | { 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Contest-Classes/Radix Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1e5; 6 | const int DIGITS = 32; 7 | const int R = 32 / DIGITS; 8 | const int radix = ( 1 << R ); 9 | const int mask = radix - 1; 10 | 11 | int A[Nmax], B[Nmax], buckets[radix]; 12 | int N; 13 | 14 | void RadixSort() 15 | { 16 | for ( int d = 0, shift = 0; d < DIGITS; ++d, shift += R ) 17 | { 18 | for ( int i = 0; i < radix; ++i ) 19 | buckets[i] = 0; 20 | 21 | for ( int i = 0; i < N; ++i ) 22 | ++buckets[ ( A[i] >> shift ) & mask ]; 23 | 24 | for ( int i = 1; i < radix; ++i ) 25 | buckets[i] += buckets[i - 1]; 26 | 27 | for ( int i = N - 1; i >= 0; i-- ) 28 | B[ --buckets[ ( A[i] >> shift ) & mask ] ] = A[i]; 29 | 30 | for ( int i = 0; i < N; ++i ) 31 | A[i] = B[i]; 32 | } 33 | } 34 | 35 | int main() 36 | { 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Contest-Classes/Simple parser.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | ///--------------------------------------------------- 6 | const int BUFFER_SIZE = (1 << 16); 7 | char buffer[BUFFER_SIZE]; 8 | int position = BUFFER_SIZE; 9 | 10 | inline char getChar() 11 | { 12 | if ( position == BUFFER_SIZE ) 13 | { 14 | position = 0; 15 | fread(buffer, BUFFER_SIZE, 1, stdin); 16 | } 17 | 18 | return buffer[position++]; 19 | } 20 | 21 | inline int getInt() 22 | { 23 | register int a = 0; 24 | char ch; 25 | int sgn = 1; 26 | 27 | do 28 | { 29 | ch = getChar(); 30 | 31 | } while ( !isdigit(ch) && ch != '-' ); 32 | 33 | if ( ch == '-' ) 34 | { 35 | sgn = -1; 36 | ch = getChar(); 37 | } 38 | 39 | do 40 | { 41 | a = (a << 3) + (a << 1) + ch - '0'; 42 | ch = getChar(); 43 | 44 | } while ( isdigit(ch) ); 45 | 46 | return a * sgn; 47 | } 48 | ///--------------------------------------------------- 49 | 50 | int main() 51 | { 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Data-Structures/RangeMinimumQuery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class RangeMinimumQuery 6 | { 7 | private: 8 | 9 | vector< vector > rmq; 10 | vector _log2; 11 | int N; 12 | 13 | public: 14 | 15 | RangeMinimumQuery(int N) 16 | { 17 | this->N = N; 18 | rmq.resize(1 + ceil(log2(N))); 19 | 20 | for (size_t i = 0; i < rmq.size(); ++i) 21 | rmq[i].resize(N + 1); 22 | 23 | _log2.resize(N + 1); 24 | } 25 | 26 | void setIndex(int index, int key) 27 | { 28 | assert(1 <= index && index <= N); 29 | rmq[0][index] = key; 30 | } 31 | 32 | void build() 33 | { 34 | for (int i = 1; (1 << i) <= N; ++i) 35 | for (int j = 1; j + (1 << i) - 1 <= N; ++j) 36 | { 37 | int p = 1 << (i - 1); 38 | rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + p]); 39 | } 40 | 41 | _log2[1] = 0; 42 | 43 | for (int i = 2; i <= N; ++i) 44 | _log2[i] = _log2[i >> 1] + 1; 45 | } 46 | 47 | int query(int x, int y) 48 | { 49 | assert(1 <= x && x <= y && y <= N); 50 | 51 | int dif = y - x + 1; 52 | int k = _log2[dif]; 53 | 54 | return min(rmq[k][x] , rmq[k][y - (1 << k) + 1]); 55 | } 56 | }; 57 | 58 | int main() 59 | { 60 | } 61 | -------------------------------------------------------------------------------- /Data-Structures/Segment Tree (non-recursive, fast implementation).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | const int NIL = numeric_limits::min(); 7 | 8 | int T[2 * Nmax]; 9 | int N, M; 10 | 11 | int join(int a, int b) 12 | { 13 | return max(a, b); 14 | } 15 | 16 | void build() 17 | { 18 | for (int i = N - 1; i >= 1; i--) 19 | T[i] = join(T[(i << 1)], T[(i << 1) | 1]); 20 | } 21 | 22 | void update(int x, int value) 23 | { 24 | x += N; 25 | 26 | T[x] = value; 27 | 28 | while (x > 1) 29 | { 30 | T[x >> 1] = join(T[x], T[x ^ 1]); 31 | x >>= 1; 32 | } 33 | } 34 | 35 | int query(int x, int y) /// [x, y) 36 | { 37 | x += N; 38 | y += N; 39 | 40 | int solX = NIL; 41 | int solY = NIL; 42 | 43 | while (x < y) 44 | { 45 | if (x & 1) 46 | { 47 | solX = join(solX, T[x]); 48 | x++; 49 | } 50 | 51 | if (y & 1) 52 | { 53 | y--; 54 | solY = join(T[y], solY); 55 | } 56 | 57 | x >>= 1; 58 | y >>= 1; 59 | } 60 | 61 | return join(solX, solY); 62 | } 63 | 64 | int main() 65 | { 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Data-Structures/Union-Find.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class UnionFind{ 6 | public: 7 | UnionFind(const int n) : Parent(vector(n + 1)), Rank(vector(n + 1)), Size(vector(n + 1)) { 8 | for (int i = 1; i <= n; ++i) 9 | MakeSet(i); 10 | } 11 | 12 | void MakeSet(int x) 13 | { 14 | Parent[x] = x; 15 | Rank[x] = 0; 16 | Size[x] = 1; 17 | } 18 | 19 | int Find(int x) 20 | { 21 | if (Parent[x] != x) 22 | Parent[x] = Find(Parent[x]); 23 | 24 | return Parent[x]; 25 | } 26 | 27 | void Union(int x, int y) 28 | { 29 | x = Find(x); 30 | y = Find(y); 31 | 32 | if (x != y) 33 | { 34 | /// append y to x 35 | if (Rank[x] > Rank[y]) 36 | { 37 | Parent[y] = x; 38 | Size[x] += Size[y]; 39 | } 40 | else /// append x to y 41 | { 42 | Parent[x] = y; 43 | Size[y] += Size[x]; 44 | 45 | if (Rank[x] == Rank[y]) 46 | Rank[y]++; 47 | } 48 | } 49 | } 50 | 51 | bool connected(int x, int y) 52 | { 53 | return Find(x) == Find(y); 54 | } 55 | 56 | int getSize(int x){ 57 | return Size[Find(x)]; 58 | } 59 | 60 | private: 61 | 62 | vector Parent; 63 | vector Rank; 64 | vector Size; 65 | }; 66 | 67 | int main() 68 | { 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Game-Theory/Acrostic Twins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Acrostic Twins 7 | 8 | Description: A move is to turn over two coins, either in the same row or 9 | the same column, with the southeast coin going from heads to tails. 10 | 11 | Strategy: 12 | 13 | g(x, y) = mex { g(x, b), g(a, y) : 0≤b Set; 30 | 31 | for ( int i = 0; i < y; i++ ) 32 | Set.insert( mex( x, i ) ); 33 | 34 | for ( int i = 0; i < x; ++i ) 35 | Set.insert( mex( i, y ) ); 36 | 37 | grundy[x][y] = 0; 38 | 39 | while ( Set.count( grundy[x][y] ) ) 40 | grundy[x][y]++; 41 | 42 | return grundy[x][y]; 43 | } 44 | 45 | int main() 46 | { 47 | memset( grundy, -1, sizeof( grundy ) ); 48 | 49 | for ( int i = 0; i <= 9; ++i, cout << endl ) 50 | for ( int j = 0; j <= 9; ++j ) 51 | cout << mex( i, j ) << " "; 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Game-Theory/Euclid's Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Euclid's Game 7 | 8 | Description: During his move the player chooses decreases the larger integer in the pair by a positive multiple 9 | of the smaller integer in the pair in such a way that both integers in the pair remain positive. 10 | 11 | Strategy: 12 | 13 | Sprague-Grundy values: 14 | 15 | The function for Euclid's game is g(a,b) = abs(a*a-b*b)/(a*b) 16 | Obviously: g(a,a) = 0 17 | **/ 18 | 19 | 20 | int sprague_grundy( int a, int b ) 21 | { 22 | return abs( a * a - b * b ) / ( a * b ); 23 | } 24 | 25 | int main() 26 | { 27 | int A, B; 28 | 29 | cin >> A >> B; 30 | cout << sprague_grundy( A, B ) << "\n"; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Game-Theory/Grunt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Grunt 7 | 8 | Description: Turn over four symmetric coins, one of which is the leftmost coin, with 9 | the rightmost coin going from heads to tails. 10 | 11 | Strategy: A singleton heads in positions 0, 1, or 2, is a terminal position and so has SG-value 0. 12 | A singleton heads in position n ≥ 3 gets replaced by heads in positions 0, x and n−x for some x Set; 30 | 31 | for ( int i = 1; i <= state / 2; ++i ) 32 | { 33 | if ( i != state - i ) 34 | Set.insert( mex( 0 ) ^ mex( i ) ^ mex( state - i ) ); 35 | } 36 | 37 | grundy[state] = 0; 38 | 39 | while ( Set.count( grundy[state] ) ) 40 | grundy[state]++; 41 | 42 | return grundy[state]; 43 | } 44 | 45 | int main() 46 | { 47 | memset( grundy, -1, sizeof( grundy ) ); 48 | 49 | for ( int i = 0; i <= 20; ++i ) 50 | cout << mex( i ) << ","; 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Game-Theory/Miseré Nim.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Miseré Nim 7 | Description: There are a finite number of heaps. 8 | The two players alternate taking any number of objects from any single one of the heaps. 9 | The one that removes the last object loses. 10 | 11 | Strategy: If all piles have one object each. If the number of heaps is even the first wins. 12 | If there is exactly one heap with more than 1 object, the first wins. 13 | If there are more: => Nim( who wins in normal Nim wins in Miseré Nim ). 14 | **/ 15 | 16 | const int Nmax = 100; 17 | 18 | int heap[Nmax]; 19 | int NumberOfHeaps; 20 | 21 | int main() 22 | { 23 | cin >> NumberOfHeaps; 24 | 25 | int xorsum = 0; 26 | int nrOne = 0; 27 | int nrMoreThanOne = 0; 28 | 29 | for ( int i = 0; i < NumberOfHeaps; ++i ) 30 | { 31 | cin >> heap[i]; 32 | 33 | if ( heap[i] == 1 ) 34 | nrOne++; 35 | 36 | if ( heap[i] > 1 ) 37 | nrMoreThanOne++; 38 | 39 | xorsum ^= heap[i]; 40 | } 41 | 42 | if ( nrOne == NumberOfHeaps ) 43 | { 44 | if ( nrOne % 2 == 0 ) 45 | cout << "First"; 46 | else 47 | cout << "Second"; 48 | } 49 | else 50 | { 51 | if ( nrMoreThanOne == 1 ) 52 | cout << "First"; 53 | else 54 | { 55 | if ( xorsum > 0 ) 56 | cout << "First"; 57 | else 58 | cout << "Second"; 59 | } 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Game-Theory/Nim Multiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100; 6 | 7 | int table[Nmax][Nmax]; 8 | 9 | int mex( int x, int y ) 10 | { 11 | if ( table[x][y] != -1 ) 12 | return table[x][y]; 13 | 14 | if ( x == 0 && y == 0 ) 15 | return table[x][y] = 0; 16 | 17 | unordered_set Set; 18 | 19 | for ( int a = 0; a < x; ++a ) 20 | for ( int b = 0; b < y; ++b ) 21 | Set.insert( mex( a, b ) ^ mex( a, y ) ^ mex( x, b ) ); 22 | 23 | table[x][y] = 0; 24 | 25 | while ( Set.count( table[x][y] ) ) 26 | table[x][y]++; 27 | 28 | return table[x][y]; 29 | } 30 | 31 | int main() 32 | { 33 | memset( table, -1, sizeof( table ) ); 34 | 35 | for ( int i = 0; i < 10; ++i, cout << endl ) 36 | for ( int j = 0; j < 10; ++j ) 37 | cout << mex( i, j ) << " "; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Game-Theory/Nim.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Nim 7 | Description: There are a finite number of heaps. 8 | The two players alternate taking any number of objects from any single one of the heaps. 9 | The goal is to be the last to take an object. 10 | **/ 11 | 12 | const int Nmax = 100; 13 | 14 | int heap[Nmax]; 15 | int NumberOfHeaps; 16 | 17 | int main() 18 | { 19 | cin >> NumberOfHeaps; 20 | 21 | int xorsum = 0; 22 | 23 | for ( int i = 0; i < NumberOfHeaps; ++i ) 24 | { 25 | cin >> heap[i]; 26 | 27 | xorsum ^= heap[i]; 28 | } 29 | 30 | if ( xorsum == 0 ) 31 | { 32 | cout << "Second wins!\n"; 33 | return 0; 34 | } 35 | 36 | cout << "First wins!\n"; 37 | 38 | for ( int i = 0; i < NumberOfHeaps; ++i ) 39 | if ( heap[i] >= ( heap[i] ^ xorsum ) ) 40 | { 41 | cout << "Move " << ( heap[i] - ( heap[i] ^ xorsum ) ) << " stones from " << i << " heap!\n"; 42 | break; 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Game-Theory/Nimble.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Nimble 7 | 8 | Description: Nimble is played on a game board consisting of a line of squares labeled: 0, 1, 2, 3, ... n. 9 | A finite number of coins is placed on the squares with possible more than one coin on a single square. 10 | A move consists of taking one of the coins and moving it to any square to the left, possibly moving 11 | over some of the coins, and possibly onto a square already containing one or more coins. 12 | The players alternate moves and the game ends when all coins a 13 | re on the square labeled 0. The last player to move wins. 14 | 15 | Strategy: A coin on square n is the same as a nim-heap of size n. 16 | **/ 17 | 18 | const int Nmax = 100; 19 | 20 | int coins[Nmax + 1]; 21 | int N; 22 | 23 | int main() 24 | { 25 | cin >> N; 26 | 27 | for ( int i = 0; i <= N; ++i ) 28 | cin >> coins[i]; 29 | 30 | int xorsum = 0; 31 | 32 | for ( int i = 1; i <= N; ++i ) 33 | if ( coins[i] & 1 ) 34 | xorsum ^= i; 35 | 36 | if ( xorsum ) 37 | cout << "First wins!"; 38 | else 39 | cout << "Second wins!"; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Game-Theory/Northcott's game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Northcott's game 7 | 8 | Description: In every row of a rectangular board, there are two checkers: one white and one black. 9 | A move consists in sliding a single checker in its original row without jumping over another checker. 10 | You play white, computer plays black. 11 | As usual, the player to make the last move wins. 12 | 13 | Strategy: The the number of spaces between the two tokens on each row are the sizes of the Nim heaps. 14 | This is the same as Poker Nim. If your opponent increases the number of spaces between two tokens, 15 | just decrease it on your next move. 16 | Else, play the game of Nim and make the Nim-sum of the number of spaces between the tokens on each row be 0. 17 | **/ 18 | 19 | const int Nmax = 100; 20 | 21 | int black[Nmax], white[Nmax]; /// (black[i] = the x coordinate of the black checker of row i) 22 | int W, H; 23 | 24 | int main() 25 | { 26 | cin >> W >> H; 27 | 28 | for ( int i = 1; i <= H; ++i ) 29 | cin >> black[i]; 30 | 31 | for ( int i = 1; i <= H; ++i ) 32 | cin >> white[i]; 33 | 34 | int xorsum = 0; 35 | 36 | for ( int i = 1; i <= H; ++i ) 37 | xorsum ^= abs( black[i] - white[i] ); 38 | 39 | if ( xorsum ) 40 | cout << "White wins!"; 41 | else 42 | cout << "Black wins!"; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Game-Theory/Ruler.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Ruler 7 | 8 | Description: Any number of coins may be turned over but they must be consecutive, and the rightmost 9 | coin must be turned from heads to tails. 10 | 11 | Strategy: 12 | g(n)=mex{0,g(n−1),g(n−1)⊕g(n−2),...,g(n−1)⊕...⊕g(1)}. 13 | Thus, g(x) is the largest power of 2 dividing x. 14 | **/ 15 | 16 | const int Nmax = 100; 17 | 18 | int grundy[Nmax]; 19 | 20 | int mex( int state ) 21 | { 22 | if ( grundy[state] != -1 ) 23 | return grundy[state]; 24 | 25 | if ( state == 0 ) 26 | return grundy[state] = 0; 27 | 28 | unordered_set Set; 29 | Set.insert( 0 ); 30 | 31 | int xorsum = 0; 32 | 33 | for ( int i = state - 1; i >= 1; i-- ) 34 | { 35 | xorsum ^= mex( i ); 36 | Set.insert( xorsum ); 37 | } 38 | 39 | grundy[state] = 0; 40 | 41 | while ( Set.count( grundy[state] ) ) 42 | grundy[state]++; 43 | 44 | return grundy[state]; 45 | } 46 | 47 | int main() 48 | { 49 | memset( grundy, -1, sizeof( grundy ) ); 50 | 51 | for ( int i = 0; i < 10; ++i ) 52 | cout << i << " " << mex( i ) << "\n"; 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Game-Theory/The 100 game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: The 100 game 7 | 8 | Description: Two players start from 0 and alternatively add a number from 1 to 10 to the sum. 9 | The player who reaches 100 wins. 10 | 11 | Strategy: The winning strategy is to reach a number in which the digits are subsequent (e.g. 01, 12, 23, 34,...) 12 | and control the game by jumping through all the numbers of this sequence. 13 | Once reached 89, the opponent has lost (he can only tell numbers from 90 to 99, and the next answer can in any case be 100). 14 | 15 | Sprague-Grundy values (0 based): 1, (0,10,9,8,7,6,5,4,3,2,1), (0,10,9,8,7,6,5,4,3,2,1), ... 16 | **/ 17 | 18 | const int Vmax = 100; 19 | 20 | int grundy[Vmax + 1]; 21 | int N, K = 10; 22 | 23 | int sprague_grundy( int state ) 24 | { 25 | if ( grundy[state] != -1 ) 26 | return grundy[state]; 27 | 28 | if ( state == Vmax ) 29 | return grundy[state] = 0; 30 | 31 | unordered_set Set; 32 | 33 | for ( int i = 1; i <= K; ++i ) 34 | if ( state + i <= Vmax ) 35 | Set.insert( sprague_grundy( state + i ) ); 36 | 37 | grundy[state] = 0; 38 | 39 | while ( Set.count( grundy[state] ) ) 40 | grundy[state]++; 41 | 42 | return grundy[state]; 43 | } 44 | 45 | int main() 46 | { 47 | memset( grundy, -1, sizeof( grundy ) ); 48 | 49 | if ( sprague_grundy( 0 ) ) 50 | cout << "First wins\n"; 51 | else 52 | cout << "Second wins\n"; 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Game-Theory/The 21 game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: The 21 game 7 | 8 | Description: The game "21" is played as a misère game with any number of players who take turns saying a number. 9 | The first player says "1" and each player in turn increases the number by 1, 2, or 3, but may not exceed 21; 10 | the player forced to say "21" loses 11 | 12 | Strategy: The winning strategy for the two-player version of this game is to always say a multiple of 4; 13 | it is then guaranteed that the other player will ultimately have to say 21. 14 | 15 | **/ 16 | 17 | const int Vmax = 21; 18 | 19 | int grundy[Vmax + 1]; 20 | int N, K = 3; 21 | 22 | int sprague_grundy( int state ) 23 | { 24 | if ( grundy[state] != -1 ) 25 | return grundy[state]; 26 | 27 | if ( state == Vmax ) 28 | return grundy[state] = 0; 29 | 30 | unordered_set Set; 31 | 32 | for ( int i = 1; i <= K; ++i ) 33 | if ( state + i <= Vmax ) 34 | Set.insert( sprague_grundy( state + i ) ); 35 | 36 | grundy[state] = 0; 37 | 38 | while ( Set.count( grundy[state] ) ) 39 | grundy[state]++; 40 | 41 | return grundy[state]; 42 | } 43 | 44 | int main() 45 | { 46 | memset( grundy, -1, sizeof( grundy ) ); 47 | 48 | if ( sprague_grundy( 0 ) ) 49 | cout << "First wins\n"; 50 | else 51 | cout << "Second wins\n"; 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Game-Theory/The subtraction game S(1,2,...,k).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: The subtraction game S(1,2,...,k) 7 | 8 | Description: It is similar to Nim. An upper bound is imposed 9 | on the number of objects that can be removed in a turn. 10 | Instead of removing arbitrarily many objects, a player 11 | can only remove 1 or 2 or ... or k at a time. 12 | 13 | Strategy: 14 | 15 | The first one loses if: 16 | n ≡ 0 (mod k+1) (in normal play) 17 | n ≡ 1 (mod k+1) (in misère play) 18 | 19 | Sprague-Grundy values: (0,1,2,3,...,k),(0,1,2,3,...,k),... 20 | **/ 21 | 22 | const int Nmax = 100; 23 | 24 | int grundy[Nmax]; 25 | int N, K; 26 | 27 | int sprague_grundy( int state ) 28 | { 29 | if ( grundy[state] != -1 ) 30 | return grundy[state]; 31 | 32 | if ( state == 0 ) 33 | return 0; 34 | 35 | unordered_set Set; 36 | 37 | for ( int i = 1; i <= K; ++i ) 38 | if ( state >= i ) 39 | Set.insert( sprague_grundy( state - i ) ); 40 | 41 | grundy[state] = 0; 42 | 43 | while ( Set.count( grundy[state] ) ) 44 | grundy[state]++; 45 | 46 | return grundy[state]; 47 | } 48 | 49 | int main() 50 | { 51 | memset( grundy, -1, sizeof( grundy ) ); 52 | 53 | cin >> N >> K; 54 | 55 | if ( sprague_grundy( N ) ) /// N % ( K + 1 ) != 0 56 | cout << "First wins\n"; 57 | else 58 | cout << "Second wins\n"; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Game-Theory/Turning Corners.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Turning Corners 7 | 8 | Description: A move consists of turning over four distinct coins at the corners of a rectangle, i.e. 9 | (a, b), (a, y), (x, b) and (x, y), where 0≤a Set; 29 | 30 | for ( int a = 0; a < x; ++a ) 31 | for ( int b = 0; b < y; ++b ) 32 | Set.insert( mex( a, b ) ^ mex( a, y ) ^ mex( x, b ) ); 33 | 34 | grundy[x][y] = 0; 35 | 36 | while ( Set.count( grundy[x][y] ) ) 37 | grundy[x][y]++; 38 | 39 | return grundy[x][y]; 40 | } 41 | 42 | int main() 43 | { 44 | memset( grundy, -1, sizeof( grundy ) ); 45 | 46 | for ( int i = 0; i <= 9; ++i, cout << endl ) 47 | for ( int j = 0; j <= 9; ++j ) 48 | cout << mex( i, j ) << " "; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Game-Theory/Twins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Twins 7 | 8 | Description: In Turning Turtles, we may turn over one or two coins, with the rightmost coin going from heads to tails. 9 | In the game, Twins, the rules are: Turn over exactly two coins, with the rightmost coin going from heads to tails. 10 | 11 | Strategy: If we label the positions of the coins from the left starting with 0, then the Sprague-Grundy 12 | function will be g(x)=x, the same as nim. 13 | **/ 14 | 15 | const int Nmax = 100; 16 | 17 | int grundy[Nmax]; 18 | 19 | int mex( int state ) 20 | { 21 | if ( grundy[state] != -1 ) 22 | return grundy[state]; 23 | 24 | if ( state == 0 ) 25 | return grundy[state] = 0; 26 | 27 | unordered_set Set; 28 | 29 | for ( int i = 0; i < state; ++i ) 30 | { 31 | Set.insert( mex( i ) ); 32 | } 33 | 34 | grundy[state] = 0; 35 | 36 | while ( Set.count( grundy[state] ) ) 37 | grundy[state]++; 38 | 39 | return grundy[state]; 40 | } 41 | 42 | int main() 43 | { 44 | memset( grundy, -1, sizeof( grundy ) ); 45 | 46 | for ( int i = 0; i < 10; ++i ) 47 | cout << mex( i ) << ","; 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Game-Theory/Wythoff's game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | Name: Wythoff's game 7 | 8 | Description: Wythoff's game is a two-player mathematical game of strategy, played with two piles of counters. 9 | Players take turns removing counters from one or both piles; in the latter case, the numbers of 10 | counters removed from each pile must be equal. 11 | The game ends when one person removes the last counter or counters, thus winning. 12 | 13 | Strategy: 14 | Cold position (n,m): nk = floor( k * phi ); 15 | mk = floor( k * phi ^ 2 ); 16 | **/ 17 | 18 | const double phi = 1.618033988749; 19 | 20 | int main() 21 | { 22 | int n, m; 23 | 24 | for ( int i = 1; i <= 10; ++i ) 25 | cout << (int)( i * phi ) << " " << (int)( i * phi * phi ) << "\n"; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Geometry-Algorithms/Area of a simple polygon O(N).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define Point pair 6 | #define x first 7 | #define y second 8 | 9 | /// assumes that polygon is given in clockwise/counter-clockwise order 10 | double signedArea(vector& points) 11 | { 12 | double area = 0; 13 | int N = points.size(); 14 | 15 | for (int i = 0; i < N; ++i) 16 | area += (points[i].x * points[(i + 1) % N].y - points[(i + 1) % N].x * points[i].y); 17 | 18 | return area / 2.0; 19 | } 20 | 21 | int main() 22 | { 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Graph-Algorithms/Graph implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Vmax = 100000 + 1; 6 | const int Emax = 200000 + 1; 7 | 8 | struct Graph 9 | { 10 | struct Node 11 | { 12 | int nod; 13 | int next; 14 | }; 15 | 16 | Node listEdges[Emax]; 17 | int head[Vmax]; 18 | int contor; 19 | 20 | Graph() 21 | { 22 | contor = 0; 23 | 24 | for ( int i = 0; i < Vmax; ++i ) 25 | head[i] = 0; 26 | } 27 | 28 | void addEdge( int a, int b ) 29 | { 30 | contor++; 31 | listEdges[contor].nod = b; 32 | listEdges[contor].next = head[a]; 33 | head[a] = contor; 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /HackerRank/ Chocolate Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 2; 6 | 7 | int d[Nmax]; 8 | map Map; 9 | 10 | int N; 11 | long long ans; 12 | 13 | void solve( int even = 0 ) 14 | { 15 | Map.clear(); 16 | Map[0] = 1; 17 | 18 | int xorsum = 0; 19 | 20 | for ( int i = N - even; i >= 1; i -= 2 ) 21 | { 22 | int aux = d[i]; 23 | 24 | if ( i > 1 ) aux -= d[i - 1]; 25 | 26 | ans += Map[xorsum ^ d[i]]; 27 | 28 | if ( i > 1 ) ans += Map[xorsum ^ aux]; 29 | 30 | xorsum ^= aux; 31 | Map[xorsum]++; 32 | } 33 | } 34 | 35 | int main() 36 | { 37 | cin >> N; 38 | 39 | for ( int i = 1; i <= N; ++i ) 40 | cin >> d[i]; 41 | 42 | solve( 0 ); 43 | solve( 1 ); 44 | 45 | cout << 1LL * N * ( N - 1 ) / 2 - ans; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /HackerRank/ Chocolate in Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1e6 + 2; 6 | 7 | int N; 8 | int A[Nmax]; 9 | 10 | int main() 11 | { 12 | cin >> N; 13 | 14 | for ( int i = 1; i <= N; ++i ) 15 | cin >> A[i]; 16 | 17 | int xorsum = 0; 18 | 19 | for ( int i = 1; i <= N; ++i ) 20 | xorsum ^= A[i]; 21 | 22 | if ( xorsum == 0 ) 23 | cout << "0\n"; 24 | else 25 | { 26 | int nrMoves = 0; 27 | 28 | for ( int i = 1; i <= N; ++i ) 29 | { 30 | int size = A[i] - ( xorsum ^ A[i] ); 31 | 32 | if ( 1 <= size && size <= A[i] ) 33 | nrMoves++; 34 | } 35 | 36 | cout << nrMoves << "\n"; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /HackerRank/Circle City.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long T, R, K; 6 | 7 | int main() 8 | { 9 | ///ifstream cin("data.in"); 10 | 11 | cin >> T; 12 | 13 | while ( T-- ) 14 | { 15 | cin >> R >> K; 16 | 17 | long long r = 1LL * R; 18 | long long c1 = 0, c3 = 0; 19 | 20 | for ( long long i = 1; i * i <= r; ++i ) 21 | { 22 | if ( r % i == 0 ) 23 | { 24 | if ( i % 4 == 1 ) 25 | c1++; 26 | 27 | if ( i % 4 == 3 ) 28 | c3++; 29 | 30 | if ( i != r / i ) 31 | { 32 | if ( ( r / i ) % 4 == 1 ) 33 | c1++; 34 | 35 | if ( ( r / i ) % 4 == 3 ) 36 | c3++; 37 | } 38 | } 39 | } 40 | 41 | if ( K >= 4LL * ( c1 - c3 ) ) 42 | cout << "possible\n"; 43 | else 44 | cout << "impossible\n"; 45 | 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /HackerRank/Mixing proteins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1e6 + 2; 6 | 7 | int dp[2][1000002]; 8 | int N, K; 9 | 10 | int main() 11 | { 12 | cin >> N >> K; 13 | 14 | int tip = 0; 15 | 16 | for ( int i = 0; i < N; ++i ) 17 | { 18 | char ch; 19 | 20 | cin >> ch; 21 | 22 | dp[tip][i] = ch - 'A'; 23 | } 24 | 25 | for ( int j = 0; ( 1 << j ) <= K; ++j ) 26 | { 27 | if ( ( 1 << j ) & K ) 28 | { 29 | for ( int i = 0; i < N; ++i ) 30 | dp[tip ^ 1][i] = dp[tip][i] ^ dp[tip][(i + (1< 2 | 3 | using namespace std; 4 | 5 | long long T, R, K; 6 | 7 | int vis[30]; 8 | 9 | int main() 10 | { 11 | ///ifstream cin("data.in"); 12 | 13 | string s; 14 | 15 | getline( cin, s ); 16 | 17 | for ( auto x: s ) 18 | { 19 | if ( !isalpha( x ) ) continue; 20 | 21 | vis[ tolower( x ) - 'a' ]++; 22 | } 23 | 24 | int valid = 1; 25 | 26 | for ( int i = 0; i < 26; ++i ) 27 | if ( vis[i] == 0 ) 28 | { 29 | valid = 0; 30 | } 31 | 32 | if ( valid ) 33 | cout << "pangram"; 34 | else 35 | cout << "not pangram"; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /HackerRank/Team Formation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ///ifstream cin("data.in"); 8 | 9 | int T, N; 10 | 11 | cin >> T; 12 | 13 | while ( T-- ) 14 | { 15 | vector v; 16 | map < int, multiset > MapSet; 17 | 18 | cin >> N; 19 | 20 | for ( int i = 0, a; i < N; ++i ) 21 | { 22 | cin >> a; 23 | v.push_back( a ); 24 | } 25 | 26 | sort( v.begin(), v.end() ); 27 | 28 | for ( int i = 0; i < N; ++i ) 29 | { 30 | if ( MapSet.count( v[i] - 1 ) ) 31 | { 32 | int dim = *MapSet[ v[i] - 1 ].begin(); 33 | MapSet[ v[i] - 1 ].erase( MapSet[ v[i] - 1 ].begin() ); 34 | MapSet[ v[i] ].insert( dim + 1 ); 35 | 36 | if ( MapSet[ v[i] - 1 ].empty() ) 37 | MapSet.erase( v[i] - 1 ); 38 | } 39 | else 40 | { 41 | MapSet[ v[i] ].insert( 1 ); 42 | } 43 | } 44 | 45 | if ( MapSet.size() == 0 ) 46 | { 47 | cout << "0\n"; 48 | continue; 49 | } 50 | 51 | int sol = 1e9; 52 | 53 | for ( auto x: MapSet ) 54 | if ( x.second.size() ) 55 | sol = min( sol, *x.second.begin() ); 56 | 57 | cout << sol << "\n"; 58 | } 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /HackerRank/Vertical Rooks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 2e3 + 2; 6 | 7 | int A[2 * Nmax]; 8 | int T, N; 9 | 10 | int main() 11 | { 12 | cin >> T; 13 | 14 | while ( T-- ) 15 | { 16 | cin >> N; 17 | 18 | for ( int i = 1; i <= 2 * N; ++i ) 19 | cin >> A[i]; 20 | 21 | int xorsum = 0; 22 | 23 | for ( int i = 1; i <= N; ++i ) 24 | xorsum ^= abs( A[i] - A[i + N] ) - 1; 25 | 26 | if ( xorsum ) 27 | cout << "player-2\n"; 28 | else 29 | cout << "player-1\n"; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Hash-Tables/Bloom filter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class BloomFilter 6 | { 7 | private: 8 | 9 | class Bitset 10 | { 11 | private: 12 | 13 | unsigned long long *b; 14 | 15 | public: 16 | 17 | Bitset() : b(nullptr) { 18 | } 19 | 20 | Bitset(const int _size) : b(new unsigned long long[1 + (_size >> 6)]) { 21 | 22 | memset(b, 0, sizeof(b)); 23 | } 24 | 25 | void set(const int p) 26 | { 27 | b[p >> 6] |= (1ULL << (p & 63)); 28 | } 29 | 30 | bool test(const int p) const 31 | { 32 | return (b[p >> 6] >> (p & 63)) & 1; 33 | } 34 | }; 35 | 36 | int N; 37 | vector primes; 38 | 39 | Bitset *filter; 40 | 41 | public: 42 | 43 | BloomFilter() : N(0), primes(vector()), filter(nullptr) { 44 | } 45 | 46 | BloomFilter(const vector &v) : N(v.size()), primes(v), filter(new Bitset[v.size()]) { 47 | 48 | for (int i = 0; i < N; ++i) 49 | filter[i] = Bitset(primes[i]); 50 | } 51 | 52 | inline void insert(const int key) 53 | { 54 | for (int i = 0; i < N; ++i) 55 | filter[i].set(key % primes[i]); 56 | } 57 | 58 | inline bool find(const int key) const 59 | { 60 | for (int i = 0; i < N; ++i) 61 | if (filter[i].test(key % primes[i]) == false) 62 | return false; 63 | 64 | return true; 65 | } 66 | }; 67 | 68 | int main() 69 | { 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Hash-Tables/Linear probing (best implementation).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | class LinearProbing 7 | { 8 | private: 9 | 10 | static const int NIL = -1; 11 | static const int DELETED = -2; 12 | 13 | int table[MAX_SIZE]; 14 | 15 | inline bool check(const size_t pos, const int key) const 16 | { 17 | return table[pos] != NIL && table[pos] != key; 18 | } 19 | 20 | int supposedPosition(const int key) const 21 | { 22 | size_t pos = 0; 23 | 24 | while (check((key + pos) % MAX_SIZE, key) == true) 25 | pos++; 26 | 27 | return (key + pos) % MAX_SIZE; 28 | } 29 | 30 | public: 31 | 32 | LinearProbing() 33 | { 34 | for (int i = 0; i < MAX_SIZE; ++i) 35 | table[i] = NIL; 36 | } 37 | 38 | void insert(const int key) 39 | { 40 | table[supposedPosition(key)] = key; 41 | } 42 | 43 | bool find(const int key) 44 | { 45 | return table[supposedPosition(key)] == key; 46 | } 47 | 48 | void erase(const int key) 49 | { 50 | int pos = supposedPosition(key); 51 | 52 | if (table[pos] == key) 53 | table[pos] = DELETED; 54 | } 55 | }; 56 | 57 | int main() 58 | { 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Hash-Tables/Linear probing (simple implementation).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | class LinearProbing 7 | { 8 | private: 9 | 10 | static const int NIL = -1; 11 | static const int DELETED = -2; 12 | 13 | int *table; 14 | 15 | public: 16 | 17 | LinearProbing() : table(new int [MAX_SIZE]) { 18 | 19 | for (size_t i = 0; i < MAX_SIZE; ++i ) 20 | table[i] = NIL; 21 | } 22 | 23 | ~LinearProbing() 24 | { 25 | delete[] table; 26 | } 27 | 28 | size_t h(const int key) const 29 | { 30 | return hash()(key) % MAX_SIZE; 31 | } 32 | 33 | void insert(const int key) 34 | { 35 | size_t p = h(key); 36 | 37 | while (table[p] != NIL && table[p] != DELETED && table[p] != key) 38 | p = (p + 1) % MAX_SIZE; 39 | 40 | table[p] = key; 41 | } 42 | 43 | bool find(const int key) const 44 | { 45 | size_t p = h(key); 46 | 47 | while (table[p] != NIL && table[p] != key) 48 | p = (p + 1) % MAX_SIZE; 49 | 50 | return table[p] == key; 51 | } 52 | 53 | void erase(const int key) 54 | { 55 | size_t p = h(key); 56 | 57 | while (table[p] != NIL && table[p] != key) 58 | p = (p + 1) % MAX_SIZE; 59 | 60 | if (table[p] == key) 61 | table[p] = DELETED; 62 | } 63 | }; 64 | 65 | int main() 66 | { 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Hash-Tables/Quadratic probing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | class QuadraticProbing 7 | { 8 | private: 9 | 10 | static const int NIL = -1; 11 | static const int DELETED = -2; 12 | 13 | int table[MAX_SIZE]; 14 | 15 | inline bool check(const size_t pos, const int key) const 16 | { 17 | return table[pos] != NIL && table[pos] != key; 18 | } 19 | 20 | int supposedPosition(const int key) const 21 | { 22 | size_t pos = 0; 23 | 24 | while (check((key + pos * pos) % MAX_SIZE, key) == true) 25 | pos++; 26 | 27 | return (key + pos * pos) % MAX_SIZE; 28 | } 29 | 30 | public: 31 | 32 | QuadraticProbing() 33 | { 34 | for (int i = 0; i < MAX_SIZE; ++i) 35 | table[i] = NIL; 36 | } 37 | 38 | void insert(const int key) 39 | { 40 | table[supposedPosition(key)] = key; 41 | } 42 | 43 | bool find(const int key) 44 | { 45 | return table[supposedPosition(key)] == key; 46 | } 47 | 48 | void erase(const int key) 49 | { 50 | int pos = supposedPosition(key); 51 | 52 | if (table[pos] == key) 53 | table[pos] = DELETED; 54 | } 55 | }; 56 | 57 | int main() 58 | { 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Infoarena/2sec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1001000 + 1; 6 | 7 | int A[Nmax]; 8 | int MaxSecv[Nmax]; 9 | int MinSecv[Nmax]; 10 | int N; 11 | 12 | int main() 13 | { 14 | ifstream in("2sec.in"); 15 | ofstream out("2sec.out"); 16 | 17 | in >> N; 18 | 19 | for ( int i = 1; i <= N; ++i ) 20 | in >> A[i]; 21 | 22 | for ( int i = 1; i <= N; ++i ) 23 | MinSecv[i] = min( MinSecv[i - 1], 0 ) + A[i]; 24 | 25 | for ( int i = N; i >= 1; i-- ) 26 | MaxSecv[i] = max( MaxSecv[i + 1], 0 ) + A[i]; 27 | 28 | int sol = -1e9; 29 | 30 | for ( int i = 1; i <= N; ++i ) 31 | sol = max( sol, MaxSecv[i + 1] - MinSecv[i] ); 32 | 33 | out << sol << "\n"; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Infoarena/Alinuta.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | 7 | int complement[Nmax]; 8 | int K, T, A, B; 9 | 10 | void preproc() 11 | { 12 | int ratie = 0; 13 | 14 | for ( int i = 0; i < Nmax; ++i ) 15 | { 16 | if ( complement[i] || i + ratie >= Nmax ) 17 | continue; 18 | 19 | complement[i] = i + ratie; 20 | 21 | if ( i + ratie < Nmax ) 22 | complement[i + ratie] = i; 23 | 24 | ratie += (K + 1); 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | ifstream in("alinuta.in"); 31 | ofstream out("alinuta.out"); 32 | 33 | in >> K >> T; 34 | 35 | preproc(); 36 | 37 | for ( int i = 1; i <= T; ++i ) 38 | { 39 | in >> A >> B; 40 | 41 | if ( complement[A] == B || complement[B] == A ) 42 | out << "B\n"; 43 | else 44 | out << "A\n"; 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Infoarena/Amlei.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 5e2 + 2; 6 | 7 | int a[10]; 8 | int N, T, U; 9 | 10 | int main() 11 | { 12 | ifstream in("amlei.in"); 13 | ofstream out("amlei.out"); 14 | 15 | in.sync_with_stdio( false ); 16 | 17 | while ( in >> N >> T >> U ) 18 | { 19 | vector < vector > V[2]; 20 | 21 | a[0] = T; 22 | a[1] = U; 23 | 24 | for ( int k = 0; k < 2; ++k ) 25 | { 26 | for ( int i = 0; i < a[k]; ++i ) 27 | { 28 | vector v; 29 | 30 | for ( int j = 0, x; j < N; ++j ) 31 | { 32 | in >> x; 33 | v.push_back( x ); 34 | } 35 | 36 | sort( v.begin(), v.end() ); 37 | V[k].push_back( v ); 38 | } 39 | 40 | sort( V[k].begin(), V[k].end() ); 41 | V[k].erase( unique( V[k].begin(), V[k].end() ), V[k].end() ); 42 | } 43 | 44 | if ( V[0] == V[1] ) 45 | out << "DA\n"; 46 | else 47 | out << "NU\n"; 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Infoarena/Charlie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | 7 | char str[Nmax]; 8 | char stiva[Nmax]; 9 | int top = 0; 10 | 11 | int main() 12 | { 13 | ifstream in("charlie.in"); 14 | ofstream out("charlie.out"); 15 | 16 | int p; 17 | 18 | in >> p; 19 | in >> str; 20 | 21 | if (p == 1) 22 | { 23 | int lgmax = 0; 24 | int n = strlen(str); 25 | 26 | for (int i = 0; i < n; ++i) 27 | { 28 | int j = i; 29 | int lg = 0; 30 | 31 | while (j + 2 < n && str[j] > str[j + 1] && str[j + 1] < str[j + 2]) 32 | { 33 | j += 2; 34 | lg += 2; 35 | } 36 | 37 | if (lg) 38 | { 39 | lg++; 40 | i = j - 1; 41 | } 42 | 43 | lgmax = max(lgmax, lg); 44 | } 45 | 46 | out << lgmax << "\n"; 47 | } 48 | else 49 | { 50 | int suma = 0; 51 | int n = strlen(str); 52 | 53 | for (int i = 0; i < n; ++i) 54 | { 55 | while (top >= 2 && stiva[top - 2] > stiva[top - 1] && stiva[top - 1] < str[i]) 56 | { 57 | suma += (max(stiva[top - 2], str[i]) - 'a' + 1); 58 | top--; 59 | } 60 | 61 | stiva[top++] = str[i]; 62 | } 63 | 64 | for (int i = 0; i < top; ++i) 65 | out << stiva[i]; 66 | 67 | out << "\n" << suma << "\n"; 68 | } 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Infoarena/ChatNoir.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ifstream in("chatnoir.in"); 8 | ofstream out("chatnoir.out"); 9 | 10 | int T, N, M, X, Y; 11 | 12 | in >> T; 13 | 14 | while ( T-- ) 15 | { 16 | in >> N >> M >> X >> Y; 17 | 18 | if ( min( min( X - 1, Y - 1 ), min( N - X, M - Y ) ) <= 4 ) 19 | out << "DA\n"; 20 | else 21 | out << "NU\n"; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Infoarena/Coco Chanel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1e5 + 2; 6 | 7 | pair A[Nmax]; 8 | int B[Nmax], sol[Nmax]; 9 | int N, M; 10 | 11 | int cauta( int aux, int pos ) 12 | { 13 | int st = pos, dr = M, gasit = -1; 14 | 15 | while ( st <= dr ) 16 | { 17 | int m = ( st + dr ) / 2; 18 | 19 | if ( aux <= B[m] ) 20 | { 21 | gasit = m; 22 | dr = m - 1; 23 | } 24 | else 25 | st = m + 1; 26 | } 27 | 28 | return gasit; 29 | } 30 | 31 | int main() 32 | { 33 | ifstream in("cocochanel.in"); 34 | ofstream out("cocochanel.out"); 35 | 36 | ios_base::sync_with_stdio( false ); 37 | 38 | in >> N >> M; 39 | 40 | for ( int i = 1; i <= N; ++i ) 41 | { 42 | in >> A[i].first; 43 | A[i].second = i; 44 | } 45 | 46 | for ( int j = 1; j <= M; ++j ) 47 | in >> B[j]; 48 | 49 | sort( A + 1, A + N + 1 ); 50 | sort( B + 1, B + M + 1 ); 51 | 52 | for ( int i = 1; i <= N; ++i ) 53 | { 54 | int s = 0; 55 | int aux = A[i].first; 56 | 57 | int j = 1, p; 58 | int cont = 0; 59 | 60 | do 61 | { 62 | p = cauta( aux, j ); 63 | 64 | aux *= 2; 65 | 66 | j = p + 1; 67 | if ( p != -1 ) cont++; 68 | 69 | } while ( p != -1 ); 70 | 71 | sol[ A[i].second ] = cont; 72 | } 73 | 74 | for ( int i = 1; i <= N; ++i ) 75 | out << sol[i] << "\n"; 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Infoarena/Colier.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ifstream in("colier.in"); 8 | ofstream out("colier.out"); 9 | 10 | ios_base::sync_with_stdio(false); 11 | 12 | int T; 13 | in >> T; 14 | 15 | while (T--) 16 | { 17 | int N, nrZero = 0, nrUnu = 0; 18 | char ch; 19 | 20 | in >> N; 21 | 22 | for (int k = 0; k < N; ++k) 23 | { 24 | in >> ch; 25 | 26 | int col = ch - '0'; 27 | 28 | if (col == 0) 29 | nrZero++; 30 | else 31 | nrUnu++; 32 | } 33 | 34 | if (nrUnu == 0) 35 | { 36 | out << "NU\n"; 37 | continue; 38 | } 39 | 40 | if (nrZero == 0) 41 | { 42 | if (N == 1) 43 | out << "DA\n"; 44 | else 45 | out << "NU\n"; 46 | 47 | continue; 48 | } 49 | 50 | if (nrZero & 1) 51 | out << "DA\n"; 52 | else 53 | out << "NU\n"; 54 | } 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Infoarena/Color5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | ifstream in("color5.in"); 6 | ofstream out("color5.out"); 7 | 8 | int N; 9 | 10 | void printEdge(int x, int y, int c) 11 | { 12 | out << x << " " << y << " " << c << "\n"; 13 | } 14 | 15 | int main() 16 | { 17 | in >> N; 18 | 19 | if ( N == 3 ) 20 | { 21 | out << "1\n"; 22 | 23 | for ( int i = 0; i < N; ++i ) 24 | printEdge(i, N, 1); 25 | 26 | for ( int i = 0; i < N; ++i ) 27 | printEdge(i, (i + 1) % N, 1); 28 | } 29 | 30 | if ( 4 <= N && N <= 6 ) 31 | { 32 | out << "2\n"; 33 | 34 | for ( int i = 0; i < N; ++i ) 35 | printEdge(i, N, 1 + (i & 1)); 36 | 37 | for ( int i = 0; i < N; ++i ) 38 | printEdge(i, (i + 1) % N, 1 + (i & 1)); 39 | } 40 | 41 | if ( N >= 7 ) 42 | { 43 | out << "3\n"; 44 | 45 | for ( int i = 0; i < N; ++i ) 46 | printEdge(i, N, 1 + (i & 1)); 47 | 48 | for ( int i = 0; i < N; ++i ) 49 | printEdge(i, (i + 1) % N, 3); 50 | } 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Infoarena/Congr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Pmax = 2 * 300000 + 1; 6 | 7 | int P; 8 | int value[Pmax]; 9 | int ind[Pmax]; 10 | 11 | int main() 12 | { 13 | ifstream in("congr.in"); 14 | ofstream out("congr.out"); 15 | 16 | ios_base::sync_with_stdio(false); 17 | 18 | in >> P; 19 | 20 | long long sum = 0; 21 | 22 | for ( int i = 1; i <= 2 * P - 1; ++i ) 23 | { 24 | in >> value[i]; 25 | ind[i] = i; 26 | 27 | if ( i <= P ) 28 | sum += value[i]; 29 | } 30 | 31 | srand((time(0))); 32 | 33 | while ( sum % P != 0 ) 34 | { 35 | int x = 1 + rand() % P; 36 | int y = P + 1 + rand() % (P - 2); 37 | 38 | sum -= value[ ind[x] ]; 39 | sum += value[ ind[y] ]; 40 | 41 | ///swap( value[ ind[x] ], value[ ind[y] ] ); 42 | swap( ind[x], ind[y] ); 43 | } 44 | 45 | for ( int i = 1; i <= P; ++i ) 46 | out << ind[i] << " "; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Infoarena/Culori.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 2 * 256 + 1; 6 | 7 | int A[Nmax][Nmax]; 8 | int Euler[Nmax]; 9 | int N; 10 | 11 | int main() 12 | { 13 | ifstream in("culori.in"); 14 | ofstream out("culori.out"); 15 | 16 | in.sync_with_stdio( false ); 17 | 18 | in >> N; 19 | 20 | N = 2 * N - 1; 21 | 22 | for ( int i = 1; i <= N; ++i ) 23 | in >> Euler[i]; 24 | 25 | for ( int i = N; i >= 1; i-- ) 26 | { 27 | A[i][i] = 1; 28 | 29 | for ( int j = i + 1; j <= N; ++j ) 30 | { 31 | if ( ( ( i + j ) % 2 == 0 ) && Euler[i] == Euler[j] ) 32 | { 33 | for ( int k = i + 1; k <= j - 1; ++k ) 34 | A[i][j] = ( A[i][j] + A[i + 1][k] * A[k + 1][j] ) % 9901; 35 | } 36 | } 37 | } 38 | 39 | out << A[1][N]; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Infoarena/Desc2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 500 + 1; 6 | const int MOD = 10007; 7 | 8 | int **dp; 9 | int N, K, X; 10 | 11 | int memo(int n, int k) 12 | { 13 | if ( k > n ) 14 | return 0; 15 | 16 | if ( k == n ) 17 | return 1; 18 | 19 | if ( n <= 0 || k <= 0 ) 20 | return 0; 21 | 22 | if ( dp[n][k] != -1 ) 23 | return dp[n][k]; 24 | 25 | return dp[n][k] = (memo(n - 1, k - 1) + memo(n - k, k)) % MOD; 26 | } 27 | 28 | int main() 29 | { 30 | ifstream in("desc2.in"); 31 | ofstream out("desc2.out"); 32 | 33 | in >> N >> K >> X; 34 | 35 | int A = N - X * K * (K - 1) / 2; 36 | 37 | dp = new int*[A + 1]; 38 | 39 | for ( int i = 0; i <= A; ++i ) 40 | { 41 | dp[i] = new int[K + 1]; 42 | 43 | for ( int j = 0; j < K + 1; ++j ) 44 | dp[i][j] = -1; 45 | } 46 | 47 | out << memo(A, K) << "\n"; 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Infoarena/Fenrir.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector < pair > edges; 6 | 7 | int main() 8 | { 9 | ofstream out("fenrir.out"); 10 | 11 | for ( int i = 1; i <= 9; ++i ) 12 | for ( int j = 10; j <= 20; ++j ) 13 | edges.push_back( pair( i, j ) ); 14 | 15 | out << edges.size() << "\n"; 16 | 17 | for ( unsigned i = 0; i < edges.size(); ++i ) 18 | out << edges[i].first << " " << edges[i].second << "\n"; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Infoarena/Gardieni.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MAX_T = 1000000; 6 | 7 | int v[MAX_T + 1]; 8 | int N, T; 9 | 10 | int main() 11 | { 12 | ifstream in("gardieni.in"); 13 | ofstream out("gardieni.out"); 14 | 15 | in >> N >> T; 16 | 17 | for (int i = 1; i <= T; ++i) 18 | v[i] = numeric_limits::max(); 19 | 20 | while (N--) 21 | { 22 | int a, b, c; 23 | in >> a >> b >> c; 24 | 25 | for (int i = a; i <= b; ++i) 26 | v[i] = min(v[i], c); 27 | } 28 | 29 | long long sol = 0; 30 | 31 | for (int i = 1; i <= T; ++i) 32 | sol += (long long)v[i]; 33 | 34 | out << sol << "\n"; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Infoarena/Grid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | list L[3]; 6 | int N, K; 7 | 8 | int main() 9 | { 10 | ifstream in("grid.in"); 11 | ofstream out("grid.out"); 12 | 13 | in >> N >> K; 14 | 15 | for ( int i = 1, id = 0; i <= 3 * N; ++i ) 16 | { 17 | L[id].push_back( i ); 18 | 19 | if ( i % N == 0 ) 20 | id++; 21 | } 22 | 23 | while ( K-- ) 24 | { 25 | int a, b, c, d; 26 | 27 | in >> a >> b >> c >> d; 28 | 29 | list::iterator it1 = L[a].begin(); 30 | advance( it1, b ); 31 | 32 | int value = *it1; 33 | L[a].erase( it1 ); 34 | 35 | list::iterator it2 = L[c].begin(); 36 | advance( it2, d ); 37 | 38 | L[c].insert( it2, value ); 39 | } 40 | 41 | for ( int i = 0; i < 3; ++i ) 42 | { 43 | for ( list::iterator x = L[i].begin(); x != L[i].end(); ++x ) 44 | out << *x << " "; 45 | 46 | out << "\n"; 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Infoarena/Grozavesti.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 300 + 1; 6 | 7 | struct Mutare 8 | { 9 | char tip; 10 | int x, y; 11 | }; 12 | 13 | Mutare M[2 * Nmax]; 14 | int nrMutari; 15 | 16 | int v[Nmax]; 17 | int N; 18 | 19 | int main() 20 | { 21 | ifstream in("grozavesti.in"); 22 | ofstream out("grozavesti.out"); 23 | 24 | in >> N; 25 | 26 | for ( int i = 1; i <= N; ++i ) 27 | for ( int j = 1; j <= N; ++j ) 28 | { 29 | int a; 30 | in >> a; 31 | 32 | if ( i == j ) 33 | v[i] = a; 34 | } 35 | 36 | for ( int i = 1; i <= N; ++i ) 37 | { 38 | int best = 1e9; 39 | int x = 0; 40 | 41 | for ( int j = i + 1; j <= N; ++j ) 42 | { 43 | if ( best > v[j] ) 44 | { 45 | best = v[j]; 46 | x = j; 47 | } 48 | } 49 | 50 | if ( best < v[i] ) 51 | { 52 | swap(v[i], v[x]); 53 | 54 | nrMutari++; 55 | M[nrMutari] = {'L', i, x}; 56 | nrMutari++; 57 | M[nrMutari] = {'C', i, x}; 58 | } 59 | } 60 | 61 | out << nrMutari << "\n"; 62 | 63 | for ( int i = 1; i <= nrMutari; ++i ) 64 | out << M[i].tip << " " << M[i].x << " " << M[i].y << "\n"; 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Infoarena/Joc5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100 + 1; 6 | 7 | int A[Nmax][Nmax]; 8 | int N; 9 | 10 | int main() 11 | { 12 | ifstream in("joc5.in"); 13 | ofstream out("joc5.out"); 14 | 15 | in >> N; 16 | 17 | do 18 | { 19 | for ( int i = 1; i <= N; ++i ) 20 | for ( int j = 1; j <= N; ++j ) 21 | in >> A[i][j]; 22 | 23 | int xorsum = 0; 24 | 25 | for ( int i = 1; i <= N; ++i ) 26 | xorsum ^= A[i][i]; 27 | 28 | if ( xorsum ) 29 | out << "1\n"; 30 | else 31 | out << "2\n"; 32 | 33 | in >> N; 34 | 35 | } while ( N ); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Infoarena/Log.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 10000 + 1; 6 | const int INF = 2e9; 7 | 8 | int a[Nmax]; 9 | int b[Nmax]; 10 | int dp[Nmax]; 11 | 12 | int N, t1, t2, t3; 13 | 14 | int costEval(int A, int B) 15 | { 16 | return t3 * (A - B) * (A - B); 17 | } 18 | 19 | int main() 20 | { 21 | ifstream in("log.in"); 22 | ofstream out("log.out"); 23 | 24 | in >> N; 25 | in >> t1 >> t2 >> t3; 26 | 27 | for ( int i = 1; i <= N; ++i ) 28 | in >> a[i] >> b[i]; 29 | 30 | for ( int i = 1; i <= N; ++i ) 31 | { 32 | int CST = costEval(a[i], b[i]) + dp[i - 1]; 33 | 34 | if ( i != 1 ) 35 | CST += t1; 36 | 37 | dp[i] = CST; 38 | 39 | int auxA = a[i]; 40 | int auxB = b[i]; 41 | 42 | int cost2 = 0; 43 | 44 | for ( int j = i - 1; j >= 1; j-- ) 45 | { 46 | if ( b[j] == auxA ) 47 | { 48 | cost2 += t2; 49 | auxA = a[j]; 50 | auxB = auxB; 51 | 52 | int COST = dp[j - 1] + cost2 + costEval(auxA, auxB); 53 | 54 | if ( j != 1 ) 55 | COST += t1; 56 | 57 | dp[i] = min(dp[i], COST); 58 | } 59 | else 60 | break; 61 | } 62 | } 63 | 64 | out << dp[N] << "\n"; 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Infoarena/Magic Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 20000; 6 | 7 | int v[Nmax]; 8 | int T, N; 9 | 10 | int main() 11 | { 12 | ifstream in("magicsequence.in"); 13 | ofstream out("magicsequence.out"); 14 | 15 | in >> T; 16 | 17 | while ( T-- ) 18 | { 19 | in >> N; 20 | 21 | for ( int i = 0; i < N; ++i ) 22 | in >> v[i]; 23 | 24 | if ( v[0] <= v[N - 1] ) 25 | out << "YES\n"; 26 | else 27 | out << "NO\n"; 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Infoarena/Map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int Lmax = 2000 + 2; 8 | 9 | char S[Lmax]; 10 | int pi[Lmax]; 11 | int N, M; 12 | 13 | void build_prefix() 14 | { 15 | int lgprefix = 0; 16 | 17 | pi[1] = 0; 18 | 19 | for ( int i = 2; i <= M; ++i ) 20 | { 21 | while ( lgprefix > 0 && S[i] != S[ lgprefix + 1 ] ) 22 | lgprefix = pi[ lgprefix ]; 23 | 24 | if ( S[i] == S[ lgprefix + 1 ] ) 25 | lgprefix++; 26 | 27 | pi[i] = lgprefix; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | ifstream in("map.in"); 34 | ofstream out("map.out"); 35 | 36 | in >> N >> M; 37 | 38 | int sol = 0; 39 | 40 | for ( int i = 1; i <= N; ++i ) 41 | { 42 | in >> ( S + 1 ); 43 | 44 | build_prefix(); 45 | 46 | int best = M; 47 | 48 | while ( pi[best] > M / 2 ) 49 | best = pi[best]; 50 | 51 | sol = max( sol, best ); 52 | } 53 | 54 | if ( sol ) 55 | { 56 | out << sol << "\n"; 57 | } 58 | else 59 | { 60 | out << M << "\n"; 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Infoarena/Medie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 9000; 6 | const int Vmax = 7000; 7 | 8 | int Hash[Vmax + 1]; 9 | int v[Nmax]; 10 | int N; 11 | 12 | int main() 13 | { 14 | ifstream in("medie.in"); 15 | ofstream out("medie.out"); 16 | 17 | ios_base::sync_with_stdio(false); 18 | 19 | in >> N; 20 | 21 | for ( int i = 1; i <= N; ++i ) 22 | { 23 | in >> v[i]; 24 | Hash[v[i]]++; 25 | } 26 | 27 | int sol = 0; 28 | 29 | for ( int i = 1; i <= N; ++i ) 30 | for ( int j = i + 1; j <= N; ++j ) 31 | { 32 | if ( (v[i] + v[j]) % 2 == 0 ) 33 | { 34 | int medie = (v[i] + v[j]) / 2; 35 | 36 | sol += Hash[medie]; 37 | 38 | if ( v[i] == medie ) 39 | sol--; 40 | 41 | if ( v[j] == medie ) 42 | sol--; 43 | } 44 | } 45 | 46 | out << sol << "\n"; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Infoarena/Melodii.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Rmax = 1e5 + 2; 6 | 7 | int period[4 * Rmax]; 8 | 9 | int main() 10 | { 11 | ifstream in("melodii.in"); 12 | ofstream out("melodii.out"); 13 | 14 | int T, R, p = 0; 15 | 16 | in >> T >> R; 17 | 18 | period[ ++p ] = 1; 19 | period[ ++p ] = 1; 20 | 21 | while ( 1 ) 22 | { 23 | p++; 24 | period[p] = ( period[p - 1] + period[p - 2] ) % R; 25 | 26 | if ( period[p] == 1 && period[p - 1] == 1 ) break; 27 | } 28 | 29 | p -= 2; 30 | 31 | while ( T-- ) 32 | { 33 | long long n; 34 | in >> n; 35 | n %= p; 36 | out << period[n + 1] << "\n"; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Infoarena/Mere.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ifstream in("mere.in"); 8 | ofstream out("mere.out"); 9 | 10 | int T, N, K; 11 | 12 | in >> T; 13 | 14 | while ( T-- ) 15 | { 16 | in >> N >> K; 17 | 18 | if ( N < K ) 19 | { 20 | out << "Remiza\n"; 21 | continue; 22 | } 23 | 24 | if ( ( N / K ) & 1 || ( N % K ) ) 25 | out << "Santa Klaus\n"; 26 | else 27 | out << "Remiza\n"; 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Infoarena/Mese.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | 7 | int dp[Nmax], age[Nmax], sumDp[Nmax]; 8 | vector G[Nmax]; 9 | int v[Nmax]; 10 | 11 | int N, S; 12 | 13 | void dfs(int nod) 14 | { 15 | for (int& son: G[nod]) 16 | dfs(son); 17 | 18 | if ( G[nod].size() == 0 ) 19 | { 20 | dp[nod] = 1; 21 | sumDp[nod] = age[nod]; 22 | } 23 | else 24 | { 25 | int contor = 0; 26 | 27 | for (int& son: G[nod]) 28 | { 29 | v[ ++contor ] = sumDp[son]; 30 | dp[nod] += dp[son]; 31 | 32 | } 33 | 34 | sort(v + 1, v + contor + 1); 35 | 36 | int sum_cr = age[nod]; 37 | int i = 1; 38 | 39 | while ( i <= contor && sum_cr + v[i] <= S ) 40 | { 41 | sum_cr += v[i]; 42 | i++; 43 | } 44 | 45 | sumDp[nod] = sum_cr; 46 | dp[nod] -= (i - 2); 47 | } 48 | } 49 | 50 | int main() 51 | { 52 | ifstream in("mese.in"); 53 | ofstream out("mese.out"); 54 | 55 | ios_base::sync_with_stdio(false); 56 | 57 | in >> N >> S; 58 | 59 | for ( int i = 1, tata; i <= N; ++i ) 60 | { 61 | in >> tata >> age[i]; 62 | G[tata].push_back(i); 63 | } 64 | 65 | age[0] = 0; 66 | dfs(0); 67 | 68 | out << dp[0] << "\n"; 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Infoarena/Mins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1000000; 6 | 7 | int sieve[Nmax + 1]; 8 | bool valid[Nmax + 1]; 9 | int C, D; 10 | 11 | int main() 12 | { 13 | ifstream in("mins.in"); 14 | ofstream out("mins.out"); 15 | 16 | in >> C >> D; 17 | C--; D--; 18 | 19 | int L = min(C, D); 20 | long long sol = 1LL * C * D; 21 | 22 | for ( int i = 2; i <= L; ++i ) 23 | { 24 | if ( sieve[i] ) continue; 25 | 26 | for ( int j = i; j <= L; j += i ) 27 | sieve[j]++; 28 | 29 | if ( 1LL * i * i <= L ) 30 | for (int j = i * i; j <= L; j += i * i ) 31 | valid[j] = true; 32 | } 33 | 34 | for ( int i = 2; i <= L; ++i ) 35 | if ( valid[i] == false ) 36 | { 37 | if ( sieve[i] & 1 ) 38 | sol -= 1LL * (C / i) * (D / i); 39 | else 40 | sol += 1LL * (C / i) * (D / i); 41 | } 42 | 43 | out << sol << "\n"; 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Infoarena/Noname 3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1000000 + 1; 6 | 7 | int N, S; 8 | int V[Nmax]; 9 | 10 | int main() 11 | { 12 | freopen("noname3.in", "r", stdin); 13 | freopen("noname3.out", "w", stdout); 14 | 15 | cin >> N >> S; 16 | 17 | if ( 2 * S - N * ( N - 1 ) <= 2 * N - 2 ) 18 | { 19 | cout << "-1\n"; 20 | return 0; 21 | } 22 | 23 | for ( int i = 1; i < N; ++i ) 24 | V[i] = i; 25 | 26 | V[N] = S - N * ( N - 1 ) / 2; 27 | 28 | int k = ( V[N] - V[N - 1] ) / N; 29 | 30 | if ( ( V[N] - V[N - 1] ) % N == 0 ) 31 | k--; 32 | 33 | for ( int i = 1; i < N; ++i ) 34 | V[i] += k; 35 | 36 | V[N] -= ( N - 1 ) * k; 37 | 38 | 39 | for ( int i = N - 1; i >= 1; i-- ) 40 | { 41 | V[N]--; 42 | V[i]++; 43 | 44 | if ( !( V[N] > V[N - 1] && V[i] < V[i + 1] ) ) 45 | { 46 | V[N]++; 47 | V[i]--; 48 | break; 49 | } 50 | } 51 | 52 | for ( int i = 1; i <= N; ++i ) 53 | cout << V[i] << " "; 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Infoarena/Noname2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | 7 | int v[Nmax]; 8 | long long sum[Nmax]; 9 | 10 | int N; 11 | long long K; 12 | 13 | long long suma(int i, int j) 14 | { 15 | if ( i > j ) 16 | return 0; 17 | else 18 | return sum[j] - sum[i - 1]; 19 | } 20 | 21 | int valid(int lg) 22 | { 23 | int st = 1, dr = lg, median = (lg + 1) / 2; 24 | 25 | while ( dr <= N ) 26 | { 27 | long long s1 = suma(st, median - 1); 28 | long long s2 = suma(median + 1, dr); 29 | 30 | int dim1 = (median - 1) - st + 1; 31 | int dim2 = dr - (median + 1) + 1; 32 | 33 | long long cost = 1LL * dim1 * v[median] - s1 + s2 - 1LL * dim2 * v[median]; 34 | 35 | if ( cost <= K ) 36 | return 1; 37 | 38 | st++; 39 | dr++; 40 | median++; 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | int main() 47 | { 48 | ifstream in("noname2.in"); 49 | ofstream out("noname2.out"); 50 | 51 | in >> N >> K; 52 | 53 | for ( int i = 1; i <= N; ++i ) 54 | in >> v[i]; 55 | 56 | sort(v + 1, v + N + 1); 57 | 58 | for ( int i = 1; i <= N; ++i ) 59 | sum[i] = sum[i - 1] + v[i]; 60 | 61 | int st = 1, dr = N, gasit = 0; 62 | 63 | while (st <= dr) 64 | { 65 | int m = (st + dr) / 2; 66 | 67 | if ( valid(m) ) 68 | { 69 | gasit = m; 70 | st = m + 1; 71 | } 72 | else 73 | dr = m - 1; 74 | } 75 | 76 | out << gasit << "\n"; 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Infoarena/Numar3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | char *str; 6 | int N; 7 | 8 | int main() 9 | { 10 | ifstream in("numar3.in"); 11 | ofstream out("numar3.out"); 12 | 13 | in >> N; 14 | 15 | char last; 16 | int highInversionPos = 0; 17 | char ch; 18 | 19 | in >> last; 20 | 21 | for ( int i = 2; i <= N; ++i ) 22 | { 23 | in >> ch; 24 | 25 | if ( ch > last ) 26 | highInversionPos = i - 1; 27 | 28 | last = ch; 29 | } 30 | 31 | in.close(); 32 | in.open("numar3.in"); 33 | 34 | in >> N; 35 | int dim = 0; 36 | 37 | str = new char[N - highInversionPos + 2]; 38 | 39 | for ( int i = 1; i < highInversionPos; ++i ) 40 | { 41 | in >> ch; 42 | out << ch; 43 | } 44 | 45 | for ( int i = highInversionPos; i <= N; ++i ) 46 | { 47 | in >> ch; 48 | str[dim++] = ch; 49 | } 50 | 51 | next_permutation(str, str + dim); 52 | 53 | str[dim] = '\0'; 54 | out << str; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Infoarena/Pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Vmax = 1000000 + 1; 6 | 7 | int sieve[Vmax]; 8 | bool use[Vmax]; 9 | bool prime[Vmax]; 10 | bool valid[Vmax]; 11 | 12 | int N, M; 13 | 14 | int main() 15 | { 16 | ifstream in("pairs.in"); 17 | ofstream out("pairs.out"); 18 | 19 | in >> N; 20 | 21 | for ( int i = 1; i <= N; ++i ) 22 | { 23 | int a; 24 | in >> a; 25 | use[a] = 1; 26 | M = max(M, a); 27 | } 28 | 29 | long long sol = 0; 30 | 31 | for ( int i = 2; i <= M; ++i ) 32 | { 33 | if ( prime[i] == false ) 34 | { 35 | for ( int j = i; j <= M; j += i ) 36 | { 37 | sieve[j]++; 38 | prime[j] = true; 39 | 40 | if ( j % (i * i) == 0 ) 41 | valid[j] = true; 42 | } 43 | } 44 | } 45 | 46 | for ( int i = 2; i <= M; ++i ) 47 | { 48 | if ( valid[i] == false ) 49 | { 50 | int nr = 0; 51 | 52 | for ( int j = i; j <= M; j += i ) 53 | nr += use[j]; 54 | 55 | if ( sieve[i] & 1 ) 56 | sol += 1LL * nr * (nr - 1) / 2; 57 | else 58 | sol -= 1LL * nr * (nr - 1) / 2; 59 | } 60 | } 61 | 62 | out << 1LL * N * (N - 1) / 2 - sol << "\n"; 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Infoarena/Partitie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 300000 + 1; 6 | 7 | struct Node 8 | { 9 | int val; 10 | int mult; 11 | 12 | bool operator < (const Node& A) const 13 | { 14 | return val > A.val; 15 | } 16 | }; 17 | 18 | pair v[Nmax]; 19 | priority_queue MinHeap; 20 | int sol[Nmax]; 21 | int N, D, K; 22 | 23 | int main() 24 | { 25 | ifstream in("partitie.in"); 26 | ofstream out("partitie.out"); 27 | 28 | in >> N >> D; 29 | 30 | for (int i = 1; i <= N; ++i) 31 | { 32 | in >> v[i].first; 33 | v[i].second = i; 34 | } 35 | 36 | sort(v + 1, v + N + 1); 37 | 38 | for (int i = 1; i <= N; ++i) 39 | { 40 | if (MinHeap.size() && v[i].first - MinHeap.top().val >= D) 41 | { 42 | Node a = MinHeap.top(); 43 | MinHeap.pop(); 44 | 45 | sol[ v[i].second ] = a.mult; 46 | MinHeap.push({v[i].first, a.mult}); 47 | } 48 | else 49 | { 50 | MinHeap.push({v[i].first, ++K}); 51 | sol[ v[i].second ] = K; 52 | } 53 | } 54 | 55 | out << K << "\n"; 56 | 57 | for (int i = 1; i <= N; ++i) 58 | out << sol[i] << "\n"; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Infoarena/Perioada2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int Lmax = 1e7 + 2; 9 | 10 | char S[Lmax]; 11 | int pi[Lmax]; 12 | int N; 13 | 14 | void build_prefix() 15 | { 16 | int lgprefix = 0; 17 | 18 | pi[1] = 0; 19 | 20 | for ( int i = 2; i <= N; ++i ) 21 | { 22 | while ( lgprefix > 0 && S[i] != S[ lgprefix + 1 ] ) 23 | lgprefix = pi[ lgprefix ]; 24 | 25 | if ( S[i] == S[ lgprefix + 1 ] ) 26 | lgprefix++; 27 | 28 | pi[i] = lgprefix; 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | ifstream in("perioada2.in"); 35 | ofstream out("perioada2.out"); 36 | 37 | in >> N; 38 | in >> ( S + 1 ); 39 | 40 | build_prefix(); 41 | 42 | int lgprefix = pi[N]; 43 | int sol = 0; 44 | 45 | while ( lgprefix ) 46 | { 47 | if ( N % ( N - lgprefix ) == 0 ) 48 | sol++; 49 | 50 | lgprefix = pi[lgprefix]; 51 | 52 | } 53 | 54 | out << sol << "\n"; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Infoarena/Planificare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | 7 | struct Interval 8 | { 9 | int x, y; 10 | 11 | bool operator < (const Interval& I) const 12 | { 13 | return y < I.y; 14 | } 15 | }; 16 | 17 | Interval v[Nmax]; 18 | multiset MS; 19 | int N, K, sol; 20 | 21 | int main() 22 | { 23 | ifstream in("planificare.in"); 24 | ofstream out("planificare.out"); 25 | 26 | in >> N >> K; 27 | 28 | for ( int i = 1; i <= N; ++i ) 29 | in >> v[i].x >> v[i].y; 30 | 31 | sort(v + 1, v + N + 1); 32 | 33 | for ( int i = 1; i <= K; ++i ) 34 | MS.insert(0); 35 | 36 | for ( int i = 1; i <= N; ++i ) 37 | { 38 | auto it = MS.lower_bound(v[i].x); 39 | 40 | if ( it != MS.end() && *it == v[i].x ) 41 | { 42 | sol++; 43 | MS.erase(it); 44 | MS.insert(v[i].y); 45 | continue; 46 | } 47 | 48 | if ( it == MS.begin() ) 49 | continue; 50 | 51 | it--; 52 | sol++; 53 | MS.erase(it); 54 | MS.insert(v[i].y); 55 | } 56 | 57 | out << sol << "\n"; 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Infoarena/Popandai2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define Point pair 6 | #define x first 7 | #define y second 8 | 9 | const int Nmax = 1000; 10 | 11 | Point P[2 * Nmax]; 12 | int N; 13 | 14 | int CCW(Point A, Point B, Point C) 15 | { 16 | return (C.x - A.x) * (B.y - A.y) - (C.y - A.y) * (B.x - A.x); 17 | } 18 | 19 | int area(int i, int j, int k) 20 | { 21 | return abs(CCW(P[i], P[j], P[k])); 22 | } 23 | 24 | int main() 25 | { 26 | ifstream in("popandai2.in"); 27 | ofstream out("popandai2.out"); 28 | 29 | in >> N; 30 | 31 | for (int i = 0; i < N; ++i) 32 | { 33 | in >> P[i].x >> P[i].y; 34 | P[N + i] = P[i]; 35 | } 36 | 37 | int best = 0; 38 | 39 | for (int i = 0; i < N; ++i) 40 | { 41 | int p = i + 1; 42 | int q = i + 3; 43 | 44 | for (int j = i + 2; j < N; ++j) 45 | { 46 | while (p < j && area(i, j, p + 1) >= area(i, j, p)) 47 | p++; 48 | 49 | while (q < i + N && area(i, j, q + 1) >= area(i, j, q)) 50 | q++; 51 | 52 | best = max(best, area(i, j, p) + area(i, j, q)); 53 | } 54 | } 55 | 56 | out << fixed << setprecision(1) << (best / 2.0) << "\n"; 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Infoarena/Por Costel si Azerah.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MOD = 1e9 + 7; 6 | 7 | int pw(int a, int p) 8 | { 9 | int sol = 1; 10 | 11 | while ( p ) 12 | { 13 | if ( p & 1 ) 14 | sol = (1LL * sol * a) % MOD; 15 | 16 | a = (1LL * a * a) % MOD; 17 | p >>= 1; 18 | } 19 | 20 | return sol; 21 | } 22 | 23 | int main() 24 | { 25 | ifstream in("azerah.in"); 26 | ofstream out("azerah.out"); 27 | 28 | int T, N; 29 | in >> T; 30 | 31 | while ( T-- ) 32 | { 33 | in >> N; 34 | 35 | int nrPare = 0, nrImpare = 0; 36 | 37 | while ( N-- ) 38 | { 39 | int a; 40 | in >> a; 41 | 42 | if ( a % 2 ) 43 | nrImpare++; 44 | else 45 | nrPare++; 46 | } 47 | 48 | int sol = pw(2, nrPare); 49 | 50 | sol = (1LL * sol * pw(2, nrImpare)) % MOD; 51 | 52 | if ( nrImpare ) 53 | sol = (1LL * sol * pw(2, MOD - 2)) % MOD; 54 | 55 | sol = (sol - 1 + MOD) % MOD; 56 | 57 | out << sol << "\n"; 58 | } 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Infoarena/Por Costel si Perechile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long solve(int N) 6 | { 7 | if ( N == 1 ) 8 | return 1; 9 | 10 | long long sum = 0; 11 | 12 | for ( int i = 1; i * i <= N; ++i ) 13 | sum += N / i; 14 | 15 | int x = 1; 16 | 17 | while ( x * x <= N ) x++; 18 | x--; 19 | 20 | return 2LL * sum - x * x; 21 | } 22 | 23 | int main() 24 | { 25 | ifstream in("perechi3.in"); 26 | ofstream out("perechi3.out"); 27 | 28 | ios_base::sync_with_stdio(false); 29 | 30 | int T, N; 31 | 32 | in >> T; 33 | 34 | while (T--) 35 | { 36 | in >> N; 37 | out << solve(N) << "\n"; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Infoarena/Por Costel si Pocnitoarea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int LIMIT = 1000; 6 | const int MOD = 1e7 + 3; 7 | 8 | int X[MOD / LIMIT + 1]; 9 | 10 | int N, A, B; 11 | int X1; 12 | int Q, Q1; 13 | 14 | void preprocess() 15 | { 16 | X[0] = X1; 17 | int solution = X1; 18 | 19 | for ( int i = 1; i < MOD; ++i ) 20 | { 21 | solution = (1LL * i * solution + A) % N; 22 | 23 | if ( i % LIMIT == 0 ) 24 | X[i / LIMIT] = solution; 25 | } 26 | } 27 | 28 | int query(int timp) 29 | { 30 | if( timp % LIMIT == 1 ) 31 | return X[timp / LIMIT] ; 32 | 33 | int where = timp / LIMIT; 34 | 35 | if ( timp % LIMIT == 0 ) 36 | where--; 37 | 38 | int solutie = X[where]; 39 | 40 | for(int i = where * LIMIT + 1; i < timp; ++i) 41 | solutie = (1LL * i * solutie + A) % N ; 42 | 43 | return solutie ; 44 | } 45 | 46 | int main() 47 | { 48 | ifstream in("pocnitoare.in"); 49 | ofstream out("pocnitoare.out"); 50 | 51 | in >> N >> A >> B >> X1 >> Q >> Q1; 52 | 53 | preprocess(); 54 | 55 | for ( int i = 1; i <= Q; ++i ) 56 | { 57 | int s = query(Q1); 58 | Q1 = (1LL * i * s + B) % MOD + 1; 59 | out << s << "\n"; 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Infoarena/Por Costel si Semipalindroamele.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 64; 6 | 7 | int T, N; 8 | long long K; 9 | char str[Nmax]; 10 | 11 | int main() 12 | { 13 | ifstream in("semipal.in"); 14 | ofstream out("semipal.out"); 15 | 16 | in >> T; 17 | 18 | while ( T-- ) 19 | { 20 | in >> N >> K; 21 | 22 | if ( K <= (1LL << (N - 2)) ) 23 | { 24 | str[1] = 'a'; 25 | str[N] = 'a'; 26 | } 27 | else 28 | { 29 | K -= (1LL << (N - 2)); 30 | str[1] = 'b'; 31 | str[N] = 'b'; 32 | } 33 | 34 | for ( int i = 2; i < N; ++i ) 35 | { 36 | if ( K <= (1LL << (N - i - 1)) ) 37 | { 38 | str[i] = 'a'; 39 | } 40 | else 41 | { 42 | K -= (1LL << (N - i - 1)); 43 | str[i] = 'b'; 44 | } 45 | } 46 | 47 | for ( int i = 1; i <= N; ++i ) 48 | out << str[i]; 49 | 50 | out << "\n"; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Infoarena/Pp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | unordered_map UM; 6 | int N, K; 7 | 8 | int solve( int nr ) 9 | { 10 | if ( UM[nr] != 0 ) 11 | return UM[nr]; 12 | 13 | if ( nr == 0 ) 14 | return UM[nr] = 2; 15 | 16 | int sol = 2; 17 | 18 | for ( int i = 2; i <= K && sol == 2; ++i ) 19 | { 20 | if ( solve( nr / i ) == 2 ) 21 | sol = 1; 22 | } 23 | 24 | return UM[nr] = sol; 25 | } 26 | 27 | int main() 28 | { 29 | ifstream in("pp.in"); 30 | ofstream out("pp.out"); 31 | 32 | in >> N >> K; 33 | out << 2 - solve( N ) << "\n"; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Infoarena/Prefixe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector prefix( const string &str ) 6 | { 7 | string a = " " + str; 8 | 9 | int N = a.size() - 1; 10 | int k = 0; 11 | 12 | vector pi( N + 1, 0 ); 13 | 14 | for ( int i = 2; i <= N; ++i ) 15 | { 16 | while ( k && a[k + 1] != a[i] ) k = pi[k]; 17 | 18 | if ( a[k + 1] == a[i] ) 19 | k++; 20 | 21 | pi[i] = k; 22 | } 23 | 24 | return pi; 25 | } 26 | 27 | int main() 28 | { 29 | ifstream in("prefixe.in"); 30 | ofstream out("prefixe.out"); 31 | 32 | int Z; 33 | string str; 34 | 35 | in >> Z; 36 | 37 | while ( Z-- ) 38 | { 39 | in >> str; 40 | 41 | vector pi = prefix( str ); 42 | 43 | for ( int i = 1; i <= (int)str.size(); ++i ) 44 | out << pi[i] << " "; 45 | 46 | out << "\n"; 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Infoarena/Reguli.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | /// cea mai scurta perioada pe sirul diferentelor dintre 2 termeni consecutivi 8 | 9 | const int Lmax = 5e5 + 2; 10 | 11 | long long Text[Lmax]; 12 | int pi[Lmax]; 13 | int N; 14 | 15 | void build_prefix() 16 | { 17 | int lgprefix = 0; 18 | 19 | pi[1] = 0; 20 | 21 | for ( int i = 2; i <= N; ++i ) 22 | { 23 | while ( lgprefix > 0 && Text[i] != Text[ lgprefix + 1 ] ) 24 | lgprefix = pi[ lgprefix ]; 25 | 26 | if ( Text[i] == Text[ lgprefix + 1 ] ) 27 | lgprefix++; 28 | 29 | pi[i] = lgprefix; 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | ifstream in("reguli.in"); 36 | ofstream out("reguli.out"); 37 | 38 | in >> N; 39 | 40 | for ( long long i = 1, last = 0, x; i <= 1LL * N; ++i ) 41 | { 42 | in >> x; 43 | 44 | if ( i != 1 ) Text[i - 1] = x - last; 45 | last = x; 46 | } 47 | 48 | N--; 49 | 50 | build_prefix(); 51 | 52 | out << N - pi[N] << "\n"; 53 | 54 | for ( int i = 1; i <= N - pi[N]; ++i ) 55 | out << Text[i] << "\n"; 56 | 57 | out << "\n"; 58 | 59 | in.close(); 60 | out.close(); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Infoarena/Rubarba.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define INF 1e9 6 | #define EPS 1e-14 7 | #define MAX_N 100000 8 | 9 | #define Point pair 10 | #define x first 11 | #define y second 12 | 13 | Point P[MAX_N]; 14 | int N; 15 | 16 | double area(double phi) 17 | { 18 | double minX = INF, minY = INF; 19 | double maxX = -INF, maxY = -INF; 20 | 21 | double cs = cos(phi); 22 | double sn = sin(phi); 23 | 24 | for (int i = 0; i < N; ++i) 25 | { 26 | double X = (long double) P[i].x * cs - P[i].y * sn; 27 | double Y = (long double) P[i].x * sn + P[i].y * cs; 28 | 29 | minX = min(minX, X); 30 | maxX = max(maxX, X); 31 | 32 | minY = min(minY, Y); 33 | maxY = max(maxY, Y); 34 | } 35 | 36 | return (maxX - minX) * (maxY - minY); 37 | } 38 | 39 | double ternary_search(double l, double r) 40 | { 41 | if (r - l < EPS) 42 | return l; 43 | 44 | double m1 = l + (r - l) / 3.0; 45 | double m2 = r - (r - l) / 3.0; 46 | 47 | if (area(m1) < area(m2)) 48 | return ternary_search(l, m2); 49 | else 50 | return ternary_search(m1, r); 51 | } 52 | 53 | int main() 54 | { 55 | ifstream in("rubarba.in"); 56 | ofstream out("rubarba.out"); 57 | 58 | in >> N; 59 | 60 | for (int i = 0; i < N; ++i) 61 | in >> P[i].x >> P[i].y; 62 | 63 | out << fixed << setprecision(2) << area(ternary_search(0, M_PI_2)); 64 | out << "\n"; 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Infoarena/Secvmin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | const int Vmax = 1000000 + 1; 7 | 8 | int A[Nmax]; 9 | int B[Nmax]; 10 | int P[Vmax]; 11 | pair posDim[Nmax]; 12 | int N, M; 13 | 14 | int main() 15 | { 16 | ifstream in("secvmin.in"); 17 | ofstream out("secvmin.out"); 18 | 19 | in >> N >> M; 20 | 21 | for ( int i = 1; i <= N; ++i ) 22 | in >> A[i]; 23 | 24 | for ( int j = 1; j <= M; ++j ) 25 | { 26 | in >> B[j]; 27 | P[ B[j] ] = j; 28 | } 29 | 30 | int best = 1e9; 31 | 32 | for ( int i = 1; i <= N; ++i ) 33 | { 34 | if ( P[ A[i] ] != 0 ) /// apare in B 35 | { 36 | int d = P[ A[i] ]; 37 | 38 | if ( d == 1 ) 39 | posDim[1] = pair( i, i ); 40 | else 41 | { 42 | if ( posDim[d - 1].first != 0 ) 43 | { 44 | posDim[d] = pair( posDim[d - 1].first, i ); 45 | 46 | if ( d == M ) 47 | best = min( best, i - posDim[d].first + 1 ); 48 | } 49 | } 50 | } 51 | } 52 | 53 | if ( best > N ) 54 | out << "-1"; 55 | else 56 | out << best; 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Infoarena/Spion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MOD = 100003; 6 | 7 | int pw( int a, int p = MOD - 2 ) 8 | { 9 | int res = 1; 10 | 11 | for ( int i = 0; ( 1 << i ) <= p; ++i ) 12 | { 13 | if ( p & ( 1 << i ) ) 14 | res = ( 1LL * res * a ) % MOD; 15 | 16 | a = ( 1LL * a * a ) % MOD; 17 | } 18 | 19 | return res; 20 | } 21 | 22 | int fact( int n ) 23 | { 24 | int res = 1; 25 | 26 | for ( int i = 1; i <= n; ++i ) 27 | res = ( 1LL * res * i ) % MOD; 28 | 29 | return res; 30 | } 31 | 32 | int p; 33 | string path; 34 | 35 | int main() 36 | { 37 | ifstream in("spion.in"); 38 | ofstream out("spion.out"); 39 | 40 | in >> p >> path; 41 | 42 | if ( p == 1 ) 43 | { 44 | out << count( path.begin(), path.end(), 'E' ) + 1 << "\n"; 45 | } 46 | else 47 | { 48 | int k = count( path.begin(), path.end(), 'V' ); 49 | int n = path.size(); 50 | 51 | out << ( ( 1LL * fact( n ) * pw( fact( k ) ) ) % MOD ) * pw( fact( n - k ) ) % MOD << "\n"; 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Infoarena/Subset2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ifstream in("subset2.in"); 8 | ofstream out("subset2.out"); 9 | 10 | int N, K; 11 | 12 | in >> N >> K; 13 | 14 | if ( K & 1 ) 15 | { 16 | out << 1 + min( K / 2, N % K ) * ( N / K + 1 ) + ( K / 2 - min( K / 2, N % K ) ) * ( N / K ); 17 | } 18 | else 19 | { 20 | out << 2 + min( K / 2 - 1, N % K ) * ( N / K + 1 ) + ( K / 2 - 1 - min( K / 2 - 1, N % K ) ) * ( N / K ); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Infoarena/Sume2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 50000 + 1; 6 | 7 | int A[Nmax]; 8 | int N; 9 | long long K; 10 | 11 | long long nrSume(int S) 12 | { 13 | long long sol = 0; 14 | 15 | int j = N; 16 | 17 | for (int i = 1; i <= N; ++i) 18 | { 19 | while (j >= i && A[i] + A[j] > S) j--; 20 | 21 | if (i <= j) 22 | { 23 | sol += j - i + 1; 24 | sol += j - i; 25 | } 26 | } 27 | 28 | return sol; 29 | } 30 | 31 | int main() 32 | { 33 | ifstream in("sume2.in"); 34 | ofstream out("sume2.out"); 35 | 36 | in >> N >> K; 37 | 38 | for (int i = 1; i <= N; ++i) 39 | in >> A[i]; 40 | 41 | sort(A + 1, A + N + 1); 42 | 43 | int st = 0, dr = (1 << 21), gasit = -1; 44 | 45 | while (st <= dr) 46 | { 47 | int m = (st + dr) / 2; 48 | 49 | if (nrSume(m) >= K) 50 | { 51 | gasit = m; 52 | dr = m - 1; 53 | } 54 | else 55 | st = m + 1; 56 | } 57 | 58 | assert(gasit != -1); 59 | 60 | out << gasit << "\n"; 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Infoarena/Tribute.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MAX_C = 50000; 6 | 7 | int visX[MAX_C + 1]; 8 | int visY[MAX_C + 1]; 9 | 10 | int DX, DY; 11 | int N; 12 | 13 | int solve(int vis[], int delta) 14 | { 15 | int st = 0, dr = delta; 16 | int sol = 0; 17 | 18 | for (int i = 0; i <= MAX_C; ++i) 19 | { 20 | if (vis[i] && (i < st || dr < i)) 21 | sol += vis[i] * min(abs(i - st), abs(i - dr)); 22 | } 23 | 24 | int nrStanga = 0; 25 | int nrDreapta = 0; 26 | int sumStanga = 0; 27 | int sumDreapta = 0; 28 | 29 | for (int i = dr + 1; i <= MAX_C; ++i) 30 | { 31 | nrDreapta += vis[i]; 32 | sumDreapta += vis[i] * abs(i - dr); 33 | } 34 | 35 | while (dr + 1 <= MAX_C) 36 | { 37 | sumStanga += nrStanga * 1; 38 | sumStanga += vis[st]; 39 | nrStanga += vis[st]; 40 | st++; 41 | 42 | sumDreapta -= nrDreapta; 43 | nrDreapta -= vis[dr + 1]; 44 | dr++; 45 | 46 | sol = min(sol, sumStanga + sumDreapta); 47 | } 48 | 49 | return sol; 50 | } 51 | 52 | int main() 53 | { 54 | ifstream in("tribute.in"); 55 | ofstream out("tribute.out"); 56 | 57 | in >> N >> DX >> DY; 58 | 59 | for (int i = 1; i <= N; ++i) 60 | { 61 | int x, y; 62 | in >> x >> y; 63 | visX[x]++; 64 | visY[y]++; 65 | } 66 | 67 | out << solve(visX, DX) + solve(visY, DY) << "\n"; 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Java/BinaryIndexedTree.java: -------------------------------------------------------------------------------- 1 | class BinaryIndexedTree 2 | { 3 | private final int[] tree; 4 | private final int N; 5 | 6 | BinaryIndexedTree(int N){ 7 | this.tree = new int[N + 1]; 8 | this.N = N; 9 | } 10 | 11 | private int lsb(int x){ 12 | return x & (-x); 13 | } 14 | 15 | void update(int position, int key){ 16 | while (position <= N){ 17 | tree[position] += key; 18 | position += lsb(position); 19 | } 20 | } 21 | 22 | int query(int position){ 23 | int sum = 0; 24 | 25 | while (position >= 1){ 26 | sum += tree[position]; 27 | position -= lsb(position); 28 | } 29 | 30 | return sum; 31 | } 32 | 33 | int query(int x, int y){ 34 | return query(y) - query(x - 1); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Java/CountingSort.java: -------------------------------------------------------------------------------- 1 | class CountingSort { 2 | static void sort(int[] array, int radix){ 3 | final int N = array.length; 4 | 5 | int[] aux = new int[N]; 6 | int[] count = new int[radix + 1]; 7 | 8 | for (int i = 0; i < N; i++) 9 | count[array[i] + 1]++; 10 | 11 | for (int i = 0; i < radix; i++) 12 | count[i + 1] += count[i]; 13 | 14 | for (int i = 0; i < N; i++) 15 | aux[count[array[i]]++] = array[i]; 16 | 17 | for (int i = 0; i < N; i++) 18 | array[i] = aux[i]; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Java/InputReader.java: -------------------------------------------------------------------------------- 1 | class InputReader implements AutoCloseable{ 2 | private BufferedReader reader; 3 | private StringTokenizer tokenizer; 4 | private InputStream inputStream; 5 | 6 | InputReader(InputStream stream) { 7 | reader = new BufferedReader(new InputStreamReader(stream), 1 << 20); 8 | tokenizer = null; 9 | inputStream = stream; 10 | } 11 | 12 | private String nextToken() { 13 | while (tokenizer == null || !tokenizer.hasMoreTokens()){ 14 | try { 15 | tokenizer = new StringTokenizer(reader.readLine()); 16 | } 17 | catch (IOException e){ 18 | throw new RuntimeException(e); 19 | } 20 | } 21 | 22 | return tokenizer.nextToken(); 23 | } 24 | 25 | public int nextInt() { 26 | return Integer.parseInt(nextToken()); 27 | } 28 | 29 | public String nextString(){ 30 | return nextToken(); 31 | } 32 | 33 | public void close() { 34 | try { 35 | inputStream.close(); 36 | } catch (IOException e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Java/LSD_RadixSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | class LSD_RadixSort { 4 | static void sort(String[] array, int W){ 5 | final int N = array.length; 6 | final int RADIX = 128; 7 | 8 | String[] aux = new String[N]; 9 | int[] count = new int[RADIX + 1]; 10 | 11 | for (int d = W - 1; d >= 0; d--) { 12 | 13 | for (int i = 0; i < N; i++) 14 | count[array[i].charAt(d) + 1]++; 15 | 16 | for (int i = 0; i < RADIX; ++i) 17 | count[i + 1] += count[i]; 18 | 19 | for (int i = 0; i < N; ++i) 20 | aux[count[array[i].charAt(d)]++] = array[i]; 21 | 22 | for (int i = 0; i < N; ++i) 23 | array[i] = aux[i]; 24 | 25 | Arrays.fill(count, 0); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Java/MultiHashTable.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class MultiHashTable { 5 | private final Map map = new HashMap<>(); 6 | 7 | public void updateValue(int key, int value){ 8 | Integer c = map.get(key); 9 | 10 | if (c == null) 11 | map.put(key, value); 12 | else 13 | map.put(key, c + value); 14 | } 15 | 16 | public int getValue(int key){ 17 | Integer c = map.get(key); 18 | 19 | if (c == null) 20 | return 0; 21 | else 22 | return c; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Java/RunningMedian.java: -------------------------------------------------------------------------------- 1 | import java.util.Comparator; 2 | import java.util.PriorityQueue; 3 | 4 | public class RunningMedian{ 5 | private final PriorityQueue minHeap = new PriorityQueue<>(Comparator.naturalOrder()); 6 | private final PriorityQueue maxHeap = new PriorityQueue<>(Comparator.reverseOrder()); 7 | 8 | public void addElement(int e){ 9 | if (maxHeap.isEmpty() || e < maxHeap.peek()) 10 | maxHeap.add(e); 11 | else 12 | minHeap.add(e); 13 | 14 | while (maxHeap.size() > minHeap.size() + 1){ 15 | minHeap.add(maxHeap.remove()); 16 | } 17 | 18 | while (minHeap.size() > maxHeap.size() + 1){ 19 | maxHeap.add(minHeap.remove()); 20 | } 21 | } 22 | 23 | public double median(){ 24 | if (minHeap.isEmpty() && maxHeap.isEmpty()) 25 | return 0; 26 | else if (minHeap.size() == maxHeap.size()) 27 | return ((double)minHeap.peek() + maxHeap.peek()) / 2; 28 | else if (minHeap.size() > maxHeap.size()) 29 | return minHeap.peek(); 30 | else 31 | return maxHeap.peek(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Java/SparseTable.java: -------------------------------------------------------------------------------- 1 | import java.util.function.IntBinaryOperator; 2 | 3 | class SparseTable { 4 | private IntBinaryOperator binaryOperator; 5 | private int N; 6 | private int[][] rmq; 7 | private int[] log2; 8 | 9 | SparseTable(int N, IntBinaryOperator operator){ 10 | this.binaryOperator = operator; 11 | this.N = N; 12 | this.rmq = new int[32 - Integer.numberOfLeadingZeros(N)][N + 1]; 13 | this.log2 = new int[N + 1]; 14 | } 15 | 16 | void set(int index, int key){ 17 | rmq[0][index] = key; 18 | } 19 | 20 | void build(){ 21 | log2[1] = 0; 22 | 23 | for (int i = 2; i <= N; i++) 24 | log2[i] = log2[i >> 1] + 1; 25 | 26 | for (int i = 1; (1 << i) <= N; ++i) 27 | for (int j = 1; j + (1 << i) - 1 <= N; ++j) 28 | rmq[i][j] = binaryOperator.applyAsInt(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]); 29 | } 30 | 31 | int query(int x, int y){ 32 | int k = log2[y - x + 1]; 33 | return binaryOperator.applyAsInt(rmq[k][x], rmq[k][y - (1 << k) + 1]); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Alexandru Văleanu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LSD_RadixSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | class LSD_RadixSort { 4 | static void sort(String[] array, int W){ 5 | final int N = array.length; 6 | final int RADIX = 128; 7 | 8 | String[] aux = new String[N]; 9 | int[] count = new int[RADIX + 1]; 10 | 11 | for (int d = W - 1; d >= 0; d--) { 12 | 13 | for (int i = 0; i < N; i++) 14 | count[array[i].charAt(d) + 1]++; 15 | 16 | for (int i = 0; i < RADIX; ++i) 17 | count[i + 1] += count[i]; 18 | 19 | for (int i = 0; i < N; ++i) 20 | aux[count[array[i].charAt(d)]++] = array[i]; 21 | 22 | for (int i = 0; i < N; ++i) 23 | array[i] = aux[i]; 24 | 25 | Arrays.fill(count, 0); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Mathematics-Algorithms/Determinant of a matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100 + 1; 6 | const double EPS = 1e-10; 7 | 8 | double A[Nmax][Nmax]; 9 | int N; 10 | 11 | double gaussian() 12 | { 13 | int i = 1, j = 1; 14 | double det = 1; 15 | 16 | while (i <= N && j <= N) 17 | { 18 | int x = 0; 19 | 20 | for ( int k = i; k <= N; ++k ) 21 | if ( A[k][j] > EPS || A[k][j] < -EPS ) 22 | { 23 | x = k; 24 | break; 25 | } 26 | 27 | if ( !x ) 28 | { 29 | j++; 30 | continue; 31 | } 32 | 33 | swap(A[i], A[x]); 34 | 35 | det *= A[i][j]; 36 | 37 | for ( int k = j + 1; k <= N; ++k ) 38 | A[i][k] /= A[i][j]; 39 | 40 | A[i][j] = 1; 41 | 42 | for ( int l = i + 1; l <= N; ++l ) 43 | { 44 | for ( int c = j + 1; c <= N; ++c ) 45 | A[l][c] -= A[i][c] * A[l][j]; 46 | 47 | A[l][j] = 0; 48 | } 49 | 50 | i++; j++; 51 | } 52 | 53 | return det; 54 | } 55 | 56 | int main() 57 | { 58 | cin >> N; 59 | 60 | for ( int i = 1; i <= N; ++i ) 61 | for ( int j = 1; j <= N; ++j ) 62 | in >> A[i][j]; 63 | 64 | cout << gaussian() << "\n"; 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Mathematics-Algorithms/Extended Euclidean algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int euclid(int a, int b, int &x, int &y) 6 | { 7 | if (b == 0) 8 | { 9 | x = 1; 10 | y = 0; 11 | return a; 12 | } 13 | else 14 | { 15 | int x0, y0; 16 | int d = euclid(b, a % b, x0, y0); 17 | 18 | x = y0; 19 | y = x0 - (a / b) * y0; 20 | 21 | return d; 22 | } 23 | } 24 | 25 | int gcd(int a, int b) 26 | { 27 | int x, y; 28 | return euclid(a, b, x, y); 29 | } 30 | 31 | int modularInverse(int a, int p) 32 | { 33 | int x, y; 34 | euclid(a, p, x, y); 35 | 36 | if (x < 0) 37 | x = x + p; 38 | 39 | return x; 40 | } 41 | 42 | int main() 43 | { 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Mathematics-Algorithms/Lucas' theorem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /** 6 | preprocess 7 | query 8 | **/ 9 | 10 | const int MOD_MAX = 1e6 + 10; 11 | 12 | int factorial[MOD_MAX]; 13 | int inverseFactorial[MOD_MAX]; 14 | 15 | int MOD; /// is prime 16 | 17 | int power(int a, int p) 18 | { 19 | int sol = 1; 20 | 21 | while (p) 22 | { 23 | if (p & 1) 24 | sol = (1LL * a * sol) % MOD; 25 | 26 | a = (1LL * a * a) % MOD; 27 | p >>= 1; 28 | } 29 | 30 | return sol; 31 | } 32 | 33 | void preprocess() 34 | { 35 | factorial[0] = 1; 36 | 37 | for (int i = 1; i < MOD; ++i) 38 | factorial[i] = (1LL * i * factorial[i - 1]) % MOD; 39 | 40 | inverseFactorial[MOD - 1] = power(factorial[MOD - 1], MOD - 2); 41 | 42 | for (int i = MOD - 2; i >= 0; i--) 43 | inverseFactorial[i] = (1LL * inverseFactorial[i + 1] * (i + 1)) % MOD; 44 | } 45 | 46 | /// n, k <= MOD 47 | int auxComb(int n, int k) 48 | { 49 | if (n < k) 50 | return 0; 51 | 52 | int sol = factorial[n]; 53 | sol = (1LL * sol * inverseFactorial[k]) % MOD; 54 | sol = (1LL * sol * inverseFactorial[n - k]) % MOD; 55 | 56 | return sol; 57 | } 58 | 59 | int C(long long n, long long k) 60 | { 61 | int sol = 1; 62 | 63 | while (n && k && sol) 64 | { 65 | sol = (1LL * sol * auxComb(n % MOD, k % MOD)) % MOD; 66 | 67 | n /= MOD; 68 | k /= MOD; 69 | } 70 | 71 | return sol; 72 | } 73 | 74 | int main() 75 | { 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Mathematics-Algorithms/Rank of a matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100; 6 | const double EPS = 1e-10; 7 | 8 | double A[Nmax][Nmax]; 9 | int N, M; 10 | 11 | void gaussian() 12 | { 13 | int i = 1, j = 1; 14 | 15 | while (i <= N && j <= M) 16 | { 17 | int x = 0; 18 | 19 | for (int k = i; k <= N; ++k) 20 | if ( A[k][j] > EPS || A[k][j] < -EPS ) 21 | { 22 | x = k; 23 | break; 24 | } 25 | 26 | if (!x) 27 | { 28 | j++; 29 | continue; 30 | } 31 | 32 | swap(A[i], A[x]); 33 | 34 | for (int k = j + 1; k <= M; ++k) 35 | A[i][k] /= A[i][j]; 36 | 37 | A[i][j] = 1; 38 | 39 | for (int l = i + 1; l <= N; ++l) 40 | { 41 | for (int c = j + 1; c <= M; ++c) 42 | A[l][c] -= A[i][c] * A[l][j]; 43 | 44 | A[l][j] = 0; 45 | } 46 | 47 | i++; j++; 48 | } 49 | } 50 | 51 | int Rank() 52 | { 53 | for ( int i = N; i >= 1; i-- ) 54 | if (A[i][i] > EPS || A[i][i] < -EPS) 55 | return i; 56 | 57 | return 0; 58 | } 59 | 60 | int main() 61 | { 62 | ifstream in("data.in"); 63 | 64 | in >> N >> M; 65 | 66 | for ( int i = 1; i <= N; ++i ) 67 | for ( int j = 1; j <= M; ++j ) 68 | in >> A[i][j]; 69 | 70 | gaussian(); 71 | 72 | cout << Rank() << "\n"; 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Number-Theory/Binary GCD algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int gcd(int a, int b) 6 | { 7 | if (a == b) 8 | return a; 9 | 10 | if (a == 0) 11 | return b; 12 | 13 | if (b == 0) 14 | return a; 15 | 16 | if (~a & 1) /// a is even 17 | { 18 | if (b & 1) ///b is odd 19 | return gcd(a >> 1, b); 20 | else 21 | return gcd(a >> 1, b >> 1) << 1; 22 | } 23 | else ///a is odd 24 | { 25 | if (~b & 1) ///b is even 26 | return gcd(a, b >> 1); 27 | } 28 | 29 | if (a > b) 30 | return gcd((a - b) >> 1, b); 31 | else 32 | return gcd((b - a) >> 1, a); 33 | } 34 | 35 | int main() 36 | { 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Number-Theory/Linear sieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MAX_N = 1000000; 6 | 7 | int smallestFactor[MAX_N + 1]; 8 | vector primes; 9 | 10 | void linear_sieve() 11 | { 12 | for (int i = 2; i <= MAX_N; ++i) 13 | { 14 | if (smallestFactor[i] == 0) 15 | { 16 | smallestFactor[i] = i; 17 | primes.push_back(i); 18 | } 19 | 20 | for (int j = 0; j < (int)primes.size() && primes[j] <= smallestFactor[i] && i * primes[j] <= MAX_N; ++j) 21 | smallestFactor[i * primes[j]] = primes[j]; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Number-Theory/Modular Exponentiation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int modular_exponentiation(int a, int p, int n) /// a^p (mod n) 6 | { 7 | int solution = 1; 8 | 9 | while (p) 10 | { 11 | if (p & 1) 12 | solution = (1LL * solution * a) % n; 13 | 14 | a = (1LL * a * a) % n; 15 | p >>= 1; 16 | } 17 | 18 | return solution; 19 | } 20 | 21 | int main() 22 | { 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Number-Theory/Modular Inverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class ModularInverse 6 | { 7 | public: 8 | 9 | ModularInverse(){} 10 | 11 | int modularInverse(int A, int N) 12 | { 13 | int x, y; 14 | euclid(A, N, x, y); 15 | 16 | if (x <= 0) 17 | x = N + x % N; 18 | 19 | return x; 20 | } 21 | 22 | private: 23 | 24 | void euclid(int a, int b, int &x, int &y) 25 | { 26 | if (!b) 27 | { 28 | x = 1; 29 | y = 0; 30 | } 31 | else 32 | { 33 | int x0, y0; 34 | 35 | euclid(b, a % b, x0, y0); 36 | 37 | x = y0; 38 | y = x0 - (a / b) * y0; 39 | } 40 | } 41 | }; 42 | 43 | int main() 44 | { 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Number-Theory/Pollard's Rho algorithm (factorization).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int getRandomInt(int x, int y) 6 | { 7 | return x + rand() % (y - x + 1); 8 | } 9 | 10 | ///pseudo-random function 11 | int f(int x, int c, int n) /// f(x) = x^2 + c (mod n) 12 | { 13 | return (1LL * x * x + c) % n; 14 | } 15 | 16 | int rho(const int n) ///returns a proper divisor of n or n (if it fails!) 17 | { 18 | if (n % 2 == 0) 19 | return 2; 20 | 21 | int a = getRandomInt(1, n - 1); 22 | int c = getRandomInt(1, n - 1); 23 | 24 | int x = a; 25 | int y = a; 26 | int divisor; 27 | 28 | do 29 | { 30 | x = f(x, c, n); 31 | 32 | y = f(y, c, n); 33 | y = f(y, c, n); 34 | 35 | divisor = __gcd(abs(x - y), n); 36 | 37 | } while (divisor == 1); 38 | 39 | return divisor; 40 | } 41 | 42 | int pollard_rho(const int n) ///returns a proper divisor of n (IT FAILS IF n IS PRIME!) 43 | { 44 | srand(time(nullptr)); 45 | 46 | int d; 47 | 48 | do 49 | { 50 | d = rho(n); 51 | 52 | } while (d == n); 53 | 54 | return d; 55 | } 56 | 57 | int main() 58 | { 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Number-Theory/Sieve of Eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MAX_N = 100000000; 6 | 7 | bitset sieve; 8 | 9 | void generate_sieve1() 10 | { 11 | sieve.reset(); 12 | 13 | sieve[1] = true; 14 | 15 | for (int i = 4; i <= MAX_N; i += 2) 16 | sieve[i] = true; 17 | 18 | for (int i = 3; i <= MAX_N; i += 2) 19 | if (sieve[i] == 0) 20 | for (int j = 3 * i; j <= MAX_N; j += 2 * i) 21 | sieve[j] = 1; 22 | 23 | int nr = 0; 24 | 25 | for (int i = 2; i <= MAX_N; ++i) 26 | if (sieve[i] == 0) 27 | nr++; 28 | 29 | cout << nr << "\n"; 30 | } 31 | 32 | void generate_sieve2() 33 | { 34 | sieve.reset(); 35 | 36 | sieve[1] = true; 37 | 38 | for (int i = 2; i * i <= MAX_N; ++i) 39 | if (sieve[i] == 0) 40 | for (int j = i * i; j <= MAX_N; j += i) 41 | sieve[j] = 1; 42 | 43 | int nr = 0; 44 | 45 | for (int i = 2; i <= MAX_N; ++i) 46 | if (sieve[i] == 0) 47 | nr++; 48 | 49 | cout << nr << "\n"; 50 | } 51 | 52 | int main() 53 | { 54 | generate_sieve1(); 55 | generate_sieve2(); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Python-Algorithms/Sorting/Bubble Sort: -------------------------------------------------------------------------------- 1 | __author__ = 'alex' 2 | #bubble sort 3 | 4 | def sortare(v): 5 | 6 | for x in range(0, len(v)): 7 | for y in range(x + 1, len(v)): 8 | if v[x] > v[y]: 9 | aux = v[x] 10 | v[x] = v[y] 11 | v[y] = aux 12 | 13 | vector = map(int, raw_input().split(' ')) 14 | sortare(vector) 15 | print vector 16 | -------------------------------------------------------------------------------- /Python-Algorithms/Sorting/QuickSort: -------------------------------------------------------------------------------- 1 | __arrayuthor__ = 'alex' 2 | #quicksort 3 | 4 | def quicksort(array, left, right): 5 | if right - left > 0: 6 | pivot, i, j = array[(left + right) / 2], left, right 7 | 8 | while i <= j: 9 | 10 | while array[i] < pivot: 11 | i += 1 12 | while array[j] > pivot: 13 | j -= 1 14 | 15 | if i <= j: 16 | array[i], array[j] = array[j], array[i] 17 | i += 1 18 | j -= 1 19 | 20 | quicksort(array, left, j) 21 | quicksort(array, i, right) 22 | 23 | 24 | vector = map(int, raw_input().split(' ')) 25 | quicksort(vector, 0, len(vector) - 1) 26 | print vector 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Competitive-Programming-Training 2 | ================================ 3 | -------------------------------------------------------------------------------- /SPOJ/ 263. Period.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int Lmax = 1e6 + 2; 7 | 8 | int T, N; 9 | char S[Lmax]; 10 | int pi[Lmax]; 11 | 12 | void build_prefix() 13 | { 14 | int lgprefix = 0; 15 | 16 | pi[1] = 0; 17 | 18 | for ( int i = 2; i <= N; ++i ) 19 | { 20 | while ( lgprefix > 0 && S[i] != S[ lgprefix + 1 ] ) 21 | lgprefix = pi[ lgprefix ]; 22 | 23 | if ( S[i] == S[ lgprefix + 1 ] ) 24 | lgprefix++; 25 | 26 | pi[i] = lgprefix; 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | ///ifstream in("data.in"); 33 | 34 | cin >> T; 35 | 36 | for ( int k = 1; k <= T; ++k ) 37 | { 38 | cin >> N >> ( S + 1 ); 39 | 40 | build_prefix(); 41 | 42 | cout << "Test case #" << k << "\n"; 43 | 44 | for ( int i = 2; i <= N; ++i ) 45 | { 46 | if ( pi[i] && ( i % ( i - pi[i] ) == 0 ) ) 47 | { 48 | cout << i << " " << i / ( i - pi[i] ) << "\n"; 49 | } 50 | } 51 | 52 | if ( k < T ) cout << "\n"; 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /SPOJ/1028. Hubulullu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int dp[100]; 6 | int n; 7 | 8 | int min_max( vector &v ) 9 | { 10 | int c = 0; 11 | 12 | for ( int i = 1; i <= n; ++i ) 13 | c += v[i]; 14 | 15 | if ( c == n ) 16 | return 0; 17 | 18 | int sol = 0; 19 | 20 | for ( int i = 1; i <= n; ++i ) 21 | { 22 | if ( v[i] != 1 ) 23 | { 24 | vector a = v; 25 | 26 | for ( int j = 1; j <= i; ++j ) 27 | { 28 | if ( i % j == 0 ) 29 | a[j] = 1; 30 | } 31 | 32 | if ( min_max( a ) == 0 ) 33 | { 34 | sol = 1; 35 | break; 36 | } 37 | } 38 | } 39 | 40 | return sol; 41 | } 42 | 43 | int main() 44 | { 45 | int T, N, cine; 46 | 47 | cin >> T; 48 | 49 | while ( T-- ) 50 | { 51 | cin >> N >> cine; 52 | 53 | if ( cine == 0 ) 54 | cout << "Airborne wins.\n"; 55 | else 56 | cout << "Pagfloyd wins.\n"; 57 | } 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /SPOJ/1419. A Game with Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int heap; 8 | 9 | cin >> heap; 10 | 11 | if ( heap % 10 == 0 ) 12 | cout << "2\n"; 13 | else 14 | cout << "1\n" << heap % 10 << "\n"; 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /SPOJ/21347. Tulip And Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | inline int readNumber() 6 | { 7 | register int a = 0; 8 | char ch; 9 | 10 | do 11 | { 12 | ch = getchar(); 13 | 14 | } while ( !isdigit(ch) ); 15 | 16 | do 17 | { 18 | a = a * 10 + ch - '0'; 19 | ch = getchar(); 20 | 21 | } while ( isdigit(ch) ); 22 | 23 | return a; 24 | } 25 | 26 | const int Nmax = 100000 + 1; 27 | 28 | int process[Nmax]; 29 | int tests, N, M; 30 | 31 | int main() 32 | { 33 | tests = readNumber(); 34 | 35 | for ( int k = 1; k <= tests; ++k ) 36 | { 37 | N = readNumber(); M = readNumber(); 38 | 39 | int last = 0; 40 | 41 | for ( int i = 1; i <= N; ++i ) 42 | { 43 | int val = readNumber(); 44 | 45 | if ( last == val ) 46 | process[i] = process[i - 1]; 47 | else 48 | process[i] = process[i - 1] + 1; 49 | 50 | last = val; 51 | } 52 | 53 | printf("Case %d:\n", k); 54 | 55 | while ( M-- ) 56 | { 57 | int a, b, c = 0; 58 | a = readNumber(); b = readNumber(); 59 | 60 | if ( process[a] == process[a - 1] ) 61 | c++; 62 | 63 | printf("%d\n", process[b] - process[a - 1] + c ); 64 | } 65 | } 66 | 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /SPOJ/3885. Coins Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1e6 + 2; 6 | 7 | vector moves; 8 | 9 | int grundy[Nmax]; 10 | 11 | int mex( int state ) 12 | { 13 | if ( grundy[state] != -1 ) 14 | return grundy[state]; 15 | 16 | if ( grundy[state] == 0 ) 17 | return grundy[state] = 0; 18 | 19 | char use[20] = {}; 20 | 21 | for ( int i = 0; i < moves.size(); ++i ) 22 | if ( state >= moves[i] ) 23 | use[ mex( state - moves[i] ) ] = 1; 24 | 25 | grundy[state] = 0; 26 | 27 | while ( use[ grundy[state] ] ) 28 | grundy[state]++; 29 | 30 | return grundy[state]; 31 | } 32 | 33 | int main() 34 | { 35 | int K, L, N; 36 | 37 | cin >> K >> L >> N; 38 | 39 | moves.push_back( 1 ); 40 | moves.push_back( K ); 41 | moves.push_back( L ); 42 | 43 | memset( grundy, -1, sizeof( grundy ) ); 44 | 45 | for ( int i = 0; i < Nmax; ++i ) 46 | grundy[i] = mex( i ); 47 | 48 | while ( N-- ) 49 | { 50 | int a; 51 | 52 | cin >> a; 53 | 54 | if ( grundy[a] ) 55 | cout << "A"; 56 | else 57 | cout << "B"; 58 | } 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /SPOJ/3969. M&M Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int T, N, value; 8 | 9 | cin >> T; 10 | 11 | while ( T-- ) 12 | { 13 | cin >> N; 14 | 15 | int numberOfOne = 0, xorsum = 0; 16 | 17 | for ( int i = 1; i <= N; ++i ) 18 | { 19 | cin >> value; 20 | 21 | if ( value == 1 ) 22 | numberOfOne++; 23 | 24 | xorsum ^= value; 25 | } 26 | 27 | if ( numberOfOne == N ) 28 | { 29 | if ( numberOfOne % 2 == 0 ) 30 | cout << "John\n"; 31 | else 32 | cout << "Brother\n"; 33 | } 34 | else 35 | { 36 | if ( numberOfOne == N - 1 ) 37 | cout << "John\n"; 38 | else 39 | { 40 | if ( xorsum ) 41 | cout << "John\n"; 42 | else 43 | cout << "Brother\n"; 44 | } 45 | } 46 | } 47 | 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /SPOJ/4474. Longest Palindromic Substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int Lmax = 1e5 + 2; 8 | 9 | char S[Lmax]; 10 | char SS[2 * Lmax]; 11 | int P[2 * Lmax]; 12 | int N, NN; 13 | 14 | void transform() 15 | { 16 | NN = 0; 17 | 18 | SS[0] = '!'; 19 | 20 | SS[ ++NN ] = '#'; 21 | 22 | for ( int i = 1; i <= N; ++i ) 23 | { 24 | SS[ ++NN ] = S[i]; 25 | SS[ ++NN ] = '#'; 26 | } 27 | 28 | SS[NN + 1] = '@'; 29 | } 30 | 31 | void Manacher() 32 | { 33 | int indexOfPalindrome = 0, rightEndOfPalindrome = 0; 34 | int maximLength = 0; 35 | 36 | for ( int i = 1; i <= NN; ++i ) 37 | { 38 | if ( rightEndOfPalindrome > i ) 39 | P[i] = min( rightEndOfPalindrome - i, P[2 * indexOfPalindrome - i] ); 40 | 41 | while ( SS[i + P[i] + 1] == SS[i - P[i] - 1] ) P[i]++; 42 | 43 | if ( P[i] + i > rightEndOfPalindrome ) 44 | { 45 | rightEndOfPalindrome = P[i] + i; 46 | indexOfPalindrome = i; 47 | } 48 | 49 | maximLength = max( maximLength, P[i] ); 50 | } 51 | 52 | cout << maximLength << "\n"; 53 | } 54 | 55 | int main() 56 | { 57 | /// ifstream cin("data.in"); 58 | 59 | cin >> N; 60 | cin >> ( S + 1 ); 61 | 62 | transform(); 63 | Manacher(); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /SPOJ/5676. STONE GAME.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100 + 2; 6 | 7 | int T, N; 8 | 9 | int main() 10 | { 11 | cin >> T; 12 | 13 | while ( T-- ) 14 | { 15 | cin >> N; 16 | 17 | int xorsum = 0; 18 | 19 | for ( int i = 1, A; i <= N; ++i ) 20 | { 21 | cin >> A; 22 | 23 | while ( A >= i ) 24 | { 25 | xorsum ^= 1; 26 | A -= i; 27 | } 28 | } 29 | 30 | if ( xorsum ) 31 | cout << "ALICE\n"; 32 | else 33 | cout << "BOB\n"; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /SPOJ/5980. Matrix Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 50 + 3; 6 | 7 | int A[Nmax][Nmax]; 8 | int grundy[Nmax][Nmax]; 9 | int T, N, M; 10 | 11 | int main() 12 | { 13 | ///ifstream cin("data.in"); 14 | 15 | cin >> T; 16 | 17 | while ( T-- ) 18 | { 19 | cin >> N >> M; 20 | 21 | for ( int i = 1; i <= N; ++i ) 22 | for ( int j = 1; j <= M; ++j ) 23 | cin >> A[i][j]; 24 | 25 | for ( int i = 1; i <= N; ++i ) 26 | { 27 | grundy[i][M + 1] = 0; 28 | 29 | for ( int j = M; j >= 1; j-- ) 30 | { 31 | if ( A[i][j] <= grundy[i][j + 1] ) 32 | grundy[i][j] = A[i][j] - 1; 33 | else 34 | grundy[i][j] = A[i][j]; 35 | } 36 | } 37 | 38 | int xorsum = 0; 39 | 40 | for ( int i = 1; i <= N; ++i ) 41 | xorsum ^= grundy[i][1]; 42 | 43 | if ( xorsum ) 44 | cout << "FIRST\n"; 45 | else 46 | cout << "SECOND\n"; 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /SPOJ/6043. The Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int T, S; 6 | 7 | int main() 8 | { 9 | cin >> T; 10 | 11 | while ( T-- ) 12 | { 13 | cin >> S; 14 | 15 | int xorsum = 0; 16 | 17 | for ( int i = 1, a; i <= S; ++i ) 18 | { 19 | cin >> a; 20 | 21 | if ( a % 2 ) 22 | xorsum ^= i; 23 | } 24 | 25 | if ( xorsum ) 26 | cout << "Tom Wins\n"; 27 | else 28 | cout << "Hanks Wins\n"; 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /SPOJ/7423. File Recover Testing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int Lmax = 1e6 + 2; 8 | 9 | char S[Lmax]; 10 | int pi[Lmax]; 11 | int N, K; 12 | 13 | void build_prefix() 14 | { 15 | int lgprefix = 0; 16 | 17 | pi[1] = 0; 18 | 19 | for ( int i = 2; i <= N; ++i ) 20 | { 21 | while ( lgprefix > 0 && S[i] != S[lgprefix + 1] ) 22 | lgprefix = pi[lgprefix]; 23 | 24 | if ( S[i] == S[lgprefix + 1] ) 25 | lgprefix++; 26 | 27 | pi[i] = lgprefix; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | ///ifstream cin("data.in"); 34 | 35 | while ( 1 ) 36 | { 37 | cin >> K >> ( S + 1 ); 38 | 39 | if ( K == -1 ) break; 40 | 41 | N = strlen( S + 1 ); 42 | 43 | build_prefix(); 44 | 45 | int period = N - pi[N]; 46 | 47 | if ( K == N ) 48 | { 49 | cout << "1\n"; 50 | continue; 51 | } 52 | 53 | if ( K < N ) 54 | { 55 | cout << "0\n"; 56 | continue; 57 | } 58 | 59 | cout << 1 + ( K - N ) / ( N - pi[N] ) << "\n"; 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /SPOJ/8217. XOR Maximization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | 7 | long long v[Nmax]; 8 | int N; 9 | 10 | int main() 11 | { 12 | ios_base::sync_with_stdio(false); 13 | 14 | cin >> N; 15 | 16 | for ( int i = 1; i <= N; ++i ) 17 | cin >> v[i]; 18 | 19 | sort(v + 1, v + N + 1, greater()); 20 | 21 | int i = 1, j = 63; 22 | 23 | while (i <= N && j >= 0) 24 | { 25 | int x = 0; 26 | 27 | for ( int k = i; k <= N; ++k ) 28 | if ( v[k] & (1LL << j) ) 29 | { 30 | x = k; 31 | break; 32 | } 33 | 34 | if ( !x ) 35 | { 36 | j--; 37 | continue; 38 | } 39 | 40 | swap(v[i], v[x]); 41 | 42 | for ( int k = 1; k <= N; ++k ) 43 | if ( k != i && v[k] & (1LL << j) ) 44 | v[k] ^= v[i]; 45 | 46 | i++; j--; 47 | } 48 | 49 | long long sol = 0; 50 | 51 | for ( int i = 1; i <= N; ++i ) 52 | sol ^= v[i]; 53 | 54 | cout << sol << "\n"; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/3-way quicksort (strings sorting).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void ternary_quicksort(char **S, int lo, int hi, int kth) 6 | { 7 | if (lo >= hi) 8 | return; 9 | 10 | int p = lo; 11 | int st = lo; 12 | int dr = hi; 13 | 14 | char charMid = (S[lo][kth] + S[hi][kth]) / 2; 15 | 16 | while (p <= dr) 17 | { 18 | char c = S[p][kth]; 19 | 20 | if (c < charMid) 21 | { 22 | swap(S[st], S[p]); 23 | st++; 24 | p++; 25 | } 26 | else if (c > charMid) 27 | { 28 | swap(S[dr], S[p]); 29 | dr--; 30 | } 31 | else 32 | p++; 33 | } 34 | 35 | ternary_quicksort(S, lo, st - 1, kth); 36 | 37 | if (S[st][kth] != '\0') 38 | ternary_quicksort(S, st, dr, kth + 1); 39 | 40 | ternary_quicksort(S, dr + 1, hi, kth); 41 | } 42 | 43 | int main() 44 | { 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Bogosort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void bogosort(int A[], int N) ///O((n+1)!) 6 | { 7 | srand(time(nullptr)); 8 | bool stop; 9 | 10 | do 11 | { 12 | random_shuffle(A, A + N); 13 | 14 | stop = true; 15 | 16 | for (int i = 1; i < N && stop == true; ++i) 17 | if (A[i - 1] > A[i]) 18 | stop = false; 19 | 20 | } while (stop == false); 21 | } 22 | 23 | int main() 24 | { 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Bubble sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void bubble_sort(int A[], int l, int r) 6 | { 7 | bool change; 8 | 9 | do 10 | { 11 | change = false; 12 | 13 | for (int i = l; i < r; ++i) 14 | if (A[i] > A[i + 1]) 15 | { 16 | swap(A[i], A[i + 1]); 17 | change = true; 18 | } 19 | 20 | } while (change == true); 21 | } 22 | 23 | int main() 24 | { 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Cocktail sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void cocktail_sort(int A[], int N) ///bidirectional bubble sort 6 | { 7 | int begin = -1; 8 | int end = N - 2; 9 | bool swapped; 10 | 11 | do 12 | { 13 | swapped = false; 14 | begin++; 15 | 16 | for (int i = begin; i <= end; ++i) 17 | if (A[i] > A[i + 1]) 18 | { 19 | swap(A[i], A[i + 1]); 20 | swapped = true; 21 | } 22 | 23 | if (swapped == false) 24 | break; 25 | 26 | swapped = false; 27 | end--; 28 | 29 | for (int i = end; i >= begin; i--) 30 | if (A[i] > A[i + 1]) 31 | { 32 | swap(A[i], A[i + 1]); 33 | swapped = true; 34 | } 35 | 36 | } while (swapped == true); 37 | } 38 | 39 | int main() 40 | { 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Comb sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void comb_sort(int A[], int N) 6 | { 7 | int gap = N; 8 | float shrink = 1.3; 9 | bool swapped = true; 10 | 11 | while (gap > 1 || swapped == true) 12 | { 13 | gap = int(1.0 * gap / shrink); 14 | 15 | if (gap < 1) 16 | gap = 1; 17 | 18 | int i = 0; 19 | swapped = false; 20 | 21 | while (i + gap < N) 22 | { 23 | if (A[i] > A[i + gap]) 24 | { 25 | swap(A[i], A[i + gap]); 26 | swapped = true; 27 | } 28 | 29 | i++; 30 | } 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Counting sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void counting_sort(int A[], int N, int B[], int C[], const int MAX_V) 6 | { 7 | for (int i = 0; i <= MAX_V; ++i) 8 | C[i] = 0; 9 | 10 | for (int i = 0; i < N; ++i) 11 | C[ A[i] ]++; 12 | 13 | for (int i = 1; i <= MAX_V; ++i) 14 | C[i] += C[i - 1]; 15 | 16 | for (int i = N - 1; i >= 0; i--) 17 | B[ --C[ A[i] ] ] = A[i]; 18 | 19 | for (int i = 0; i < N; ++i) 20 | A[i] = B[i]; 21 | } 22 | 23 | int main() 24 | { 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Heapsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void max_heapify(int H[], int N, int i) 6 | { 7 | int l, r, largest; 8 | bool changed; 9 | 10 | do 11 | { 12 | changed = false; 13 | 14 | l = 2 * i; 15 | r = 2 * i + 1; 16 | 17 | if (l <= N && H[l] > H[i]) 18 | largest = l; 19 | else 20 | largest = i; 21 | 22 | if (r <= N && H[r] > H[largest]) 23 | largest = r; 24 | 25 | if (i != largest) 26 | { 27 | swap(H[i], H[largest]); 28 | i = largest; 29 | changed = true; 30 | } 31 | 32 | } while (changed == true); 33 | } 34 | 35 | void build_max_heap(int H[], int N) 36 | { 37 | for (int i = N / 2; i >= 1; i--) 38 | max_heapify(H, N, i); 39 | } 40 | 41 | void heapsort(int H[], int N) ///1-based 42 | { 43 | build_max_heap(H, N); 44 | 45 | for (int i = N; i >= 2; i--) 46 | { 47 | swap(H[i], H[1]); 48 | N--; 49 | max_heapify(H, N, 1); 50 | } 51 | } 52 | 53 | int main() 54 | { 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Insertion sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void insertion_sort(int A[], int l, int r) 6 | { 7 | for (int i = l + 1; i <= r; ++i) 8 | { 9 | int j = i; 10 | 11 | while (j > l && A[j - 1] > A[j]) 12 | { 13 | swap(A[j - 1], A[j]); 14 | j--; 15 | } 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Merge sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void merge_sort(int A[], int B[], int l, int r) 6 | { 7 | if (l == r) 8 | return; 9 | 10 | int m = (l + r) / 2; 11 | 12 | merge_sort(A, B, l, m); 13 | merge_sort(A, B, m + 1, r); 14 | 15 | int i = l, j = m + 1; 16 | int k = l; 17 | 18 | while (i <= m && j <= r) 19 | { 20 | if (A[i] <= A[j]) 21 | B[k++] = A[i++]; 22 | else 23 | B[k++] = A[j++]; 24 | } 25 | 26 | while (i <= m) 27 | B[k++] = A[i++]; 28 | 29 | while (j <= r) 30 | B[k++] = A[j++]; 31 | 32 | for (int p = l; p <= r; ++p) 33 | A[p] = B[p]; 34 | } 35 | 36 | int main() 37 | { 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Order Statistics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void kth_element(int A[], int l, int r, int k) 6 | { 7 | if (l >= r) 8 | return; 9 | 10 | int i = l, j = r; 11 | int p = A[l + rand() % (r - l + 1)]; 12 | 13 | do 14 | { 15 | while (A[i] < p) i++; 16 | while (A[j] > p) j--; 17 | 18 | if (i <= j) 19 | { 20 | swap(A[i], A[j]); 21 | 22 | i++; 23 | j--; 24 | } 25 | 26 | } while (i < j); 27 | 28 | if (i <= k && k <= r) 29 | kth_element(A, i, r, k); 30 | 31 | if (l <= k && k <= j) 32 | kth_element(A, l, j, k); 33 | } 34 | 35 | int main() 36 | { 37 | srand(time(nullptr)); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void quicksort(int A[], int l, int r) 6 | { 7 | if (l >= r) 8 | return; 9 | 10 | int i = l, j = r; 11 | int p = A[l + rand() % (r - l + 1)]; 12 | 13 | do 14 | { 15 | while (A[i] < p) i++; 16 | while (A[j] > p) j--; 17 | 18 | if (i <= j) 19 | { 20 | swap(A[i], A[j]); 21 | 22 | i++; 23 | j--; 24 | } 25 | 26 | } while (i < j); 27 | 28 | quicksort(A, i, r); 29 | quicksort(A, l, j); 30 | } 31 | 32 | int main() 33 | { 34 | srand(time(nullptr)); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Radix sort (LSD).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int MAX_DIGITS = 32; 6 | const int MAX_D = 4; 7 | const int SIZE_RADIX = 1 << (MAX_DIGITS / MAX_D); 8 | const int mask = SIZE_RADIX - 1; 9 | 10 | void radix_sort(int A[], int N, int B[], int C[]) 11 | { 12 | for (int d = 0, shift = 0; d < MAX_D; d++, shift += (MAX_DIGITS / MAX_D)) 13 | { 14 | for (int i = 0; i < SIZE_RADIX; ++i) 15 | C[i] = 0; 16 | 17 | for (int i = 0; i < N; ++i) 18 | C[ (A[i] >> shift) & mask ]++; 19 | 20 | for (int i = 1; i < SIZE_RADIX; ++i) 21 | C[i] += C[i - 1]; 22 | 23 | for (int i = N - 1; i >= 0; i--) 24 | B[ --C[ (A[i] >> shift) & mask ] ] = A[i]; 25 | 26 | for (int i = 0; i < N; ++i) 27 | A[i] = B[i]; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Selection sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void selection_sort(int A[], int l, int r) 6 | { 7 | for (int i = l; i < r; ++i) 8 | { 9 | int p = i; 10 | 11 | for (int j = i + 1; j <= r; ++j) 12 | if (A[j] < A[p]) 13 | p = j; 14 | 15 | swap(A[i], A[p]); 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Shell sort (James A. Sellers' sequence).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void shell_sort(int A[], int n) 6 | { 7 | const int gaps[] = {1, 2, 4, 8, 21, 56, 149, 404, 1098, 2982, 8104, 22027, 59875, 162756, 442414}; ///James A. Sellers sequence 8 | 9 | for (int p = 14; p >= 0; p--) 10 | { 11 | int gap = gaps[p]; 12 | 13 | for (int i = gap; i < n; ++i) 14 | { 15 | int temp = A[i]; 16 | int j = i; 17 | 18 | while (j >= gap && A[j - gap] > temp) 19 | { 20 | A[j] = A[j - gap]; 21 | j -= gap; 22 | } 23 | 24 | A[j] = temp; 25 | } 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Shell sort (M.Ciura's gap sequence).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void shell_sort(int A[], int n) 6 | { 7 | const int gaps[] = {1, 4, 10, 23, 57, 132, 301, 701, 1750}; ///Marcin Ciura's gap sequence 8 | 9 | for (int p = 8; p >= 0; p--) 10 | { 11 | int gap = gaps[p]; 12 | 13 | for (int i = gap; i < n; ++i) 14 | { 15 | int temp = A[i]; 16 | int j = i; 17 | 18 | while (j >= gap && A[j - gap] > temp) 19 | { 20 | A[j] = A[j - gap]; 21 | j -= gap; 22 | } 23 | 24 | A[j] = temp; 25 | } 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Sorting-and-Order-Statistics/Stooge sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void stooge_sort(int A[], int lo, int hi) /// O(n^2.7095...) 6 | { 7 | if (A[lo] > A[hi]) 8 | swap(A[lo], A[hi]); 9 | 10 | int length = hi - lo + 1; 11 | 12 | if (length >= 3) 13 | { 14 | stooge_sort(A, lo, hi - length / 3); 15 | stooge_sort(A, lo + length / 3, hi); 16 | stooge_sort(A, lo, hi - length / 3); 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /String-Algorithms/3-way quicksort (strings sorting).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void quicksort(char **S, int lo, int hi, int kth) 6 | { 7 | if (lo >= hi) 8 | return; 9 | 10 | int p = lo; 11 | int st = lo; 12 | int dr = hi; 13 | 14 | char midChar = (S[lo][kth] + S[hi][kth]) / 2; 15 | 16 | while (p <= dr) 17 | { 18 | char c = S[p][kth]; 19 | 20 | if (c < midChar) 21 | { 22 | swap(S[p], S[st]); 23 | st++; 24 | p++; 25 | } 26 | else if (c > midChar) 27 | { 28 | swap(S[p], S[dr]); 29 | dr--; 30 | } 31 | else 32 | p++; 33 | } 34 | 35 | quicksort(S, lo, st - 1, kth); 36 | 37 | if (S[st][kth] != '\0') 38 | quicksort(S, st, dr, kth + 1); 39 | 40 | quicksort(S, dr + 1, hi, kth); 41 | } 42 | 43 | int main() 44 | { 45 | FILE *in = fopen("stringsort.in", "r"); 46 | FILE *out = fopen("stringsort.out", "w"); 47 | 48 | int N; 49 | fscanf(in, "%d\n", &N); 50 | 51 | char **S = new char*[N]; 52 | 53 | for (int i = 0; i < N; ++i) 54 | { 55 | int l; 56 | fscanf(in, "%d ", &l); 57 | 58 | S[i] = new char[l + 1]; 59 | 60 | fscanf(in, "%s\n", S[i]); 61 | } 62 | 63 | quicksort(S, 0, N - 1, 0); 64 | 65 | for (int i = 0; i < N; ++i) 66 | fprintf(out, "%s\n", S[i]); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /String-Algorithms/All periods of a string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int Lmax = 1e7 + 2; 9 | 10 | char S[Lmax]; 11 | int pi[Lmax]; 12 | int N; 13 | 14 | void build_prefix() 15 | { 16 | int lgprefix = 0; 17 | 18 | pi[1] = 0; 19 | 20 | for ( int i = 2; i <= N; ++i ) 21 | { 22 | while ( lgprefix > 0 && S[i] != S[ lgprefix + 1 ] ) 23 | lgprefix = pi[ lgprefix ]; 24 | 25 | if ( S[i] == S[ lgprefix + 1 ] ) 26 | lgprefix++; 27 | 28 | pi[i] = lgprefix; 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | cin >> N; 35 | cin >> ( S + 1 ); 36 | 37 | build_prefix(); 38 | 39 | int lgprefix = pi[N]; 40 | int sol = 0; 41 | 42 | while ( lgprefix ) 43 | { 44 | if ( N % ( N - lgprefix ) == 0 ) 45 | sol++; 46 | 47 | lgprefix = pi[lgprefix]; 48 | } 49 | 50 | cout << sol << "\n"; 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /String-Algorithms/Rolling hash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | class RollingHash 7 | { 8 | /** 9 | MOD * BASE < INT.MAX_VALUE 10 | BASE * BASE < INT.MAX_VALUE 11 | MOD * SIGMA < INT.MAX_LALUE 12 | **/ 13 | 14 | public: 15 | 16 | int code; 17 | int base; 18 | 19 | RollingHash(const int length = 0) : code(0), base(1) { 20 | 21 | for (int i = 1; i < length; ++i) 22 | base = (base * BASE) % MOD; 23 | } 24 | 25 | void pop_front(const char ch) 26 | { 27 | code = (code - (base * ch) % MOD + MOD) % MOD; ///POSSIBLE OVERFLOW 28 | } 29 | 30 | void push_back(const char ch) 31 | { 32 | code = (code * BASE) % MOD; ///POSSIBLE OVERFLOW 33 | code = (code + ch) % MOD; 34 | } 35 | 36 | bool operator == (const RollingHash& R) const 37 | { 38 | return this->code == R.code; 39 | } 40 | 41 | bool operator != (const RollingHash& R) const 42 | { 43 | return this->code != R.code; 44 | } 45 | }; 46 | 47 | int main() 48 | { 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /String-Algorithms/Suffix Array O(N^2).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int Lmax = 1e5 + 2; 9 | 10 | char S[Lmax]; 11 | int SA[Lmax]; 12 | int N; 13 | 14 | int cmp( int i, int j ) 15 | { 16 | return strcmp( S + i, S + j ) < 0; 17 | } 18 | 19 | void buildSuffixArray() 20 | { 21 | for ( int i = 0; i < N; ++i ) 22 | SA[i] = i; 23 | 24 | sort( SA, SA + N, cmp ); 25 | } 26 | 27 | int main() 28 | { 29 | cin.sync_with_stdio( false ); 30 | 31 | cin >> S; 32 | 33 | N = strlen( S ); 34 | 35 | buildSuffixArray(); 36 | 37 | for ( int i = 0; i < N; ++i ) 38 | cout << SA[i] << "\n"; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /String-Algorithms/Suffix Array O(Nlog^2N).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef unsigned long long ull; 9 | 10 | const ull BASE = 137; 11 | const int Lmax = 1e5 + 2; 12 | 13 | char S[Lmax]; 14 | int SA[Lmax]; 15 | ull H[Lmax], Hpow[Lmax]; 16 | int N; 17 | 18 | ull getHash( int i, int dim ) 19 | { 20 | return ( H[i] - H[i + dim] * Hpow[dim] ); 21 | } 22 | 23 | bool cmp( int i, int j ) 24 | { 25 | int lo = 1, hi = min( N - i, N - j ); 26 | 27 | while ( lo <= hi ) 28 | { 29 | int m = ( lo + hi ) / 2; 30 | 31 | if ( getHash( i, m ) == getHash( j, m ) ) 32 | lo = m + 1; 33 | else 34 | hi = m - 1; 35 | } 36 | 37 | return S[i + hi] < S[j + hi]; 38 | } 39 | 40 | void buildSuffixArray() 41 | { 42 | Hpow[0] = 1; 43 | 44 | for ( int i = 1; i <= N; ++i ) 45 | Hpow[i] = Hpow[i - 1] * BASE; 46 | 47 | H[N] = 0; 48 | 49 | for ( int i = N - 1; i >= 0; i-- ) 50 | { 51 | H[i] = H[i + 1] * BASE + S[i]; 52 | SA[i] = i; 53 | } 54 | 55 | stable_sort( SA, SA + N, cmp ); 56 | } 57 | 58 | int main() 59 | { 60 | cin.sync_with_stdio( false ); 61 | 62 | cin >> S; 63 | 64 | N = strlen( S ); 65 | 66 | buildSuffixArray(); 67 | 68 | for ( int i = 0; i < N; ++i ) 69 | cout << SA[i] << "\n"; 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /String-Algorithms/Z Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector Z_Algorithm( const string &str ) 6 | { 7 | int L = 0, R = 0; 8 | int lg = str.size(); 9 | 10 | vector Z( lg, 0 ); 11 | 12 | for ( int i = 1; i < lg; ++i ) 13 | { 14 | if ( i <= R ) 15 | Z[i] = min( R - i + 1, Z[i - L] ); 16 | 17 | while ( i + Z[i] < lg && str[ Z[i] ] == str[ i + Z[i] ] ) Z[i]++; 18 | 19 | if ( i + Z[i] - 1 > R ) 20 | { 21 | L = i; 22 | R = i + Z[i] - 1; 23 | } 24 | } 25 | 26 | return Z; 27 | } 28 | 29 | int main() 30 | { 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Timus/1102. Strange Dialog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1e7 + 5; 6 | 7 | char dp[Nmax]; 8 | char A[Nmax]; 9 | 10 | string v[] = { "input", "output", "one", "puton", "in", "out" }; 11 | int T, N; 12 | 13 | int sePoate( int j, int pos ) 14 | { 15 | if ( j + v[pos].size() - 1 > N ) 16 | return false; 17 | 18 | for ( int i = 0; i < v[pos].size(); ++i ) 19 | if ( A[i + j] != v[pos][i] ) 20 | return false; 21 | 22 | return true; 23 | } 24 | 25 | int main() 26 | { 27 | scanf("%d\n", &T); 28 | 29 | while ( T-- ) 30 | { 31 | scanf("%s", A + 1); 32 | 33 | N = strlen( A + 1 ); 34 | 35 | memset( dp, 0, sizeof( char ) * ( N + 3 ) ); 36 | 37 | dp[0] = 1; 38 | 39 | for ( int i = 0; i < N; ++i ) 40 | { 41 | if ( dp[i] ) 42 | { 43 | for ( int j = 0; j < 6; ++j ) 44 | { 45 | if ( sePoate( i + 1, j ) ) 46 | dp[i + v[j].size()] = 1; 47 | } 48 | } 49 | } 50 | 51 | if ( dp[N] ) 52 | puts("YES"); 53 | else 54 | puts("NO"); 55 | } 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Timus/1716. Alternative Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int Nmax = 5001; 8 | 9 | int N, S; 10 | 11 | double YES[3][Nmax]; 12 | double NO[3][Nmax]; 13 | 14 | int main() 15 | { 16 | ///ifstream cin("date.in"); 17 | 18 | cin >> N >> S; 19 | 20 | int Yes = S - 2 * N; 21 | int No = N - Yes; 22 | 23 | for ( int i = 0; i <= Yes; ++i ) 24 | { 25 | for ( int j = 0; j <= No; ++j ) 26 | { 27 | if ( i >= 1 ) 28 | { 29 | YES[i % 2][j] = YES[(i - 1) % 2][j] * i / ( i + j ) * 1.0; 30 | NO[i % 2][j] = ( 1 + YES[(i - 1) % 2][j] ) * i / ( i + j ) * 1.0; 31 | } 32 | 33 | if ( j >= 1 ) 34 | { 35 | NO[i % 2][j] += NO[i % 2][j - 1] * j / ( i + j ) * 1.0; 36 | YES[i % 2][j] += ( 1 + NO[i % 2][j - 1] ) * j / ( i + j ) * 1.0; 37 | } 38 | } 39 | } 40 | 41 | cout << fixed << setprecision( 10 ); 42 | cout << YES[Yes % 2][No]< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class InputBoxChecker 9 | { 10 | public: 11 | 12 | int valid(long long smallest, long long largest, long long number) 13 | { 14 | for ( long long l = 1; number * l <= largest; l *= 10LL ) 15 | { 16 | if ( smallest / l <= number && number <= largest / l ) 17 | return true; 18 | } 19 | 20 | return false; 21 | } 22 | 23 | vector checkPrefix(int smallest, int largest, vector numbers) 24 | { 25 | vector v; 26 | 27 | for ( int i = 0; i < numbers.size(); ++i ) 28 | { 29 | if ( valid( smallest, largest, numbers[i] ) ) 30 | v.push_back( "VALID" ); 31 | else 32 | v.push_back( "INVALID" ); 33 | } 34 | 35 | return v; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /TopCoder/SRM-310-DIV2/MeasuringTemperature: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class MeasuringTemperature 7 | { 8 | public: 9 | 10 | double averageTemperature(vector measuredValues) 11 | { 12 | double sum = 0; 13 | double nr = 0; 14 | 15 | for ( int i = 0; i < measuredValues.size(); ++i ) 16 | { 17 | if ( measuredValues[i] < -273 ) continue; 18 | 19 | if ( i >= 2 ) 20 | { 21 | if ( abs( measuredValues[i] - measuredValues[i - 1] ) > 2 && abs( measuredValues[i] - measuredValues[i - 2] ) > 2 ) 22 | continue; 23 | } 24 | 25 | sum += measuredValues[i]; 26 | nr++; 27 | } 28 | 29 | if ( nr == 0 ) 30 | return -300.0; 31 | else 32 | return ( sum / nr ); 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /TopCoder/SRM-612-DIV-2/EmoticonsDiv2: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | #define pp pair 10 | 11 | const int Nmax = 1000 + 3; 12 | const int inf = 1e9; 13 | int A[Nmax][Nmax]; /// A[i][j] = i emoticons on the screen and j in the clipboard 14 | 15 | class EmoticonsDiv2 16 | { 17 | public: 18 | 19 | int printSmiles(int smiles) 20 | { 21 | for ( int i = 0; i <= smiles; ++i ) 22 | for ( int j = 0; j <= smiles; ++j ) 23 | A[i][j] = inf; 24 | 25 | A[1][0] = 0; 26 | 27 | for ( int i = 1; i <= smiles; ++i ) 28 | for ( int j = 0; j <= smiles; ++j ) 29 | { 30 | if ( i + j <= smiles && A[i + j][j] > A[i][j] + 1 ) 31 | A[i + j][j] = A[i][j] + 1; 32 | 33 | if ( A[i][i] > A[i][j] + 1 ) 34 | A[i][i] = A[i][j] + 1; 35 | } 36 | 37 | int minim = inf; 38 | 39 | for ( int i = 1; i <= smiles; ++i ) 40 | minim = min( minim, A[smiles][i] ); 41 | 42 | return minim; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /TopCoder/SRM-612-DIV-2/LeftAndRightHandedDiv2: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class LeftAndRightHandedDiv2 10 | { 11 | public: 12 | 13 | int count(string S) 14 | { 15 | int sol = 0; 16 | 17 | for ( int i = 0; i < S.size() - 1; ++i ) 18 | { 19 | if ( S[i] == 'R' && S[i + 1] == 'L' ) 20 | sol++; 21 | } 22 | 23 | return sol; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /TopCoder/SRM-612-DIV-2/PowersOfTwo: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int Nmax = 50; 10 | const int Bmax = 60; 11 | 12 | long long DP[Bmax + 1][Nmax + 1]; 13 | int apar[Bmax + 1]; 14 | 15 | class PowersOfTwo 16 | { 17 | public: 18 | 19 | long long memo( int bit, int extra ) 20 | { 21 | if ( DP[bit][extra] != -1 ) 22 | return DP[bit][extra]; 23 | 24 | if ( bit == Bmax ) 25 | return 1; 26 | 27 | DP[bit][extra] = 0; 28 | 29 | int new_extra = apar[bit] + extra; 30 | 31 | DP[bit][extra] += memo( bit + 1, new_extra / 2 ); 32 | 33 | if ( new_extra > 0 ) 34 | DP[bit][extra] += memo( bit + 1, ( new_extra - 1 ) / 2 ); 35 | 36 | return DP[bit][extra]; 37 | } 38 | 39 | long long count(vector powers) 40 | { 41 | for ( int i = 0; i < Bmax; ++i ) 42 | apar[i] = std::count( powers.begin(), powers.end(), ( 1LL << i ) ); 43 | 44 | memset( DP, -1, sizeof ( DP ) ); 45 | 46 | return memo( 0, 0 ); 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /TopCoder/SRM-621-DIV-2/MixingColors: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int Nmax = 50 + 2; 9 | 10 | class MixingColors 11 | { 12 | public: 13 | 14 | int vis[33]; 15 | 16 | int minColors(vector colors) 17 | { 18 | for ( int i = 0; i < colors.size(); ++i ) 19 | { 20 | for ( int j = 0; ( 1 << j ) <= colors[i]; ++j ) 21 | { 22 | if ( colors[i] & ( 1 << j ) ) 23 | vis[j] = 1; 24 | } 25 | } 26 | 27 | int sum = 0; 28 | 29 | for ( int i = 0; i < 33; ++i ) 30 | sum += vis[i]; 31 | 32 | return min( sum, (int)colors.size() ); 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /TopCoder/SRM-621-DIV-2/NumbersChallenge: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int Nmax = 20 + 2; 9 | const int Lmax = 100000 + 2; 10 | 11 | class NumbersChallenge 12 | { 13 | public: 14 | 15 | bool format[Lmax * Nmax]; 16 | 17 | int MinNumber(vector S) 18 | { 19 | memset( format, 0, sizeof( format ) ); 20 | 21 | format[0] = 1; 22 | int maxim = 0; 23 | 24 | for ( int i = 0; i < S.size(); ++i ) 25 | { 26 | for ( int j = maxim; j >= 0; j-- ) 27 | if ( format[j] ) 28 | format[j + S[i]] = 1; 29 | 30 | maxim += S[i]; 31 | } 32 | 33 | for ( int i = 1; i <= Lmax * Nmax; ++i ) 34 | if ( format[i] == 0 ) 35 | return i; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /TopCoder/SRM-621-DIV-2/TwoWaysSorting: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int cmp( const string &a, const string &b ) 10 | { 11 | return a.size() < b.size(); 12 | } 13 | 14 | class TwoWaysSorting 15 | { 16 | public: 17 | 18 | string sortingMethod(vector stringList) 19 | { 20 | vector v = stringList, vv = stringList; 21 | 22 | sort( v.begin(), v.end() ); 23 | sort( vv.begin(), vv.end(), cmp ); 24 | 25 | int validB = 1; 26 | int validL = 1; 27 | int validLG = 1; 28 | 29 | for ( int i = 0; i < v.size(); ++i ) 30 | { 31 | if ( v[i] != stringList[i] ) 32 | { 33 | validL = 0; 34 | } 35 | 36 | if ( i > 0 ) 37 | { 38 | if ( v[i].size() > v[i - 1].size() ) 39 | validLG = 0; 40 | } 41 | } 42 | 43 | if ( validL ) 44 | { 45 | if ( validLG ) 46 | return "both"; 47 | else 48 | return "lexicographically"; 49 | } 50 | else 51 | { 52 | int vis = 1; 53 | 54 | for ( int i = 0; i < vv.size(); ++i ) 55 | { 56 | if ( vv[i] != stringList[i] ) 57 | vis = 0; 58 | } 59 | 60 | if ( vis ) 61 | return "lengths"; 62 | } 63 | 64 | return "none"; 65 | } 66 | }; 67 | -------------------------------------------------------------------------------- /TopCoder/SRM-626-DIV-2/FixedDiceGameDiv2: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class FixedDiceGameDiv2 9 | { 10 | public: 11 | 12 | double getExpectation(int a, int b) 13 | { 14 | double sol = 0; 15 | double cnt = 0; 16 | 17 | for ( int i = 1; i <= b; ++i ) 18 | { 19 | for ( int j = i + 1; j <= a; ++j ) 20 | { 21 | sol += j; 22 | cnt++; 23 | } 24 | } 25 | 26 | return sol / cnt; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Tree-Algorithms/Lowest Common Ancestor in O(sqrtN).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int Nmax = 1e5 + 2; 8 | 9 | vector G[Nmax]; 10 | int path[Nmax], father[Nmax]; 11 | int depth[Nmax]; 12 | int N, Q, sqrtN; 13 | 14 | void DFS( int node, int P ) 15 | { 16 | path[node] = P; 17 | 18 | if ( depth[node] % sqrtN == 0 ) 19 | P = node; 20 | 21 | for ( auto x: G[node] ) 22 | { 23 | depth[x] = depth[node] + 1; 24 | DFS( x, P ); 25 | } 26 | } 27 | 28 | int lca( int x, int y ) 29 | { 30 | while ( path[x] != path[y] ) 31 | { 32 | if ( depth[x] < depth[y] ) 33 | y = path[y]; 34 | else 35 | x = path[x]; 36 | } 37 | 38 | while ( x != y ) 39 | { 40 | if ( depth[x] < depth[y] ) 41 | y = father[y]; 42 | else 43 | x = father[x]; 44 | } 45 | 46 | return x; 47 | } 48 | 49 | int main() 50 | { 51 | cin.sync_with_stdio( false ); 52 | 53 | cin >> N >> Q; 54 | 55 | for ( sqrtN = 1; sqrtN * sqrtN <= N; sqrtN++ ); 56 | 57 | for ( int i = 2, a; i <= N; ++i ) 58 | { 59 | cin >> a; 60 | 61 | G[a].push_back( i ); 62 | father[i] = a; 63 | } 64 | 65 | DFS( 1, 1 ); 66 | 67 | while ( Q-- ) 68 | { 69 | int a, b; 70 | 71 | cin >> a >> b; 72 | cout << lca( a, b ) << "\n"; 73 | } 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Tree-Algorithms/Tree's diameter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int Nmax = 1e5; 10 | 11 | vector G[Nmax + 1]; 12 | queue Q; 13 | int dist[Nmax + 1]; 14 | 15 | int N; 16 | 17 | int BFS( int nod ) 18 | { 19 | fill( dist + 1, dist + N + 1, 0 ); 20 | 21 | dist[nod] = 1; 22 | Q.push( nod ); 23 | 24 | while ( !Q.empty() ) 25 | { 26 | nod = Q.front(); 27 | Q.pop(); 28 | 29 | for ( auto x: G[nod] ) 30 | if ( !dist[x] ) 31 | { 32 | dist[x] = dist[nod] + 1; 33 | Q.push( x ); 34 | } 35 | } 36 | 37 | return *max_element( dist + 1, dist + N + 1 ); 38 | } 39 | 40 | int diameter() 41 | { 42 | int d = BFS( 1 ); 43 | int v = find( dist + 1, dist + N + 1, d ) - dist; 44 | 45 | return BFS( v ); 46 | } 47 | 48 | int main() 49 | { 50 | cin >> N; 51 | 52 | for ( int i = 1, a, b; i < N; ++i ) 53 | { 54 | cin >> a >> b; 55 | 56 | G[a].push_back( b ); 57 | G[b].push_back( a ); 58 | } 59 | 60 | cout << diameter() << "\n"; 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /USACO/February-2015/Cow Hopscotch (Silver): -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100 + 1; 6 | const int MOD = 1000000007; 7 | 8 | int dp[Nmax][Nmax]; 9 | int A[Nmax][Nmax]; 10 | 11 | int N, M, K; 12 | 13 | int main() 14 | { 15 | ifstream in("hopscotch.in"); 16 | ofstream out("hopscotch.out"); 17 | 18 | ios_base::sync_with_stdio(false); 19 | 20 | in >> N >> M >> K; 21 | 22 | for ( int i = 1; i <= N; ++i ) 23 | for ( int j = 1; j <= M; ++j ) 24 | in >> A[i][j]; 25 | 26 | dp[N][M] = 1; 27 | 28 | for ( int i = N; i >= 1; i-- ) 29 | for ( int j = M; j >= 1; j-- ) 30 | { 31 | for ( int l = i + 1; l <= N; ++l ) 32 | for ( int c = j + 1; c <= N; ++c ) 33 | if ( A[l][c] != A[i][j] ) 34 | dp[i][j] = (dp[i][j] + dp[l][c]) % MOD; 35 | } 36 | 37 | out << dp[1][1] << "\n"; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /USACO/February-2015/Superbull: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 2000 + 1; 6 | const int INF = (1 << 30); 7 | 8 | int v[Nmax]; 9 | int dist[Nmax], vis[Nmax]; 10 | int N; 11 | 12 | int main() 13 | { 14 | ifstream in("superbull.in"); 15 | ofstream out("superbull.out"); 16 | 17 | in >> N; 18 | 19 | for ( int i = 1; i <= N; ++i ) 20 | in >> v[i]; 21 | 22 | for ( int i = 0; i <= N; ++i ) 23 | dist[i] = -INF; 24 | 25 | dist[1] = 0; 26 | long long sum = 0; 27 | 28 | for ( int i = 1; i <= N; ++i ) 29 | { 30 | int root = 0; 31 | int best = -INF; 32 | 33 | for ( int j = 1; j <= N; ++j ) 34 | if ( vis[j] == 0 && dist[j] > best ) 35 | { 36 | root = j; 37 | best = dist[j]; 38 | } 39 | 40 | vis[root] = 1; 41 | sum += dist[root]; 42 | 43 | for ( int j = 1; j <= N; ++j ) 44 | if ( vis[j] == 0 ) 45 | dist[j] = max(dist[j], v[root] ^ v[j]); 46 | } 47 | 48 | out << sum << "\n"; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Unordered-Map killer: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void gen02(std::ostream& out) { 6 | int n = 100000; 7 | int k = 4000; 8 | int p = 7517; 9 | n = n / k * k; 10 | std::vector as(n); 11 | for (int i = 0; i < n / k; i++) { 12 | for (int j = 0; j < k; j++) { 13 | as[i * k + j] = j * p + 1; 14 | } 15 | } 16 | out << n << std::endl; 17 | for (int j = 0; j < n ;j++) { 18 | if (j > 0) out << " "; 19 | out << as[j]; 20 | } 21 | out << std::endl; 22 | } 23 | 24 | int main() 25 | { 26 | gen02(cout); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Varena/Abwords.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ifstream in("abwords.in"); 8 | ofstream out("abwords.out"); 9 | 10 | int N, pw; 11 | 12 | in >> N; 13 | 14 | for ( pw = 1; pw <= N; pw <<= 1 ); 15 | 16 | out << "A"; 17 | 18 | if ( N % 2 ) 19 | for ( int i = 2; i <= 1 + pw / 2; ++i ) 20 | out << "B"; 21 | else 22 | for ( int i = 2; i <= 2 + pw / 4; ++i ) 23 | out << "B"; 24 | 25 | out << "\n"; 26 | 27 | for ( int i = 1; i <= pw - N; ++i ) 28 | out << "2"; 29 | 30 | for ( int i = 1; i <= N - pw / 2; ++i ) 31 | out << "21"; 32 | 33 | out << "\n"; 34 | 35 | in.close(); 36 | out.close(); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Varena/Antitir.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 1000000 + 1; 6 | 7 | int X[Nmax]; 8 | int Y[Nmax]; 9 | 10 | int N; 11 | long long sol; 12 | 13 | int modul(int a) 14 | { 15 | if (a < 0) 16 | return -a; 17 | else 18 | return a; 19 | } 20 | 21 | int main() 22 | { 23 | freopen("antitir.in", "r", stdin); 24 | freopen("antitir.out", "w", stdout); 25 | 26 | scanf("%d ", &N); 27 | 28 | for (int i = 1; i <= N; ++i) 29 | scanf("%d %d ", &X[i], &Y[i]); 30 | 31 | int M = (N + 1) / 2; 32 | 33 | nth_element(X + 1, X + M, X + N + 1); 34 | nth_element(Y + 1, Y + M, Y + N + 1); 35 | 36 | for (int i = 1; i <= N; ++i) 37 | { 38 | sol += modul(X[i] - X[M]); 39 | sol += modul(Y[i] - Y[M]); 40 | } 41 | 42 | cout << sol; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Varena/Bart.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 500000; 6 | 7 | int Z[Nmax + 1]; 8 | char str[Nmax + 1]; 9 | int N; 10 | 11 | int main() 12 | { 13 | ifstream in("bart.in"); 14 | ofstream out("bart.out"); 15 | 16 | in >> str; 17 | 18 | N = strlen( str ); 19 | 20 | int L = 0, R = 0; 21 | 22 | for ( int i = 1; i < N; ++i ) 23 | { 24 | if ( i <= R ) 25 | Z[i] = min( R - i + 1, Z[i - L] ); 26 | 27 | while ( i + Z[i] < N && str[ Z[i] ] == str[ i + Z[i] ] ) Z[i]++; 28 | 29 | if ( i + Z[i] - 1 > R ) 30 | { 31 | L = i; 32 | R = i + Z[i] - 1; 33 | } 34 | } 35 | 36 | int printed = 0; 37 | 38 | for ( int i = 0; i < N && !printed; ++i ) 39 | if ( i + Z[i] == N ) 40 | { 41 | for ( int j = 0; j < i; ++j ) 42 | out << str[j]; 43 | 44 | printed = 1; 45 | } 46 | 47 | if ( !printed ) 48 | { 49 | for ( int j = 0; j < N; ++j ) 50 | out << str[j]; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Varena/Costume.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 2e5 + 1; 6 | 7 | int aib[Nmax]; 8 | int N; 9 | 10 | inline int lsb( int x ) 11 | { 12 | return x & -x; 13 | } 14 | 15 | void update( int pos, int val ) 16 | { 17 | for ( int i = pos; i <= N; i += lsb( i ) ) 18 | aib[i] += val; 19 | } 20 | 21 | int query( int pos ) 22 | { 23 | int s = 0; 24 | 25 | for ( int i = pos; i >= 1; i -= lsb( i ) ) 26 | s += aib[i]; 27 | 28 | return s; 29 | } 30 | 31 | int query( int x, int y ) 32 | { 33 | return query( y ) - query( x - 1 ); 34 | } 35 | 36 | int cauta( int k ) 37 | { 38 | int init = k, pos = 0; 39 | 40 | for ( int step = ( 1 << 18 ); step; step >>= 1 ) 41 | { 42 | if ( pos + step <= N && aib[pos + step] < k ) 43 | { 44 | k -= aib[pos + step]; 45 | pos += step; 46 | } 47 | } 48 | 49 | if ( query( pos + 1 ) == init ) 50 | return pos + 1; 51 | else 52 | return -1; 53 | } 54 | 55 | 56 | int main() 57 | { 58 | ifstream in("costume.in"); 59 | ofstream out("costume.out"); 60 | 61 | in >> N; 62 | 63 | for ( int i = 1, a; i <= N; ++i ) 64 | { 65 | in >> a; 66 | update( a, 1 ); 67 | out << cauta( ( i + 1 ) / 2 ) << " "; 68 | } 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Varena/Latin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ifstream in("latin.in"); 8 | ofstream out("latin.out"); 9 | 10 | int N, L; 11 | in >> N; 12 | 13 | L = (1 << N); 14 | 15 | for (int i = 1; i <= L; ++i) 16 | { 17 | int aux = i; 18 | 19 | for (int k = 1; k <= L; ++k) 20 | { 21 | out << aux << " "; 22 | 23 | if ( ++aux > L ) 24 | aux -= L; 25 | } 26 | 27 | out << "\n"; 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Varena/Maxxor2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000 + 1; 6 | 7 | long long v[Nmax]; 8 | int N; 9 | 10 | int main() 11 | { 12 | ifstream in("maxxor2.in"); 13 | ofstream out("maxxor2.out"); 14 | 15 | ios_base::sync_with_stdio(false); 16 | 17 | in >> N; 18 | 19 | for ( int i = 1; i <= N; ++i ) 20 | in >> v[i]; 21 | 22 | sort(v + 1, v + N + 1, greater()); 23 | 24 | int i = 1, j = 63; 25 | 26 | while (i <= N && j >= 0) 27 | { 28 | int x = 0; 29 | 30 | for ( int k = i; k <= N; ++k ) 31 | if ( v[k] & (1LL << j) ) 32 | { 33 | x = k; 34 | break; 35 | } 36 | 37 | if ( !x ) 38 | { 39 | j--; 40 | continue; 41 | } 42 | 43 | swap(v[i], v[x]); 44 | 45 | for ( int k = 1; k <= N; ++k ) 46 | if ( k != i && v[k] & (1LL << j) ) 47 | v[k] ^= v[i]; 48 | 49 | i++; j--; 50 | } 51 | 52 | long long sol = 0; 53 | 54 | for ( int i = 1; i <= N; ++i ) 55 | sol ^= v[i]; 56 | 57 | out << sol << "\n"; 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Varena/Pointeri.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 200000; 6 | 7 | int st[Nmax], dr[Nmax]; 8 | int N; 9 | 10 | void inorder( int node, int &last, int &prim ) 11 | { 12 | if ( st[node] != -1 ) 13 | inorder( st[node], last, prim ); 14 | 15 | if ( last == -1 ) 16 | prim = node; 17 | else 18 | dr[last] = node; 19 | 20 | st[node] = last; 21 | last = node; 22 | 23 | if ( dr[node] != -1 ) 24 | inorder( dr[node], last, prim ); 25 | } 26 | 27 | int main() 28 | { 29 | FILE *f = fopen("pointeri.in", "r"); 30 | FILE *g = fopen("pointeri.out", "w"); 31 | 32 | int root, prim = -1, last = -1; 33 | 34 | fscanf(f, "%d %d\n", &N, &root); 35 | 36 | for ( int i = 0; i < N; ++i ) 37 | fscanf(f, "%d", &st[i]); 38 | 39 | for ( int i = 0; i < N; ++i ) 40 | fscanf(f, "%d", &dr[i]); 41 | 42 | inorder( root, last, prim ); 43 | 44 | fprintf(g, "%d\n", prim); 45 | 46 | for ( int i = 0; i < N; ++i ) 47 | fprintf(g, "%d ", st[i]); 48 | 49 | fprintf(g, "\n"); 50 | 51 | for ( int i = 0; i < N; ++i ) 52 | fprintf(g, "%d ", dr[i]); 53 | 54 | fprintf(g, "\n"); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Varena/Points2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long i64; 6 | 7 | const int Nmax = 36; 8 | 9 | i64 sume[1 << (Nmax / 2)]; 10 | i64 v[Nmax]; 11 | 12 | i64 S; 13 | int N; 14 | 15 | int main() 16 | { 17 | ifstream in("points2.in"); 18 | ofstream out("points2.out"); 19 | 20 | in >> N >> S; 21 | 22 | for ( int i = 0; i < N; ++i ) 23 | in >> v[i]; 24 | 25 | int n = N / 2; 26 | int ind = 0; 27 | 28 | for ( int i = 0; i < ( 1 << n ); ++i ) 29 | { 30 | i64 suma = 0; 31 | 32 | for ( int j = 0; j < n; ++j ) 33 | if ( i & ( 1 << j ) ) 34 | suma += v[j]; 35 | 36 | sume[ ind++ ] = suma; 37 | } 38 | 39 | sort( sume, sume + ind ); 40 | 41 | int prev = n; 42 | n = N - n; 43 | 44 | i64 sol = 0; 45 | 46 | for ( int i = 0; i < ( 1 << n ); ++i ) 47 | { 48 | i64 suma = 0; 49 | 50 | for ( int j = 0; j < n; ++j ) 51 | if ( i & ( 1 << j ) ) 52 | suma += v[prev + j]; 53 | 54 | int p = lower_bound( sume, sume + ind, S - suma ) - sume; 55 | 56 | if ( p != -1 ) 57 | sol += ind - p; 58 | } 59 | 60 | out << sol << "\n"; 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Varena/Păsări.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 100000; 6 | 7 | int aib[Nmax + 1]; 8 | int table[256]; 9 | 10 | int N, S, K; 11 | 12 | void initTable() 13 | { 14 | for ( int i = 0; i < 256; ++i ) 15 | table[i] = table[i / 2] + ( i & 1 ); 16 | } 17 | 18 | inline int popcount( int x ) 19 | { 20 | int s = 0; 21 | 22 | for ( int i = 0; i < 32; i += 8 ) 23 | s += table[ ( x >> i ) & 0xff ]; 24 | 25 | return s; 26 | } 27 | 28 | inline int lsb( int x ) 29 | { 30 | return x & ( -x ); 31 | } 32 | 33 | void update( int pos, int val ) 34 | { 35 | for ( int i = pos; i <= N; i += lsb( i ) ) 36 | aib[i] |= val; 37 | } 38 | 39 | int query( int pos ) 40 | { 41 | int s = 0; 42 | 43 | for ( int i = pos; i >= 1; i -= lsb( i ) ) 44 | s |= aib[i]; 45 | 46 | return s; 47 | } 48 | 49 | int main() 50 | { 51 | ifstream in("pasari.in"); 52 | ofstream out("pasari.out"); 53 | 54 | in >> N >> S >> K; 55 | 56 | initTable(); 57 | 58 | while ( K-- ) 59 | { 60 | int tip, s, x; 61 | 62 | in >> tip; 63 | 64 | if ( tip == 1 ) 65 | { 66 | in >> s >> x; 67 | s--; 68 | update( x, 1 << s ); 69 | } 70 | else 71 | { 72 | in >> x; 73 | out << popcount( query( x ) ) << "\n"; 74 | } 75 | } 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Varena/SCV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int C, T, M, X; 6 | 7 | int findDays( int nrKg, int nrRobots ) 8 | { 9 | if ( nrKg >= X ) 10 | return 0; 11 | 12 | int d = ( X - nrKg ) / ( nrRobots * M ); 13 | 14 | if ( ( X - nrKg ) % ( nrRobots * M ) ) 15 | d++; 16 | 17 | return d; 18 | } 19 | 20 | int greedy() 21 | { 22 | int nrKg = 0; 23 | int nrRobots = 1; 24 | int day = 0; 25 | 26 | while( nrKg < X ) 27 | { 28 | if ( nrKg < C ) 29 | { 30 | day++; 31 | nrKg += nrRobots * M; 32 | } 33 | else /// 2 optiuni 34 | { 35 | int limita = min( 1LL * ( 1LL * nrKg - 1LL * C + 1LL * T * M * nrRobots ), 1LL * X ); 36 | 37 | int c1 = findDays( nrKg, nrRobots ); 38 | int c2 = T + findDays( limita, nrRobots + 1 ); 39 | 40 | if ( c1 < c2 ) 41 | { 42 | day++; 43 | nrKg += nrRobots * M; 44 | } 45 | else 46 | { 47 | day += T; 48 | nrKg = limita; 49 | nrRobots++; 50 | } 51 | } 52 | } 53 | 54 | return day; 55 | } 56 | 57 | int main() 58 | { 59 | ifstream in("scv.in"); 60 | ofstream out("scv.out"); 61 | 62 | ios_base::sync_with_stdio( false ); 63 | 64 | in >> C >> T >> M >> X; 65 | out << greedy() << "\n"; 66 | 67 | in.close(); 68 | out.close(); 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Varena/Traseu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int Nmax = 500 + 1; 6 | 7 | int dp[2][Nmax]; 8 | int A; 9 | int N, M; 10 | 11 | int main() 12 | { 13 | ifstream in("traseu.in"); 14 | ofstream out("traseu.out"); 15 | 16 | ios_base::sync_with_stdio( false ); 17 | 18 | in >> N >> M; 19 | 20 | for ( int i = 1; i <= N; ++i ) 21 | { 22 | for ( int j = 1; j <= M; ++j ) 23 | { 24 | in >> A; 25 | dp[i & 1][j] = max( dp[i & 1 ^ 1][j], dp[i & 1][j - 1] ) + A; 26 | } 27 | } 28 | 29 | out << dp[N & 1][M] << "\n"; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /script.sh: -------------------------------------------------------------------------------- 1 | for filename in $1/*; do 2 | echo $filename 3 | 4 | if ! ([[ $filename = *".java"* ]] || [[ $filename = *".cpp"* ]]); then 5 | mv "$filename" "$filename.cpp" 6 | fi 7 | 8 | done 9 | --------------------------------------------------------------------------------