├── Material ├── Juniors Phase 1 │ ├── #01 - Static Range Querie │ │ ├── Session's codes │ │ │ ├── Count Sort.cpp │ │ │ ├── Prefix.cpp │ │ │ └── RMEXQ.cpp │ │ └── Static Range Queries.md │ ├── #02 - STL 1 │ │ └── STLS #1.md │ ├── #03 - STL 2 │ │ └── Week#3.md │ ├── #04 - Binary Search │ │ └── Week#4.md │ ├── #05 - Two Pointer │ │ └── Week#5.md │ └── Images │ │ ├── EmbeddedImage.png │ │ ├── InkedEmbeddedImage_LI.jpg │ │ ├── TimeComplexity.png │ │ ├── Two Pointer.webp │ │ ├── Untitled.png │ │ ├── Week3.png │ │ ├── counting-sort.png │ │ └── two_pointer.gif └── Juniors Phase 2 │ ├── Codes │ ├── Mo.cpp │ ├── SQRT_decom.cpp │ └── SQRT_decomp (Struct).cpp │ ├── DP with DS │ └── Increasing Subsequences.cpp │ ├── DP with Masks │ ├── Palindrome Free Strings(kickstart'22) .cpp │ └── TSP.cpp │ ├── DSU │ └── DSU-UNDO.cpp │ ├── Dynamic Programming.md │ ├── Merge Sort Tree │ ├── With-update.cpp │ └── without-update.cpp │ ├── Mo's - Sqrt decomposition.md │ ├── SegmenTree │ ├── Codeforces Edu │ │ ├── Part #1 │ │ │ ├── Setp #1 │ │ │ │ ├── A - Segment Tree for the Sum.cpp │ │ │ │ ├── B - Segment Tree for the Minimum.cpp │ │ │ │ └── C - Numberof Minimums on a Segment.cpp │ │ │ ├── Setp #2 │ │ │ │ ├── A - Segment with the Maximum Sum.cpp │ │ │ │ ├── B - K-th one.cpp │ │ │ │ ├── C - First element at least X.cpp │ │ │ │ └── D - First element at least X - 2.cpp │ │ │ └── Step #3 │ │ │ │ └── A - Inversions.cpp │ │ └── Part #2 │ │ │ ├── Step #1 │ │ │ ├── A - Addition to Segment.cpp │ │ │ ├── B - Applying MAX to Segment.cpp │ │ │ └── C - Assignment to Segment.cpp │ │ │ └── Step #2 │ │ │ ├── A - Addition and Minimum.cpp │ │ │ └── B - Multiplication and Sum.cpp │ └── Vjudge │ │ └── Can you answer these queries I.cpp │ └── Sparse Table.md ├── README.md ├── Solutions ├── ACPC-Kickoff-2022 │ ├── Kickoff-Contest │ │ ├── Problem A - Pizza time.cpp │ │ ├── Problem B- Reversing Strings.cpp │ │ ├── Problem C- Ali’s Schedule.cpp │ │ ├── Problem D- Make it K-good.cpp │ │ ├── Problem E- Wonder City.cpp │ │ ├── Problem F-Array Erasing.cpp │ │ ├── Problem G- Plan the event.cpp │ │ ├── Problem I- Moving Balls.cpp │ │ ├── Problem M- New Sorting Algorithm.cpp │ │ ├── Problem N- K-Subarrays.cpp │ │ ├── Problem O- Ziko The Rabbit.cpp │ │ └── statement (1).pdf │ └── practice contest │ │ ├── Problem A - The Walking Adam.cpp │ │ ├── Problem B- Duplicate Files.cpp │ │ ├── Problem C - Self Describing Numbers.cpp │ │ ├── Problem D- Guess Number.cpp │ │ └── statement.pdf ├── Battle of The Brains │ └── Contest #1 │ │ ├── A - Xorred .cpp │ │ ├── B - Casino .cpp │ │ ├── C - Cooperative Game on a Tree .cpp │ │ ├── D - Katya and the Broken Keyboard .cpp │ │ ├── E - Cookie Day .cpp │ │ ├── F - Three Suitcases .cpp │ │ ├── G - Scooter Numbers .cpp │ │ ├── H - Unalike Gcd & Lcm .cpp │ │ ├── I - Qual B .cpp │ │ ├── J - Count the Christmas Trees .cpp │ │ ├── K - Max MEX .cpp │ │ └── L - Make it Palindrome .cpp ├── Codeforces Gym Solutions │ ├── (Aleppo + HAIST + SVU + Private) CPC 2022 │ │ ├── B. Bored of Board Games.cpp │ │ ├── C. Perfect Seating.cpp │ │ ├── D. Fairplay.cpp │ │ ├── E. Robovac.cpp │ │ ├── F. A + B (Harder version).cpp │ │ ├── H. FAT Sequences.cpp │ │ ├── K. Movie Planning.cpp │ │ ├── L. ResliPhobia.cpp │ │ └── M. Permutations Score.cpp │ ├── 2014-2015 ACM-ICPC Pacific Northwest Regional Contest (Div. 2) │ │ ├── M - Polyhedra.cpp │ │ ├── N - Majority.cpp │ │ ├── O - Diamonds.cpp │ │ ├── R - Ramp Number.cpp │ │ ├── T - Runes.cpp │ │ ├── U - Top 25.cpp │ │ ├── W - Wormhole.cpp │ │ └── X - Wrench.cpp │ ├── 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest │ │ ├── A. Alex Origami Squares.cpp │ │ └── B. Black and White.cpp │ ├── 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16) │ │ ├── A. The game of Osho.cpp │ │ ├── D. Popcorn.cpp │ │ ├── E. Jumping.cpp │ │ ├── G. The Galactic Olympics.cpp │ │ ├── H. Commandos.cpp │ │ ├── J. Whistle's New Car [BackTracking].cpp │ │ └── J. Whistle's New Car [Merge Sort Tree].cpp │ ├── 2017 JUST Programming Contest 2.0 │ │ ├── A. On The Way to Lucky Plaza.cpp │ │ ├── B. So You Think You Can Count_ .cpp │ │ ├── C. MRT Map.cpp │ │ ├── D. Husam's Bug.cpp │ │ ├── E. Abdalrahman Ali Bugs.cpp │ │ ├── F. Certifications.cpp │ │ ├── G. In the Chairman's office.cpp │ │ ├── H. Give Me This Pizza.cpp │ │ ├── I. Husam and the Broken Present 1.cpp │ │ ├── J. Husam and the Broken Present 2.cpp │ │ └── K. Counting Time.cpp │ ├── 2018 BACS Contest Replay │ │ └── J - Non Super Boring Substring.cpp │ ├── 2018, XI Samara Regional Intercollegiate Programming Contest │ │ ├── A. Restoring Numbers.cpp │ │ ├── B. Minimal Area.cpp │ │ ├── C. Third-Party Software.cpp │ │ ├── E. Substring Reverse.cpp │ │ ├── F. Tree Restoration.cpp │ │ ├── H. Safe Path.cpp │ │ ├── J. Parallelograms.cpp │ │ ├── K. Video Reviews.cpp │ │ ├── L. Queries on a String.cpp │ │ └── M. Forgotten Spell.cpp │ ├── ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018) │ │ ├── B - Friends and Cookies.cpp │ │ ├── C - Flip the Bits.cpp │ │ ├── D - Magic Sticks.cpp │ │ ├── E - N-Dimensional Grid.cpp │ │ ├── F - Minimum Sum of Array.cpp │ │ ├── H - Making Friends.cpp │ │ ├── I - Split the Number.cpp │ │ └── M - Greedy Pirate.cpp │ ├── JU Flash Contest │ │ ├── A. N integers.cpp │ │ ├── B. Mahmoud the Thief.cpp │ │ ├── D. Coach Ayoub.cpp │ │ ├── E. New Max.cpp │ │ ├── G. ABC.cpp │ │ ├── I. Abu Tahun Mod problem.cpp │ │ ├── J. Negative effect.cpp │ │ ├── K. Scientest Ayoub (A).cpp │ │ ├── L. Scientist Ayoub (B).cpp │ │ └── M. Ahmad Jaber Rectangles.cpp │ └── NCD 2019 │ │ ├── A - Hasan the lazy judge.cpp │ │ ├── B - Let me sleep.cpp │ │ ├── C - Hasan and his lazy students.cpp │ │ ├── D - Football Cup.cpp │ │ ├── E - Adnan and the Burned drivers.cpp │ │ ├── F - Research projects.cpp │ │ ├── H - Mr. Hamra and his quantum particles.cpp │ │ ├── J - Bashar and daylight saving time.cpp │ │ ├── K - Masaoud LOVES PIZZAS.cpp │ │ ├── L - Chemistry Exam.cpp │ │ └── M - NCD Salary.cpp ├── ECPC 2024 - Are You Ready │ └── Team Contest #1 │ │ ├── A. Arrayland's Challenge.cpp │ │ ├── B. Bacterial Sampling.cpp │ │ ├── C. Chocolate Packing.cpp │ │ ├── E. Evaluating Linear Expressions.cpp │ │ ├── F. Factory TikTak Trend.cpp │ │ ├── G. Granitus Stone Towers.cpp │ │ ├── J. Journey To Stringland.cpp │ │ ├── K. Knights In The Board.cpp │ │ └── L. Lost Land of Numeralia.cpp └── Google-Kick-Start-2022 │ ├── Round A │ ├── Challenge Nine.cpp │ ├── Interesting Integers.cpp │ ├── Palindrome Free Strings.cpp │ └── Speed Typing.cpp │ ├── Round B │ ├── Infinity Area.cpp │ ├── Palindromic Factors.cpp │ └── Unlock the Padlock.cpp │ └── Round C │ ├── Ants on a Stick.cpp │ ├── New Password.cpp │ └── Range Partition.cpp └── templates ├── Adaptive Simpson's.cpp ├── Combinatorics ├── Catalan Numbers.pdf ├── Efficient combinations.cpp └── combinations O(N^2).cpp ├── DS ├── 2D sparse Table.cpp ├── BIT.cpp ├── BIT2DOffline.cpp ├── BIT2DWithUpdate.cpp ├── BIT2d.cpp ├── BITUpdateRange.cpp ├── BSFenwickTree.cpp ├── BinaryTrie.cpp ├── Hilbert Order .cpp ├── Kinetic Tournament.cpp ├── LineContainer.cpp ├── MO with DSU.cpp ├── MO with SQRT trick.cpp ├── MO with update.cpp ├── MergeSortTree (Fractional cascading) .cpp ├── MergeSortTree.cpp ├── Monotonic Queue.cpp ├── Multiset.cpp ├── Persistent Binary Trie.cpp ├── Persistent Segment Tree (Lazy).cpp ├── Persistent Segment Tree.cpp ├── SegTree.cpp ├── SegTree2D.cpp ├── SegTreeArithmeticProgressionSumQuery.cpp ├── SegTreeSD.cpp ├── SparsTable.cpp ├── Treap.cpp ├── order_set.cpp └── sqrt_array.cpp ├── Graph ├── BFS01.cpp ├── Bridges.cpp ├── DSU With Rollback.cpp ├── Dijkstra.cpp ├── Dinic.cpp ├── Euler Path Directed.cpp ├── Euler Path Undirected.cpp ├── Floyd-Warshall.cpp ├── HopcroftKarp.cpp ├── Hungarian.cpp ├── Kth Shortest Path.cpp ├── Kuhn.cpp ├── MCMF.cpp ├── PersistentDSU.cpp ├── SCC and TwoSat.cpp ├── SPFA.cpp └── Topological.cpp ├── Linear Algebra ├── MatPower.cpp └── XOR Basis.cpp ├── Number Theory ├── A List of Useful Equations in Competitive Programming _ Inception.pdf ├── Bazout's Identity.cpp ├── CRT.cpp ├── Chicken McNugget Theorem.txt ├── Discrete Root.cpp ├── DiscreteLog.cpp ├── Fibonacci Numbers.pdf ├── Floor Sum.cpp ├── Gauss - doubles.cpp ├── Gauss.cpp ├── Linear Congruence Equation.cpp ├── Linear Diophantine Equation.pdf ├── Linear Sieve and Mobius.cpp ├── MillerRabin.cpp ├── Phi.cpp ├── PollardRho.cpp ├── Power Tower.cpp ├── Prime power in N!.cpp ├── Primitive root.cpp ├── Segmented Sieve.cpp ├── Trivariable Linear Diophantine Equation.cpp └── cheat.pdf ├── Strings ├── Aho-Corasick.cpp ├── Hashing.cpp ├── KMP.cpp ├── Manacher.cpp ├── Palindromic Tree.cpp ├── SegTreeHashing.cpp ├── Suffix Array and LCP.cpp ├── SuffixAutomaton.cpp ├── Trie.cpp └── Z.cpp ├── Trees ├── Centroid Decomposition.cpp ├── HLD.cpp ├── LCA Euler tour.cpp ├── LCA.cpp ├── MO on path of tree.cpp ├── Small to large.cpp └── Tree Hashing.cpp └── Utils ├── Balanced_parentheses_count.cpp ├── Big Integer.cpp ├── Game.cpp ├── SubsetSum O( N SQRT).cpp ├── Tringle.cpp ├── fraction Class.cpp ├── kthRoot.cpp ├── stress.cpp └── unorderd_map O(1).cpp /Material/Juniors Phase 1/#01 - Static Range Querie/Session's codes/Count Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void count_sort(string &str) 6 | { 7 | int frq[26] = {0}; 8 | for (int i = 0; i < str.size(); ++i) 9 | { 10 | frq[str[i] - 'a']++; 11 | } 12 | int idx = 0; 13 | char cur = 'a'; 14 | for (int i = 0; i < 26; i++, cur++) 15 | { 16 | for (int j = 0; j < frq[i]; j++) 17 | { 18 | str[idx] = cur; 19 | ++idx; 20 | } 21 | } 22 | } 23 | 24 | int32_t main() 25 | { 26 | ios_base::sync_with_stdio(false); 27 | cin.tie(nullptr), cout.tie(nullptr); 28 | string str; 29 | cin >> str; 30 | count_sort(str); 31 | cout << str << '\n'; 32 | } 33 | -------------------------------------------------------------------------------- /Material/Juniors Phase 1/#01 - Static Range Querie/Session's codes/Prefix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int32_t main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr), cout.tie(nullptr); 9 | string s; 10 | int n, q, l, r; 11 | char ch; 12 | cin >> n >> s >> q; 13 | 14 | int arr[28][n]; 15 | for (int i = 0; i < 28; ++i) 16 | { 17 | for (int j = 0; j < n; ++j) 18 | { 19 | arr[i][j] = 0; 20 | } 21 | } 22 | 23 | for (int i = 0; i < n; ++i) 24 | { 25 | //// s[i] = 'c' - 'a' 26 | /// arr[0][i] = 1 27 | ch = s[i] - 'a'; 28 | arr[ch][i] = 1; 29 | } 30 | 31 | for (int i = 0; i < 28; ++i) 32 | { 33 | for (int j = 1; j < n; ++j) 34 | { 35 | arr[i][j] += arr[i][j - 1]; 36 | } 37 | } 38 | 39 | while (q--) 40 | { 41 | cin >> l >> r >> ch; 42 | int res = arr[ch - 'a'][r] - arr[ch - 'a'][l - 1]; 43 | cout << res << '\n'; 44 | } 45 | } -------------------------------------------------------------------------------- /Material/Juniors Phase 1/#01 - Static Range Querie/Session's codes/RMEXQ.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int min(int a, int b) 5 | { 6 | if (a < b) 7 | return a; 8 | return b; 9 | } 10 | 11 | const int N = 1e5 + 5; 12 | int arr[N], prefix[N], suffix[N]; 13 | 14 | int32_t main() 15 | { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(nullptr), cout.tie(nullptr); 18 | int n; 19 | cin >> n; 20 | prefix[0] = arr[0]; 21 | suffix[n - 1] = arr[n - 1]; 22 | for (int i = 1; i < n; ++i) 23 | { 24 | prefix[i] = min(arr[i], prefix[i - 1]); 25 | } 26 | for (int i = n - 2; i >= 0; --i) 27 | { 28 | suffix[i] = min(arr[i], suffix[i + 1]); 29 | } 30 | 31 | int q; 32 | cin >> q; 33 | int l, r; 34 | while (q--) 35 | { 36 | cin >> l >> r; 37 | int res = n + 1; 38 | if (l > 0) 39 | { 40 | res = prefix[l - 1]; 41 | } 42 | if (r + 1 < n) 43 | { 44 | res = min(res, suffix[r + 1]); 45 | } 46 | cout << res << '\n'; 47 | } 48 | } -------------------------------------------------------------------------------- /Material/Juniors Phase 1/#03 - STL 2/Week#3.md: -------------------------------------------------------------------------------- 1 | # Week #3 2 | 3 | ![Image](../Images/InkedEmbeddedImage_LI.jpg) 4 | 5 | #### As you can see we finished half of them and now we continue ... 6 | 7 | ## Pointers 8 | * pointer part 1 : [video](https://youtu.be/f7I4cnJ5KJo) 9 | * pointer part 2 : [video](https://youtu.be/ibSjFL607QA) 10 | 11 | --- 12 | 13 | ## 1- Stack 14 | * Video : [link](https://www.youtube.com/watch?v=9r7IDtX5KS4) 15 | * Blog : [link](https://www.geeksforgeeks.org/stack-in-cpp-stl/) 16 | --- 17 | ## 2- Queue 18 | * Video : [link](https://www.youtube.com/watch?v=iLJXB9Daeq8) 19 | * Blog : [link](https://www.geeksforgeeks.org/queue-cpp-stl/) 20 | --- 21 | ## 3- Priority queue 22 | * Video : [link](https://www.youtube.com/watch?v=0zr0JqSw7ic) 23 | * Blog : [link](https://www.geeksforgeeks.org/priority-queue-in-cpp-stl/) 24 | --- 25 | ## 4- List 26 | * Video : [link](https://www.youtube.com/watch?v=U2oXdm4PfeQ) 27 | * Blog : [link](https://www.geeksforgeeks.org/list-cpp-stl/) 28 | --- 29 | ## 5- Forward list 30 | * Video : [link](https://www.youtube.com/watch?v=hp7BCTdEDFs) 31 | * Blog : [link](https://www.geeksforgeeks.org/forward-list-c-set-1-introduction-important-functions/) 32 | --- 33 | 34 | ## By this session we finished sequence containers and container adaptors .. the associative containers are left to teh next session 35 | 36 | -------------------------------------------------------------------------------- /Material/Juniors Phase 1/#04 - Binary Search/Week#4.md: -------------------------------------------------------------------------------- 1 | 2 | # Week #4 3 | 4 | ## 1- Thinking Techniques Tips: 5 | #### important videos : 6 | * [Incremental Thinking](https://youtu.be/5zILiqyQ2ts) 7 | 8 | * [Problem Simplification](https://youtu.be/x1rCxxKfFbM) 9 | 10 | ## 2- Searching Techniques - Binary Search: 11 | 12 | * [Dr. Mostafa Saad - Binary Search (Arabic)](https://youtu.be/2G7RzlxTNPo) 13 | 14 | * [Errichto - Binary Search](https://youtu.be/olcmPKZNqnM) 15 | 16 | * [TopCoder Binary Search](https://www.topcoder.com/thrive/articles/Binary%20Search) 17 | 18 | * there's also course in [Codeforces EDU](https://codeforces.com/edu/courses) about binary search you can find it throw : [ITMO Courses](https://codeforces.com/edu/course/2/lesson/6) 19 | -------------------------------------------------------------------------------- /Material/Juniors Phase 1/#05 - Two Pointer/Week#5.md: -------------------------------------------------------------------------------- 1 | 2 | # Week #5 3 | 4 | ## 1- Two Pointer Technique: 5 | 6 | ![Image](Images/two_pointer.gif) 7 | 8 | * [Two Pointer Technique](https://youtu.be/n-Xwrr8RFQ0) 9 | 10 | * [Quora - Two Pointer Technique](https://www.quora.com/profile/Abhijit-Dixit-6/The-Two-Pointer-Algorithm) 11 | 12 | * [G4G - Two Pointer Technique](https://www.geeksforgeeks.org/window-sliding-technique) 13 | 14 | * there's also course in [Codeforces EDU](https://codeforces.com/edu/courses) about Two Pointer you can find it throw : [ITMO Courses](https://codeforces.com/edu/course/2/lesson/9) 15 | -------------------------------------------------------------------------------- /Material/Juniors Phase 1/Images/EmbeddedImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Material/Juniors Phase 1/Images/EmbeddedImage.png -------------------------------------------------------------------------------- /Material/Juniors Phase 1/Images/InkedEmbeddedImage_LI.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Material/Juniors Phase 1/Images/InkedEmbeddedImage_LI.jpg -------------------------------------------------------------------------------- /Material/Juniors Phase 1/Images/TimeComplexity.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Material/Juniors Phase 1/Images/TimeComplexity.png -------------------------------------------------------------------------------- /Material/Juniors Phase 1/Images/Two Pointer.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Material/Juniors Phase 1/Images/Two Pointer.webp -------------------------------------------------------------------------------- /Material/Juniors Phase 1/Images/Untitled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Material/Juniors Phase 1/Images/Untitled.png -------------------------------------------------------------------------------- /Material/Juniors Phase 1/Images/Week3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Material/Juniors Phase 1/Images/Week3.png -------------------------------------------------------------------------------- /Material/Juniors Phase 1/Images/counting-sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Material/Juniors Phase 1/Images/counting-sort.png -------------------------------------------------------------------------------- /Material/Juniors Phase 1/Images/two_pointer.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Material/Juniors Phase 1/Images/two_pointer.gif -------------------------------------------------------------------------------- /Material/Juniors Phase 2/Codes/Mo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | const int N = 1e5 + 5; 5 | int arr[N], ret[N]; 6 | int SQ, cur; 7 | 8 | struct Query { 9 | int l, r, i, b; 10 | 11 | Query() {} 12 | 13 | Query(int l, int r, int i) : l(l), r(r), i(i), b(l / SQ) {} 14 | 15 | bool operator<(const Query &x) const { 16 | //// division is much slower than other arithmetical operations 17 | ///return make_pair(l / SQ, r) < make_pair(x.l / SQ, x.r); 18 | 19 | return b != x.b ? b < x.b : r < x.r; 20 | } 21 | }; 22 | 23 | void add(int i) { 24 | cur += arr[i]; 25 | } 26 | 27 | void remove(int i) { 28 | cur -= arr[i]; 29 | } 30 | 31 | void solve(vector &queries) { 32 | sort(queries.begin(), queries.end()); 33 | int l = 0, r = 0; 34 | add(0); 35 | for (auto &q: queries) { //// call add() before remove() 36 | while (r < q.r) 37 | add(++r); 38 | while (q.l < l) 39 | add(--l); 40 | while (q.r < r) 41 | remove(r--); 42 | while (l < q.l) 43 | remove(l++); 44 | ret[q.i] = cur; 45 | } 46 | 47 | 48 | } 49 | 50 | signed main() { 51 | int n, q; 52 | cin >> n >> q; 53 | 54 | SQ = ceil(sqrt(n)); //// set the SQ 55 | 56 | 57 | for (int i = 0; i < n; ++i) { 58 | cin >> arr[i]; 59 | } 60 | vector queris; 61 | queris.reserve(q); ///// https://cplusplus.com/reference/vector/vector/reserve 62 | for (int i = 0, l, r; i < q; ++i) { 63 | cin >> l >> r; 64 | queris.emplace_back(--l, --r, i); /// zero or 1 based ? 65 | } 66 | solve(queris); 67 | for (int i = 0; i < q; ++i) { 68 | cout << ret[i] << '\n'; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/DP with DS/Increasing Subsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | const int N = 100007, mod = 5'000'000; 6 | 7 | int add(int a, int b) { 8 | a += b; 9 | if (a >= mod)a -= mod; 10 | return a; 11 | } 12 | 13 | int bit[80][N + 7]; 14 | int sz; 15 | 16 | void update(int i, int v) { 17 | while (i <= N) { 18 | bit[sz][i] = add(bit[sz][i], v); 19 | i += (i & -i); 20 | } 21 | } 22 | 23 | int query(int i) { 24 | int ret = 0; 25 | while (i > 0) { 26 | ret = add(ret, bit[sz][i]); 27 | i -= (i & -i); 28 | } 29 | return ret; 30 | } 31 | 32 | signed main() { 33 | ios_base::sync_with_stdio(false); 34 | cout.tie(nullptr), cin.tie(nullptr); 35 | int n, k; 36 | cin >> n >> k; 37 | int arr[n]; 38 | for (int &i: arr) { 39 | cin >> i, i += 2; 40 | } 41 | int a[n]; 42 | for (int i = 0; i < n; ++i) { 43 | a[i] = query(arr[i] - 1); 44 | update(arr[i], 1); 45 | } 46 | for (sz = 1; sz < k - 1; ++sz) { 47 | for (int j = 0; j < n; ++j) { 48 | int x = a[j]; 49 | a[j] = query(arr[j] - 1); 50 | update(arr[j], x); 51 | } 52 | } 53 | cout << accumulate(a, a + n, 0LL, add) << '\n'; 54 | } 55 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/DP with Masks/Palindrome Free Strings(kickstart'22) .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | const int N = 5e4 + 1; 5 | int n; 6 | string s; 7 | int mem[1 << 5][N]; 8 | int vis[1 << 5][N], vs; 9 | 10 | bool check(int l, int r, int msk) { 11 | while (l < r) { 12 | if (((msk >> l) & 1) != ((msk >> r) & 1)) 13 | return true; 14 | ++l, --r; 15 | } 16 | return false; 17 | } 18 | 19 | int dp(int i, int m) { 20 | if (i == n)return 1; 21 | int &ret = mem[m][i]; 22 | if (vis[m][i] == vs)return ret; 23 | vis[m][i] = vs; 24 | if (s[i] == '?') { 25 | m <<= 1; 26 | if ((i < 4 || check(0, 4, m)) && (i < 5 || check(0, 5, m))) { 27 | if (dp(i + 1, m & ((1 << 5) - 1))) 28 | return ret = 1; 29 | } 30 | m |= 1; 31 | if ((i < 4 || check(0, 4, m)) && (i < 5 || check(0, 5, m))) { 32 | m &= ((1 << 5) - 1); 33 | if (dp(i + 1, m)) 34 | return ret = 1; 35 | } 36 | } else { 37 | m <<= 1; 38 | m |= (s[i] - '0'); 39 | if ((i < 4 || check(0, 4, m)) && (i < 5 || check(0, 5, m))) { 40 | m &= ((1 << 5) - 1); 41 | if (dp(i + 1, m)) 42 | return ret = 1; 43 | } 44 | } 45 | return ret = 0; 46 | } 47 | 48 | void solve() { 49 | cin >> n >> s; 50 | cout << (dp(0, 0) ? "POSSIBLE" : "IMPOSSIBLE"); 51 | } 52 | 53 | int32_t main() { 54 | std::ios_base::sync_with_stdio(false); 55 | cin.tie(nullptr), cout.tie(nullptr); 56 | int tc; 57 | cin >> tc; 58 | for (vs = 1; vs <= tc; ++vs) { 59 | cout << "Case #" << vs << ": "; 60 | solve(); 61 | if (vs != tc)cout << '\n'; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/DP with Masks/TSP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | vector> a; 5 | int n, k; 6 | int full; 7 | 8 | double mem[19][(1 << 18) + 5]; 9 | 10 | double dis(int i, int j) { 11 | return ::hypot(a[i][0] - a[j][0], a[i][1] - a[j][1]); 12 | } 13 | 14 | double dp(int i, int msk) { 15 | if (msk == full)return dis(i, k); 16 | double &ret = mem[i][msk]; 17 | if (ret == ret)return ret; 18 | ret = 0; 19 | for (int j = 0; j < n; ++j) { 20 | if (msk & (1 << j))continue; 21 | ret = min( 22 | ret, 23 | dp(j, msk | (1 << j)) + dis(i, j) 24 | ); 25 | } 26 | return ret; 27 | } 28 | 29 | 30 | int32_t main() { 31 | std::ios_base::sync_with_stdio(false); 32 | cin.tie(nullptr), cout.tie(nullptr); 33 | memset(mem, -1, sizeof mem); 34 | cin >> n >> k, --k; 35 | full = (1 << n) - 1; 36 | a.resize(n); 37 | for (auto &[x, y]: a)cin >> x >> y; 38 | cout << dp(k, 1 << k); 39 | } 40 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/Dynamic Programming.md: -------------------------------------------------------------------------------- 1 | 2 | ## 1- Sources: 3 | 4 | #### Week #1 - (Knapsack, Subset styles, Building Output) 5 | 6 | * [Introduction - 1](https://youtu.be/gFdP6X4CyKU) 7 | 8 | * [Introduction - 2](https://youtu.be/1j3srLj-C5Q) 9 | 10 | * [Subset Style](https://youtu.be/vAqaki1BhS0) 11 | 12 | * [Building Output](https://youtu.be/s3IGwpJwCTA) 13 | 14 | * [stack overflow](https://youtu.be/pJbeTrSKl3Y?list=PLPt2dINI2MIattDutu7IOAMlUuLeN8k2p&t=388) - watch from 06:30- 15 | 16 | * [Ad-hoc - Memoizing in maps](https://youtu.be/gGEHeDdcNRs) 17 | 18 | #### Week #2 (Ranges) 19 | 20 | * [Consecutive Ranges Style](https://youtu.be/bDlAqeWsKsg) 21 | 22 | * [Nested Ranges Style](https://youtu.be/b4AC2jGNGEM) 23 | 24 | * [General Ranges Style](https://youtu.be/pJbeTrSKl3Y) 25 | 26 | #### Week #3 (Counting, Digits) 27 | 28 | * [Counting](https://youtu.be/lE09Ss_Sy0A) 29 | 30 | * [Digit DP Introduction](https://youtu.be/1--8_0bOHwA) 31 | 32 | * [Digit DP Phase 1](https://youtu.be/3yG5OJMw3VM) 33 | 34 | * [Digit DP Phase 2](https://youtu.be/dQ99enW770E) 35 | 36 | * [Digit DP Phase 3](https://youtu.be/9YbxYnsp4BY) 37 | 38 | * [Digit DP Codeforces](https://codeforces.com/blog/entry/53960) 39 | 40 | * [Introduction to Digit DP Codeforces](https://codeforces.com/blog/entry/84928) 41 | 42 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/Mo's - Sqrt decomposition.md: -------------------------------------------------------------------------------- 1 | 2 | ## 1- Sources: 3 | 4 | #### 1- Sqrt Decomposition : 5 | 6 | * [Code](/Material/Juniors%20Phase%202/Codes/SQRT_decom.cpp) - [using Struct](/Material/Juniors%20Phase%202/Codes/SQRT_decomp%20(Struct).cpp) 7 | 8 | * [Square root decomposition and applications](https://codeforces.com/blog/entry/83248) 9 | 10 | * [cp-algorithms](https://cp-algorithms.com/data_structures/sqrt_decomposition.html) 11 | 12 | * [anudeep2011 Blog](https://blog.anudeep2011.com/mos-algorithm) 13 | 14 | #### 2- Mo's algorithm : 15 | 16 | * [Code (from Session)](/Material/Juniors%20Phase%202/Codes/Mo.cpp) 17 | 18 | * [hackerearth](https://www.hackerearth.com/practice/notes/mos-algorithm/#:~:text=Mo's%20algorithm%20is%20a%20generic,R%5D%20as%20its%20argument.) 19 | 20 | * [Avoiding logs in Mo's algorithm when we use set](https://codeforces.com/blog/entry/100910#:~:text=2.%20Avoiding%20logs%20in%20Mo%27s%20algorithm) 21 | 22 | #### 3- [Coordinate Compression Technique](https://youtu.be/nqJIXtfs7p4) 23 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/SegmenTree/Codeforces Edu/Part #1/Setp #1/A - Segment Tree for the Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | struct SegmentTree { 7 | private: 8 | int sz; 9 | vector seg; 10 | 11 | int op(int a, int b) { 12 | return a + b; 13 | } 14 | 15 | void update(int i, int v, int x, int lx, int rx) { 16 | if (lx == rx) { 17 | seg[x] = v; 18 | return; 19 | } 20 | int m = (rx + lx) >> 1; 21 | if (i <= m) { /// go left 22 | update(i, v, x * 2 + 1, lx, m); 23 | } else { /// go right 24 | update(i, v, x * 2 + 2, m + 1, rx); 25 | } 26 | seg[x] = op(seg[x * 2 + 1], seg[x * 2 + 2]); 27 | } 28 | 29 | int query(int l, int r, int x, int lx, int rx) { 30 | if (r < lx || rx < l) return 0; 31 | if (l <= lx && rx <= r)return seg[x]; 32 | int m = (rx + lx) >> 1; 33 | return op( 34 | query(l, r, x * 2 + 1, lx, m), 35 | query(l, r, x * 2 + 2, m + 1, rx) 36 | ); 37 | } 38 | 39 | public: 40 | SegmentTree(int n) { 41 | sz = 1; 42 | while (sz < n)sz <<= 1; 43 | seg = vector(sz << 1); 44 | } 45 | 46 | void update(int i, int v) { 47 | update(i, v, 0, 0, sz - 1); 48 | } 49 | 50 | int query(int l, int r) { 51 | return query(l, r, 0, 0, sz - 1); 52 | } 53 | 54 | }; 55 | 56 | 57 | signed main() { 58 | ios_base::sync_with_stdio(false); 59 | cout.tie(nullptr), cin.tie(nullptr); 60 | int n, m; 61 | cin >> n >> m; 62 | SegmentTree seg(n); 63 | int x; 64 | for (int i = 0; i < n; ++i) { 65 | cin >> x; 66 | seg.update(i, x); 67 | } 68 | int ty, l, r; 69 | while (m--) { 70 | cin >> ty >> l >> r; 71 | if (ty == 1) { 72 | seg.update(l, r); 73 | } else { 74 | cout << seg.query(l, r - 1) << '\n'; 75 | } 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/SegmenTree/Codeforces Edu/Part #1/Setp #2/B - K-th one.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | struct SegmentTree { 7 | private: 8 | #define node int 9 | int sz; 10 | vector seg; 11 | 12 | node op(node a, node b) { 13 | return a + b; 14 | } 15 | 16 | void update(int i, int x, int lx, int rx) { 17 | if (lx == rx) { 18 | seg[x] ^= 1; 19 | return; 20 | } 21 | int m = (rx + lx) >> 1; 22 | if (i <= m) { /// go left 23 | update(i, x * 2 + 1, lx, m); 24 | } else { /// go right 25 | update(i, x * 2 + 2, m + 1, rx); 26 | } 27 | seg[x] = op(seg[x * 2 + 1], seg[x * 2 + 2]); 28 | } 29 | 30 | node query(int K, int x, int lx, int rx) { 31 | if (lx == rx) { 32 | return lx; 33 | } 34 | int lf = seg[x * 2 + 1]; 35 | int m = (rx + lx) >> 1; 36 | if (K <= lf) { 37 | return query(K, x * 2 + 1, lx, m); 38 | } else { 39 | return query(K - lf, x * 2 + 2, m + 1, rx); 40 | } 41 | } 42 | 43 | public: 44 | SegmentTree(int n) { 45 | sz = 1; 46 | while (sz < n)sz <<= 1; 47 | seg = vector(sz << 1); 48 | } 49 | 50 | void update(int i) { 51 | update(i, 0, 0, sz - 1); 52 | } 53 | 54 | int query(int k) { 55 | return query(k, 0, 0, sz - 1); 56 | } 57 | 58 | }; 59 | 60 | 61 | signed main() { 62 | ios_base::sync_with_stdio(false); 63 | cout.tie(nullptr), cin.tie(nullptr); 64 | int n, m; 65 | cin >> n >> m; 66 | SegmentTree seg(n); 67 | for (int i = 0, x; i < n; ++i) { 68 | cin >> x; 69 | if (x) seg.update(i); 70 | } 71 | 72 | int ty, l; 73 | while (m--) { 74 | cin >> ty >> l; 75 | if (ty == 1) { 76 | seg.update(l); 77 | } else { 78 | cout << seg.query(l + 1) << '\n'; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/SegmenTree/Codeforces Edu/Part #1/Setp #2/D - First element at least X - 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | struct SegmentTree { 7 | private: 8 | #define node int 9 | int sz; 10 | vector seg; 11 | 12 | node op(node a, node b) { 13 | return max(a, b); 14 | } 15 | 16 | void update(int i, int v, int x, int lx, int rx) { 17 | if (lx == rx) { 18 | seg[x] = v; 19 | return; 20 | } 21 | int m = (rx + lx) >> 1; 22 | if (i <= m) { /// go left 23 | update(i, v, x * 2 + 1, lx, m); 24 | } else { /// go right 25 | update(i, v, x * 2 + 2, m + 1, rx); 26 | } 27 | seg[x] = op(seg[x * 2 + 1], seg[x * 2 + 2]); 28 | } 29 | 30 | node query(int v, int i, int x, int lx, int rx) { 31 | if (seg[x] < v || i > rx)return -1; 32 | if (rx == lx) return lx; 33 | int m = (lx + rx) >> 1; 34 | int res = query(v, i, x * 2 + 1, lx, m); 35 | if (res != -1)return res; 36 | return query(v, i, x * 2 + 2, m + 1, rx); 37 | } 38 | 39 | public: 40 | SegmentTree(int n) { 41 | sz = 1; 42 | while (sz < n)sz <<= 1; 43 | seg = vector(sz << 1); 44 | } 45 | 46 | void update(int i, int x) { 47 | update(i, x, 0, 0, sz - 1); 48 | } 49 | 50 | int query(int k, int i) { 51 | return query(k, i, 0, 0, sz - 1); 52 | } 53 | 54 | }; 55 | 56 | 57 | signed main() { 58 | ios_base::sync_with_stdio(false); 59 | cout.tie(nullptr), cin.tie(nullptr); 60 | int n, m; 61 | cin >> n >> m; 62 | SegmentTree seg(n); 63 | for (int i = 0, x; i < n; ++i) { 64 | cin >> x; 65 | seg.update(i, x); 66 | } 67 | int ty, i, x; 68 | while (m--) { 69 | cin >> ty >> i >> x; 70 | if (ty == 1) { 71 | seg.update(i, x); 72 | } else { 73 | cout << seg.query(i, x) << '\n'; 74 | } 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/SegmenTree/Codeforces Edu/Part #1/Step #3/A - Inversions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct SegmentTree { 6 | private: 7 | #define node long long 8 | 9 | vector seg; 10 | int sz; 11 | 12 | node opt(node lft, node rt) { 13 | return lft + rt; 14 | } 15 | 16 | void update(int i, int x, int lx, int rx) { 17 | if (rx == lx) { 18 | seg[x] = 1; 19 | return; 20 | } 21 | int m = (rx + lx) >> 1; 22 | if (i <= m) { 23 | update(i, x * 2 + 1, lx, m); 24 | } else { 25 | update(i, x * 2 + 2, m + 1, rx); 26 | } 27 | seg[x] = opt(seg[x * 2 + 1], seg[x * 2 + 2]); 28 | } 29 | 30 | int query(int i, int x, int lx, int rx) { 31 | if (rx < i)return 0; 32 | if (i <= lx)return seg[x]; 33 | int m = (rx + lx) >> 1; 34 | return opt( 35 | query(i, x * 2 + 1, lx, m), 36 | query(i, x * 2 + 2, m + 1, rx) 37 | ); 38 | } 39 | 40 | public: 41 | SegmentTree(int n) { 42 | sz = 1; 43 | while (sz < n)sz <<= 1; 44 | seg = vector(sz << 1); 45 | } 46 | 47 | void update(int i) { 48 | update(i, 0, 0, sz - 1); 49 | } 50 | 51 | int query(int i) { 52 | return query(i, 0, 0, sz - 1); 53 | } 54 | 55 | #undef node 56 | }; 57 | 58 | signed main() { 59 | ios_base::sync_with_stdio(false); 60 | cout.tie(nullptr), cin.tie(nullptr); 61 | int n; 62 | cin >> n; 63 | int x; 64 | SegmentTree seg(n); 65 | for (int i = 0; i < n; ++i) { 66 | cin >> x; 67 | cout << seg.query(--x) << ' '; 68 | seg.update(x); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/SegmenTree/Codeforces Edu/Part #2/Step #1/A - Addition to Segment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | struct SegmentTree { 7 | private: 8 | vector seg; 9 | int sz; 10 | 11 | void update(int l, int r, int v, int x, int lx, int rx) { 12 | if (l <= lx && rx <= r) { 13 | seg[x] += v; 14 | return; 15 | } 16 | if (r < lx || rx < l)return; 17 | int m = (rx + lx) >> 1; 18 | update(l, r, v, x * 2 + 1, lx, m); 19 | update(l, r, v, x * 2 + 2, m + 1, rx); 20 | } 21 | 22 | int query(int i, int x, int lx, int rx) { 23 | if (rx == lx) { 24 | return seg[x]; 25 | } 26 | int m = (rx + lx) >> 1; 27 | if (i <= m) { 28 | return seg[x] + query(i, x * 2 + 1, lx, m); 29 | } 30 | return seg[x] + query(i, x * 2 + 2, m + 1, rx); 31 | } 32 | 33 | public: 34 | 35 | SegmentTree(int n) { 36 | sz = 1; 37 | while (sz < n)sz <<= 1; 38 | seg = vector(sz << 1); 39 | } 40 | 41 | void update(int l, int r, int v) { 42 | update(l, r, v, 0, 0, sz - 1); 43 | } 44 | 45 | int query(int i) { 46 | return query(i, 0, 0, sz - 1); 47 | } 48 | 49 | 50 | }; 51 | 52 | 53 | signed main() { 54 | ios_base::sync_with_stdio(false); 55 | cout.tie(nullptr), cin.tie(nullptr); 56 | int n, q; 57 | cin >> n >> q; 58 | SegmentTree seg(n); 59 | int l, r, ty, v; 60 | while (q--) { 61 | cin >> ty; 62 | if (ty == 1) { 63 | cin >> l >> r >> v; 64 | seg.update(l, --r, v); 65 | } else { 66 | cin >> l; 67 | cout << seg.query(l) << '\n'; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/SegmenTree/Codeforces Edu/Part #2/Step #1/B - Applying MAX to Segment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | struct SegmentTree { 7 | private: 8 | vector seg; 9 | int sz; 10 | 11 | void update(int l, int r, int v, int x, int lx, int rx) { 12 | if (l <= lx && rx <= r) { 13 | seg[x] = max(v, seg[x]); 14 | return; 15 | } 16 | if (r < lx || rx < l)return; 17 | int m = (rx + lx) >> 1; 18 | update(l, r, v, x * 2 + 1, lx, m); 19 | update(l, r, v, x * 2 + 2, m + 1, rx); 20 | } 21 | 22 | int query(int i, int x, int lx, int rx) { 23 | if (rx == lx) { 24 | return seg[x]; 25 | } 26 | int m = (rx + lx) >> 1; 27 | if (i <= m) { 28 | return max(seg[x], query(i, x * 2 + 1, lx, m)); 29 | } 30 | return max(seg[x], query(i, x * 2 + 2, m + 1, rx)); 31 | } 32 | 33 | public: 34 | 35 | SegmentTree(int n) { 36 | sz = 1; 37 | while (sz < n)sz <<= 1; 38 | seg = vector(sz << 1); 39 | } 40 | 41 | void update(int l, int r, int v) { 42 | update(l, r, v, 0, 0, sz - 1); 43 | } 44 | 45 | int query(int i) { 46 | return query(i, 0, 0, sz - 1); 47 | } 48 | 49 | 50 | }; 51 | 52 | 53 | signed main() { 54 | ios_base::sync_with_stdio(false); 55 | cout.tie(nullptr), cin.tie(nullptr); 56 | int n, q; 57 | cin >> n >> q; 58 | SegmentTree seg(n); 59 | int l, r, ty, v; 60 | while (q--) { 61 | cin >> ty; 62 | if (ty == 1) { 63 | cin >> l >> r >> v; 64 | seg.update(l, --r, v); 65 | } else { 66 | cin >> l; 67 | cout << seg.query(l) << '\n'; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/SegmenTree/Codeforces Edu/Part #2/Step #1/C - Assignment to Segment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | int timer; 6 | 7 | struct SegmentTree { 8 | private: 9 | vector> seg; 10 | int sz; 11 | 12 | void update(int l, int r, int v, int x, int lx, int rx) { 13 | if (l <= lx && rx <= r) { 14 | seg[x] = {timer, v}; 15 | return; 16 | } 17 | if (r < lx || rx < l)return; 18 | int m = (rx + lx) >> 1; 19 | update(l, r, v, x * 2 + 1, lx, m); 20 | update(l, r, v, x * 2 + 2, m + 1, rx); 21 | } 22 | 23 | pair query(int i, int x, int lx, int rx) { 24 | if (rx == lx) return seg[x]; 25 | int m = (rx + lx) >> 1; 26 | if (i <= m) { 27 | return max(seg[x], query(i, x * 2 + 1, lx, m)); 28 | } 29 | return max(seg[x], query(i, x * 2 + 2, m + 1, rx)); 30 | } 31 | 32 | public: 33 | 34 | SegmentTree(int n) { 35 | sz = 1; 36 | while (sz < n)sz <<= 1; 37 | seg = vector>(sz << 1); 38 | } 39 | 40 | void update(int l, int r, int v) { 41 | ++timer; 42 | update(l, r, v, 0, 0, sz - 1); 43 | } 44 | 45 | int query(int i) { 46 | return query(i, 0, 0, sz - 1).second; 47 | } 48 | 49 | 50 | }; 51 | 52 | 53 | signed main() { 54 | ios_base::sync_with_stdio(false); 55 | cout.tie(nullptr), cin.tie(nullptr); 56 | int n, q; 57 | cin >> n >> q; 58 | SegmentTree seg(n); 59 | int l, r, ty, v; 60 | while (q--) { 61 | cin >> ty; 62 | if (ty == 1) { 63 | cin >> l >> r >> v; 64 | seg.update(l, --r, v); 65 | } else { 66 | cin >> l; 67 | cout << seg.query(l) << '\n'; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Material/Juniors Phase 2/Sparse Table.md: -------------------------------------------------------------------------------- 1 | 2 | ## 1- Sources: 3 | 4 | #### Week #4 - (Sparse table) - Static queries 5 | 6 | * [Awesome Explanation (CP algo)](https://cp-algorithms.com/data_structures/sparse-table.html#range-minimum-queries-rmq) 7 | 8 | * [Errichto (English)](https://youtu.be/0jWeUdxrGm4) 9 | 10 | * [Some problems](https://codeforces.com/blog/entry/70418) 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive Programming 2 | My competitive programming repo, with resources and codes that I like it ❤️ 3 | 4 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem A - Pizza time.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 12 | freopen("pizza.in", "r", stdin); 13 | int TC = 1; 14 | cin >> TC; 15 | while (TC--) { 16 | int a, b; 17 | cin >> a >> b; 18 | cout << a - b << '\n'; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem B- Reversing Strings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 12 | freopen("rev.in", "r", stdin); 13 | int TC = 1; 14 | cin >> TC; 15 | while (TC--) { 16 | int n, m, i = 0, flag = 0; 17 | string s, t; 18 | cin >> n >> m >> t >> s; 19 | if (n > m) { 20 | cout << "NO\n"; 21 | continue; 22 | } 23 | while (m > n) { 24 | flag = !flag; 25 | if (flag)i++; 26 | m--; 27 | } 28 | string temp = s.substr(i, n); 29 | if (flag) reverse(temp.begin(), temp.end()); 30 | cout << (temp == t ? "YES\n" : "NO\n"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem C- Ali’s Schedule.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int to_seconds(string &s, string &t) { 11 | int ret = stoi(s.substr(0, 2)) * 60 * 60 + 12 | stoi(s.substr(3, 2)) * 60 + 13 | stoi(s.substr(6)); 14 | if (t == "PM") ret += 12 * 60 * 60; 15 | return ret; 16 | } 17 | 18 | signed main() { 19 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 20 | freopen("schedule.in", "r", stdin); 21 | int TC = 1; 22 | cin >> TC; 23 | while (TC--) { 24 | int n; 25 | cin >> n; 26 | vector> arr(n); 27 | string s, t; 28 | for (int i = 0, x; i < n; ++i) { 29 | cin >> x >> s >> t; 30 | arr[i] = {to_seconds(s, t), x}; 31 | } 32 | sort(arr.begin(), arr.end()); 33 | for (auto &i: arr) 34 | cout << i.second << ' '; 35 | cout << '\n'; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem D- Make it K-good.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int to_seconds(string &s, string &t) { 11 | int ret = stoi(s.substr(0, 2)) * 60 * 60 + 12 | stoi(s.substr(3, 2)) * 60 + 13 | stoi(s.substr(6)); 14 | if (t == "PM") ret += 12 * 60 * 60; 15 | return ret; 16 | } 17 | 18 | signed main() { 19 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 20 | freopen("good.in", "r", stdin); 21 | int TC = 1; 22 | cin >> TC; 23 | while (TC--) { 24 | int n, k; 25 | string s; 26 | cin >> n >> k >> s; 27 | vector> freq(k, vector(26)); 28 | for (int i = 0; i < n; ++i) 29 | freq[i % k][s[i] - 'a']++; 30 | int ret = 0; 31 | for (auto &i: freq) 32 | ret += accumulate(i.begin(), i.end(), 0) 33 | - *max_element(i.begin(), i.end()); 34 | cout << ret << '\n'; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem F-Array Erasing.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include "bits/stdc++.h" 7 | 8 | using namespace std; 9 | const int N = 1000007; 10 | #define int long long 11 | 12 | int arr[N]; 13 | bool vis[N]; 14 | 15 | signed main() { 16 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 17 | freopen("erase.in", "r", stdin); 18 | int n; 19 | cin >> n; 20 | for (int i = 0; i < n; ++i) 21 | cin >> arr[i]; 22 | sort(arr, arr + n); 23 | int ret = 0; 24 | for (int i = 0; i < n; ++i) { 25 | if (!vis[arr[i]]) { 26 | ret++; 27 | for (int j = arr[i]; j < N; j += arr[i]) 28 | vis[j] = true; 29 | } 30 | } 31 | cout << ret; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem I- Moving Balls.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | #include "bits/stdc++.h" 6 | 7 | using namespace std; 8 | #define int long long 9 | 10 | 11 | signed main() { 12 | 13 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 14 | freopen("collisions.in", "r", stdin); 15 | 16 | int TC; 17 | cin >> TC; 18 | while (TC--) { 19 | int n, m; 20 | cin >> n >> m; 21 | cout << n * m << '\n'; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem M- New Sorting Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 7 | freopen("sort.in", "r", stdin); 8 | int TC; 9 | TC = 1; 10 | cin >> TC; 11 | while (TC--) { 12 | int n, x = (1 << 30) - 1; 13 | cin >> n; 14 | vector v(n); 15 | for (int &i: v) 16 | cin >> i; 17 | 18 | for (int i = 29; ~i; --i) { 19 | x &= ~(1 << i); 20 | bool good = true; 21 | for (int j = 1; j < n; ++j) 22 | good &= (v[j] | x) >= (v[j - 1] | x); 23 | if (!good) 24 | x |= 1 << i; 25 | } 26 | cout << x << "\n"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem N- K-Subarrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | #define all(x) x.begin(),x.end() 10 | 11 | int main() { 12 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 13 | freopen("sub.in", "r", stdin); 14 | int TC; 15 | TC = 1; 16 | cin >> TC; 17 | while (TC--) { 18 | int n, k; 19 | cin >> n >> k; 20 | int arr[n], ret[n]; 21 | map mp; 22 | for (int &i: arr) cin >> i; 23 | vector s(arr, arr + n); 24 | sort(all(s)); 25 | for (int i = 0; i < n; ++i) { 26 | if (mp.count(arr[i])) ret[i] = mp[arr[i]]++; 27 | else { 28 | mp[arr[i]] = lower_bound(all(s), arr[i]) - s.begin(); 29 | ret[i] = mp[arr[i]]++; 30 | } 31 | } 32 | int ans = 0; 33 | for (int i = n - 1; i >= 0; --i) { 34 | int cur = ret[i]; 35 | while (i > cur) { 36 | i--; 37 | if (i)cur = min(cur, ret[i]); 38 | } 39 | ans++; 40 | } 41 | cout << (ans >= k ? "YES" : "NO"); 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/Problem O- Ziko The Rabbit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | #define int long long 10 | 11 | signed main() { 12 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 13 | freopen("ziko.in", "r", stdin); 14 | int TC; 15 | TC = 1; 16 | cin >> TC; 17 | while (TC--) { 18 | int n; 19 | cin >> n; 20 | cout << ((n - 1) | n) << '\n'; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/Kickoff-Contest/statement (1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Solutions/ACPC-Kickoff-2022/Kickoff-Contest/statement (1).pdf -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/practice contest/Problem A - The Walking Adam.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 12 | freopen("adam.in", "r", stdin); 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | string s; 17 | cin >> s; 18 | int n = s.length(), ret = n; 19 | for (int i = 0; i < n; ++i) { 20 | if (s[i] == 'D') { 21 | ret = i; 22 | break; 23 | } 24 | } 25 | cout << ret << '\n'; 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/practice contest/Problem B- Duplicate Files.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Handle Codeforces : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 12 | freopen("files.in", "r", stdin); 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | int n; 17 | cin >> n; 18 | map mp; // FileName -> id 19 | set ret; 20 | int id; string s; 21 | while (n--) { 22 | cin >> s >> id; 23 | if (mp.count(s) == 0 or mp[s] > id) { 24 | ret.erase(mp[s]); 25 | ret.insert(id); 26 | mp[s] = id; 27 | } 28 | } 29 | for (const int &i: ret) 30 | cout << i << ' '; 31 | cout << '\n'; 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/practice contest/Problem C - Self Describing Numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Handle Codeforces : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 12 | freopen("self.in", "r", stdin); 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | string s, ret = "self-describing\n"; 17 | int freq[10] = {0}; 18 | cin >> s; 19 | for (char &i: s) ++freq[i - '0']; 20 | for (int i = 0, len = s.length(); i < len; ++i) { 21 | if (s[i] - '0' != freq[i]) { 22 | ret = "not self-describing\n"; 23 | break; 24 | } 25 | } 26 | cout << ret; 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/practice contest/Problem D- Guess Number.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Author : Abdelaleem Ahmed 4 | Handle Codeforces : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define int long long 10 | 11 | signed main() { 12 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 13 | int st = 1, end = 1e9, mid, ans = 1; 14 | string s; 15 | while (st <= end) { 16 | mid = (end - st) / 2 + st; 17 | cout << mid << "\n"; 18 | cout.flush(); 19 | cin >> s; 20 | if (s == "=") { 21 | ans = mid; 22 | break; 23 | } else if (s == "<") 24 | end = mid - 1; 25 | else 26 | ans = mid, st = mid + 1; 27 | 28 | } 29 | 30 | cout << "! " << ans; 31 | cout.flush(); 32 | } 33 | -------------------------------------------------------------------------------- /Solutions/ACPC-Kickoff-2022/practice contest/statement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/Solutions/ACPC-Kickoff-2022/practice contest/statement.pdf -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/B - Casino .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define int long long 4 | using namespace std; 5 | const int mod = 1e9 + 7, N = 1e5 + 5; 6 | int fact[N], inv[N]; 7 | int f[3]; 8 | 9 | int power(int a, int p) { 10 | if (!p)return 1; 11 | int res = power(a, p / 2); 12 | res = res * res % mod; 13 | if (p & 1)res = res * a % mod; 14 | return res; 15 | } 16 | 17 | int per(int n, int k) { 18 | if (k > n)return 0; 19 | return fact[n] * inv[n - k] % mod; 20 | } 21 | 22 | int solve(int i) { 23 | if (!f[1] && !f[2])return 1; 24 | int res = f[i]; 25 | if (res == 0)return 0; 26 | f[i]--; 27 | res = res * solve((i + i) % 3) % mod; 28 | f[i]++; 29 | return res; 30 | } 31 | 32 | signed main() { 33 | ios_base::sync_with_stdio(false); 34 | cin.tie(nullptr); 35 | fact[0] = inv[0] = 1; 36 | for (int i = 1; i < N; ++i) { 37 | fact[i] = i * fact[i - 1] % mod; 38 | inv[i] = power(fact[i], mod - 2); 39 | } 40 | int n; 41 | cin >> n; 42 | for (int i = 0, x; i < n; ++i) { 43 | cin >> x, f[x % 3]++; 44 | } 45 | int a = 0, b = 0; 46 | if (f[1]) { 47 | f[1]--; 48 | a = (f[1] + 1) * solve(1) % mod; 49 | f[1]++; 50 | } 51 | if (f[2]) { 52 | f[2]--; 53 | b = (f[2] + 1) * solve(2) % mod; 54 | f[2]++; 55 | } 56 | int c = per(n - 1, f[0]); 57 | cout << (a + b) % mod * c % mod; 58 | } 59 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/C - Cooperative Game on a Tree .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define int long long 4 | using namespace std; 5 | const int N = 2e5 + 5; 6 | int n; 7 | vector c[N]; 8 | int ans[N], d[N], p[N]; 9 | 10 | void dfs(int u, int left) { 11 | if (c[u].empty()) 12 | return; 13 | if (!left) 14 | ans[u]++, left = d[u]; 15 | for (auto &v: c[u]) 16 | ans[v] = ans[u], dfs(v, left - 1); 17 | } 18 | 19 | signed main() { 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(nullptr); 22 | cin >> n; 23 | for (int i = 1, x; i < n; ++i) { 24 | cin >> x, p[i] = --x, c[x].push_back(i); 25 | } 26 | queue q; 27 | for (int i = 0; i < n; ++i) { 28 | if (!c[i].size()) 29 | q.push(i); 30 | } 31 | vector vis(n); 32 | while (!q.empty()) { 33 | int u = q.front(); 34 | q.pop(); 35 | if (!vis[p[u]]) { 36 | vis[p[u]] = true; 37 | d[p[u]] = d[u] + 1; 38 | q.push(p[u]); 39 | } 40 | } 41 | dfs(0, 0); 42 | cout << *max_element(ans, ans + N) << '\n'; 43 | } 44 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/D - Katya and the Broken Keyboard .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define int long long 4 | using namespace std; 5 | 6 | signed main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int n; 10 | string s; 11 | cin >> s >> n; 12 | vector frq(26); 13 | for (auto &i: s) { 14 | frq[i - 'a']++; 15 | } 16 | int ans = s.size(); 17 | while (n--) { 18 | char c; 19 | int x; 20 | cin >> c >> x; 21 | ans += (frq[c - 'a'] + x - 2) / (x - 1); 22 | } 23 | cout << ans ; 24 | } 25 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/E - Cookie Day .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | void solve() { 7 | int n, k; 8 | cin >> n >> k; 9 | int res = -1; 10 | for (int i = 0, x; i < n; ++i) { 11 | cin >> x; 12 | if (x >= k) { 13 | int v = x % k; 14 | if (res == -1 || res > v)res = v; 15 | } 16 | } 17 | cout << res << '\n'; 18 | } 19 | 20 | int32_t main() { 21 | ios::sync_with_stdio(false); 22 | cin.tie(nullptr); 23 | int t; 24 | cin >> t; 25 | while (t--) { 26 | solve(); 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/F - Three Suitcases .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int w1, w2, w3, a, b, c; 6 | 7 | int p(int w) { return w < 5 ? a : w < 10 ? b : c; } 8 | 9 | signed main() { 10 | ios_base::sync_with_stdio(false); 11 | cin.tie(nullptr); 12 | cin >> w1 >> w2 >> w3 >> a >> b >> c; 13 | int ans = p(w1) + p(w2) + p(w3); 14 | ans = min(ans, p(w1 + w2) + p(w3)); 15 | ans = min(ans, p(w1 + w3) + p(w2)); 16 | ans = min(ans, p(w3 + w2) + p(w1)); 17 | ans = min(ans, p(w1 + w2 + w3)); 18 | cout << ans << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/G - Scooter Numbers .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | const int N = 1e3 + 5, mod = 1e9 + 7; 6 | 7 | int add(int a, int b) { return (a + b) % mod; } 8 | 9 | int dp1[N][N]; 10 | int dp2[N][N]; 11 | 12 | int increasing(int i, int rem) { 13 | if (rem == 0)return 1; 14 | if (rem < i)return 0; 15 | int &res = dp1[i][rem]; 16 | if (~res)return res; 17 | res = increasing(i + 1, rem); 18 | if (rem >= i) { 19 | res = add(res, increasing(i, rem - i)); 20 | } 21 | return res; 22 | } 23 | 24 | int decreasing(int i, int rem) { 25 | if (rem == 0)return i <= 1; 26 | if (rem < 0 || i == 0)return 0; 27 | int &res = dp2[i][rem]; 28 | if (~res)return res; 29 | res = add( 30 | decreasing(i, rem - i), /// add same number 31 | decreasing(i - 1, rem - i + 1) /// add (i-1) to the list 32 | ); 33 | return res; 34 | } 35 | 36 | 37 | int32_t main() { 38 | ios_base::sync_with_stdio(false); 39 | cin.tie(nullptr); 40 | 41 | memset(dp1, -1, sizeof dp1); 42 | memset(dp2, -1, sizeof dp2); 43 | 44 | int n; 45 | cin >> n; 46 | int res = 0; 47 | for (int i = 1; i <= n + 1; ++i) { 48 | for (int sumNext = 0, sumBefore = n; sumNext <= n; ++sumNext, sumBefore--) { 49 | int after_mex = increasing(i + 1, sumNext); 50 | int before_mex = decreasing(i - 1, sumBefore - i + 1); 51 | int cnt = after_mex * before_mex % mod; 52 | res = add(res, cnt * i % mod); 53 | } 54 | } 55 | cout << res; 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/H - Unalike Gcd & Lcm .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | void solve() { 7 | int x, q; 8 | cin >> x >> q; 9 | vector primes; 10 | for (int i = 2; i <= x / i; ++i) { 11 | if (x % i)continue; 12 | int c = 0; 13 | while (!(x % i))++c, x /= i; 14 | primes.emplace_back(c); 15 | } 16 | if (x > 1)primes.emplace_back(1); 17 | int p; 18 | while (q--) { 19 | cin >> p; 20 | int res = 1; 21 | if (p > 1) { 22 | for (auto &pw: primes) { 23 | res <<= (pw % p == 0); 24 | } 25 | } 26 | cout << res << '\n'; 27 | } 28 | 29 | } 30 | 31 | int32_t main() { 32 | ios::sync_with_stdio(false); 33 | cin.tie(nullptr); 34 | int t; 35 | cin >> t; 36 | while (t--) { 37 | solve(); 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/I - Qual B .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | int32_t main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int n, k; 10 | string s; 11 | cin >> n >> k >> s; 12 | for (int i = 0; i < n; ++i) { 13 | if (k > 0 && s[i] == 'o') { 14 | --k; 15 | } else { 16 | s[i] = 'x'; 17 | } 18 | } 19 | cout << s; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/J - Count the Christmas Trees .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define int long long 4 | using namespace std; 5 | 6 | const int M = 1e9 + 7, N = 5008; 7 | int mem[N][N]; 8 | 9 | int dp(int i, int x) { 10 | if (i == 0) return !x; 11 | auto &ret = mem[i][x]; 12 | if (~ret) return ret; 13 | ret = dp(i - 1, x); 14 | if (x >= 1) ret = (ret + dp(i - 1, x - 1)) % M; 15 | if (x >= 2) ret = (ret + dp(i - 1, x - 2)) % M; 16 | return ret; 17 | } 18 | 19 | signed main() { 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(nullptr); 22 | int n; 23 | cin >> n; 24 | int ans = 1; 25 | memset(mem, -1, sizeof mem); 26 | for (int i = 2; i <= n; ++i) { 27 | ans = dp(i - 1, i) * ans % M; 28 | } 29 | cout << ans << '\n'; 30 | } 31 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/K - Max MEX .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define int long long 4 | using namespace std; 5 | const int mod = 1e9 + 7, N = 1e5 + 5; 6 | 7 | signed main() { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | int n, k; 11 | set arr; 12 | cin >> n >> k; 13 | while (n--) { 14 | int x; 15 | cin >> x; 16 | arr.insert(x); 17 | } 18 | int mex = 0; 19 | while (arr.count(mex))++mex; 20 | cout << min(k, mex); 21 | } 22 | -------------------------------------------------------------------------------- /Solutions/Battle of The Brains/Contest #1/L - Make it Palindrome .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define int long long 5 | 6 | int32_t main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int n; 10 | cin >> n; 11 | int res = 0; 12 | map> mp; 13 | vector calc(n + 1); 14 | for (int i = 1; i <= n; ++i) { 15 | calc[i] = i / 2; 16 | } 17 | partial_sum(calc.begin(), calc.end(), calc.begin()); 18 | for (int i = 0, x; i < n; ++i) { 19 | cin >> x; 20 | res += calc[n - i]; 21 | mp[x].emplace_back(i); 22 | } 23 | for (auto &[_, arr]: mp) { 24 | int sum = 0, cnt = 0; 25 | multiset sz; 26 | for (int i: arr) { 27 | int x = n - i; 28 | while (sz.size() && *sz.rbegin() >= x) { 29 | sum -= *sz.rbegin(); 30 | ++cnt; 31 | sz.erase(--sz.end()); 32 | } 33 | res -= sum + cnt * x; 34 | sz.insert(i + 1); 35 | sum += i + 1; 36 | } 37 | } 38 | cout << res; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/(Aleppo + HAIST + SVU + Private) CPC 2022/C. Perfect Seating.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/103821/problem/C 5 | */ 6 | 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | signed main() { 13 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 14 | #ifndef ONLINE_JUDGE 15 | freopen("ans.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int TC = 1; 19 | cin >> TC; 20 | while (TC--) { 21 | int n; 22 | cin >> n; 23 | int mx = 0; 24 | bool yes = true; 25 | for (int i = 0, x; i < n; ++i) { 26 | cin >> x; 27 | yes &= (x > mx); 28 | mx = max(x, mx); 29 | } 30 | cout << (yes ? "Yes\n" : "No\n"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/(Aleppo + HAIST + SVU + Private) CPC 2022/D. Fairplay.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/103821/problem/D 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | #define ll long long 11 | #define int long long 12 | 13 | signed main() { 14 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 15 | #ifndef ONLINE_JUDGE 16 | freopen("ans.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | int TC; 20 | cin >> TC; 21 | 22 | auto can = [](int x) { 23 | int S = sqrt(x); 24 | return S * S != x; 25 | }; 26 | 27 | while (TC--) { 28 | int n; 29 | cin >> n; 30 | int lst = n + 1; 31 | if (can(lst)) { 32 | cout << "-1\n"; 33 | continue; 34 | } 35 | vector ret; 36 | for (int i = 1; i * i != lst; ++i) ret.emplace_back(i); 37 | cout << ret.size() << '\n'; 38 | for (int &i: ret)cout << i << ' '; 39 | cout << '\n'; 40 | } 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/(Aleppo + HAIST + SVU + Private) CPC 2022/E. Robovac.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/103821/problem/E 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | #define ll long long 11 | #define int long long 12 | 13 | void solve() { 14 | int n; 15 | cin >> n; 16 | if (n == 1)return void(cout << "0\n"); 17 | int r = (n >> 1); 18 | int ret = r * (r + 1); 19 | int l = n - r - 1; 20 | ret += l * (l + 1); 21 | cout << ret - max(l, r) << '\n'; 22 | } 23 | 24 | signed main() { 25 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 26 | #ifndef ONLINE_JUDGE 27 | freopen("ans.txt", "r", stdin); 28 | freopen("output.txt", "w", stdout); 29 | #endif 30 | int TC = 1; 31 | cin >> TC; 32 | while (TC--) 33 | solve(); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/(Aleppo + HAIST + SVU + Private) CPC 2022/F. A + B (Harder version).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/103821/problem/F 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | #define ll long long 11 | 12 | void solve() { 13 | int ret = 0, x; 14 | for (int i = 0; i < 7; ++i) { 15 | cin >> x; 16 | ret += x; 17 | } 18 | cout << ret << '\n'; 19 | } 20 | 21 | signed main() { 22 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 23 | #ifndef ONLINE_JUDGE 24 | freopen("ans.txt", "r", stdin); 25 | freopen("output.txt", "w", stdout); 26 | #endif 27 | int TC = 1; 28 | cin >> TC; 29 | while (TC--) { 30 | solve(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/(Aleppo + HAIST + SVU + Private) CPC 2022/H. FAT Sequences.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/103821/problem/H 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | #define ll long long 11 | #define int long long 12 | const int mod = 1000000007, N = (int) 1e5 + 7; 13 | int fact[N]; 14 | 15 | int add(int x, int y) { 16 | x += y; 17 | while (x >= mod) x -= mod; 18 | while (x < 0) x += mod; 19 | return x; 20 | } 21 | 22 | 23 | signed main() { 24 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 25 | #ifndef ONLINE_JUDGE 26 | freopen("ans.txt", "r", stdin); 27 | freopen("output.txt", "w", stdout); 28 | #endif 29 | fact[1] = 1; 30 | fact[2] = 2; 31 | for (int i = 3; i < N; ++i) 32 | fact[i] = add(fact[i - 1], fact[i - 2] + 1); 33 | int TC = 1; 34 | cin >> TC; 35 | while (TC--) { 36 | int n; 37 | cin >> n; 38 | cout << fact[n] << '\n'; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/(Aleppo + HAIST + SVU + Private) CPC 2022/K. Movie Planning.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/103821/problem/K 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | #define ll long long 11 | #define int long long 12 | const int mod = 1000000007; 13 | 14 | int add(int x, int y) { 15 | x += y; 16 | while (x >= mod) x -= mod; 17 | while (x < 0) x += mod; 18 | return x; 19 | } 20 | 21 | int mul(int x, int y) { 22 | return x % mod * y % mod; 23 | } 24 | 25 | 26 | void solve() { 27 | int n, m; 28 | cin >> n >> m; 29 | vector e(m + 7); 30 | vector> arr(n); 31 | for (int i = 0, u, v; i < n; ++i) { 32 | cin >> u >> v; 33 | e[u] += (m - v + 1); 34 | arr[i] = {u, v}; 35 | } 36 | const int MX = 1e18; 37 | for (int i = m - 1; i; --i) 38 | e[i] += e[i + 1]; 39 | 40 | int ret = 0; 41 | for (int i = 0; i < n; ++i) { 42 | ret = add(ret, 43 | mul( 44 | arr[i].first, 45 | e[arr[i].second + 1] 46 | ) 47 | ); 48 | } 49 | cout << ret << '\n'; 50 | } 51 | 52 | signed main() { 53 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 54 | #ifndef ONLINE_JUDGE 55 | freopen("ans.txt", "r", stdin); 56 | freopen("output.txt", "w", stdout); 57 | #endif 58 | int TC; 59 | cin >> TC; 60 | while (TC--) 61 | solve(); 62 | } 63 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/(Aleppo + HAIST + SVU + Private) CPC 2022/L. ResliPhobia.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/103821/problem/L 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define ll long long 10 | #define int long long 11 | const int N = 1007; 12 | int mem[3][N][N], vis[3][N][N], arr[N], vs; 13 | int n, k; 14 | 15 | int dp(int i, int mx, int s) { 16 | if (mx > k)return -1e18; 17 | if (i == n)return 0; 18 | int &ret = mem[s][i][mx]; 19 | if (vis[s][i][mx] == vs)return ret; 20 | vis[s][i][mx] = vs; 21 | ret = max( 22 | dp(i + 1, mx, s), 23 | arr[i] + dp(i + 1, 24 | (s != (arr[i] & 1) ? 1 : mx + 1), 25 | (arr[i] & 1) 26 | ) 27 | ); 28 | return ret; 29 | } 30 | 31 | 32 | int solve() { 33 | vs++; 34 | cin >> n >> k; 35 | for (int i = 0; i < n; ++i) cin >> arr[i]; 36 | return dp(0, 0, 2); 37 | } 38 | 39 | signed main() { 40 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 41 | #ifndef ONLINE_JUDGE 42 | freopen("ans.txt", "r", stdin); 43 | freopen("output.txt", "w", stdout); 44 | #endif 45 | int TC = 1; 46 | cin >> TC; 47 | while (TC--) 48 | cout << solve() << '\n'; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/(Aleppo + HAIST + SVU + Private) CPC 2022/M. Permutations Score.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/103821/problem/M 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int mod = 1e9 + 7; 11 | 12 | int mul(int x, int y) { 13 | return x * 1LL * y % mod; 14 | } 15 | 16 | const int N = 1e5 + 7; 17 | int pref[N], fact[N]; 18 | 19 | int main() { 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(nullptr), cout.tie(nullptr); 22 | #ifndef ONLINE_JUDGE 23 | freopen("ans.txt", "r", stdin); 24 | freopen("output.txt", "w", stdout); 25 | #endif 26 | for (int i = 1; i < N; ++i) 27 | for (int j = i * 2; j < N; j += i) 28 | ++pref[j]; 29 | partial_sum(pref, pref + N, pref); 30 | fact[1] = 0, fact[2] = 1; 31 | for (int i = 3; i < N; ++i) 32 | fact[i] = mul(fact[i - 1], i); 33 | int t, n; 34 | cin >> t; 35 | while (t--) { 36 | cin >> n; 37 | cout << mul(fact[n], pref[n]) << '\n'; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2014-2015 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)/M - Polyhedra.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To Gym : https://codeforces.com/gym/101653 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define all(x) (x).begin(),(x).end() 10 | 11 | signed main() { 12 | ios::sync_with_stdio(false); 13 | cin.tie(nullptr), cout.tie(nullptr); 14 | #ifndef ONLINE_JUDGE 15 | freopen("ans.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int T; 19 | cin >> T; 20 | while (T--) { 21 | int v, e; 22 | cin >> v >> e; 23 | cout << (2 + e - v) << '\n'; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2014-2015 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)/N - Majority.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To Gym : https://codeforces.com/gym/101653 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | #define all(x) (x).begin(),(x).end() 11 | const int mod = 1e9 + 7, N = 2e5 + 7; 12 | 13 | 14 | signed main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(nullptr), cout.tie(nullptr); 17 | #ifndef ONLINE_JUDGE 18 | freopen("ans.txt", "r", stdin); 19 | freopen("output.txt", "w", stdout); 20 | #endif 21 | int T; 22 | cin >> T; 23 | while (T--) { 24 | int n; 25 | cin >> n; 26 | vector f(1001); 27 | while (n--) { 28 | int x; 29 | cin >> x; 30 | ++f[x]; 31 | } 32 | int cnt = 0, ret = 0; 33 | for (int i = 0; i < f.size(); ++i) 34 | if (f[i] > cnt) 35 | ret = i, cnt = f[i]; 36 | 37 | assert(ret); 38 | cout << ret << '\n'; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2014-2015 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)/O - Diamonds.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To Gym : https://codeforces.com/gym/101653 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define all(x) (x).begin(),(x).end() 10 | const int mod = 1e9 + 7, N = 2e5 + 7; 11 | int a[N], b[N]; 12 | int mem[2][102][102][201]; 13 | int vis[2][102][102][201], vs; 14 | int n; 15 | 16 | 17 | int dp(int i, int x, int y, bool st) { 18 | if (i == n)return 0; 19 | int &ret = mem[st][x][y][i]; 20 | if (vis[st][x][y][i] == vs)return ret; 21 | vis[st][x][y][i] = vs; 22 | int op1 = 0; 23 | if (!st or (a[i] > x and b[i] < y) ) 24 | op1 = 1 + dp(i + 1, a[i], b[i], true); 25 | ret = max( 26 | op1, 27 | dp(i + 1, x, y, st) 28 | ); 29 | return ret; 30 | } 31 | 32 | 33 | signed main() { 34 | ios::sync_with_stdio(false); 35 | cin.tie(nullptr), cout.tie(nullptr); 36 | #ifndef ONLINE_JUDGE 37 | freopen("ans.txt", "r", stdin); 38 | freopen("output.txt", "w", stdout); 39 | #endif 40 | int T; 41 | cin >> T; 42 | double x; 43 | for (vs = 1; vs <= T; ++vs) { 44 | cin >> n; 45 | for (int i = 0; i < n; ++i) { 46 | cin >> x; a[i] = x * 10; 47 | cin >> x; b[i] = x * 10; 48 | } 49 | cout << dp(0, 0, 0, false) << '\n'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2014-2015 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)/R - Ramp Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define all(x) (x).begin(),(x).end() 5 | const int mod = 1e9 + 7, N = 2e5 + 7; 6 | #define int long long 7 | int n; 8 | int mem[2][2][10][81]; 9 | vector digits; 10 | 11 | int dp(int i = 0, int ls = 0, bool st = 0, bool sm = 0) { 12 | if (i == n)return st; 13 | int &ret = mem[sm][st][ls][i]; 14 | if (~ret)return ret; 15 | ret = 0; 16 | int mx = sm ? 9 : digits[i]; 17 | for (int d = ls; d <= mx; ++d) 18 | ret += dp(i + 1, d, st or d, sm or d < digits[i]); 19 | 20 | return ret; 21 | } 22 | 23 | void init() { 24 | memset(mem, -1, sizeof mem); 25 | digits.clear(); 26 | } 27 | 28 | int solve() { 29 | init(); 30 | string s; 31 | cin >> s; 32 | n = s.length(); 33 | while (s.size()) { 34 | digits.emplace_back(s.back() - '0'); 35 | s.pop_back(); 36 | } 37 | reverse(all(digits)); 38 | for (int i = 1; i < n; ++i) 39 | if (digits[i] < digits[i - 1])return -1; 40 | return dp(); 41 | } 42 | 43 | 44 | signed main() { 45 | ios::sync_with_stdio(false); 46 | cin.tie(nullptr), cout.tie(nullptr); 47 | #ifndef ONLINE_JUDGE 48 | freopen("ans.txt", "r", stdin); 49 | freopen("output.txt", "w", stdout); 50 | #endif 51 | int T; 52 | cin >> T; 53 | while (T--) 54 | cout << solve() << '\n'; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2014-2015 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)/U - Top 25.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To Gym : https://codeforces.com/gym/101653 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | #define all(x) (x).begin(),(x).end() 11 | const int mod = 1e9 + 7, N = 2e5 + 7; 12 | 13 | 14 | void solve() { 15 | vector ret; 16 | int n; cin >> n; 17 | string a[n], b[n]; 18 | for (auto &i: a) cin >> i; 19 | for (auto &i: b) cin >> i; 20 | int i = 0; 21 | while (i < n) { 22 | map x, y; 23 | int sz = 1; 24 | x[a[i]]++; 25 | y[b[i]]++; 26 | i++; 27 | while (x != y) { 28 | assert(i < n); 29 | x[a[i]]++; 30 | y[b[i]]++; 31 | ++sz, ++i; 32 | } 33 | ret.emplace_back(sz); 34 | } 35 | cout << ret.front(); 36 | for (i = 1; i < ret.size(); ++i) cout << ' ' << ret[i]; 37 | } 38 | 39 | 40 | signed main() { 41 | ios::sync_with_stdio(false); 42 | cin.tie(nullptr), cout.tie(nullptr); 43 | #ifndef ONLINE_JUDGE 44 | freopen("ans.txt", "r", stdin); 45 | freopen("output.txt", "w", stdout); 46 | #endif 47 | int T; 48 | cin >> T; 49 | double x; 50 | while (T--) { 51 | solve(); 52 | if (T) cout << '\n'; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest/A. Alex Origami Squares.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link : https://codeforces.com/gym/100801 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false); 12 | cout.tie(nullptr), cin.tie(nullptr); 13 | #ifndef ONLINE_JUDGE 14 | freopen("output.out", "w", stdout); 15 | #else 16 | freopen("alex.in", "r", stdin); 17 | freopen("alex.out", "w", stdout); 18 | #endif 19 | double h, w; 20 | cin >> h >> w; 21 | double ret = max({ 22 | min(h, (w / 3.0)), 23 | min((h / 3), w), 24 | min(h / 2, w / 2) 25 | 26 | } 27 | ); 28 | cout << fixed << setprecision(4) << ret; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest/B. Black and White.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link : https://codeforces.com/gym/100801 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int mod = 1e9 + 7, N = 2e4; 11 | 12 | #define char unsigned char 13 | char arr[4][N]; 14 | 15 | signed main() { 16 | ios_base::sync_with_stdio(false); 17 | cout.tie(nullptr), cin.tie(nullptr); 18 | #ifndef ONLINE_JUDGE 19 | freopen("output.out", "w", stdout); 20 | #else 21 | freopen("black.in", "r", stdin); 22 | freopen("black.out", "w", stdout); 23 | #endif 24 | int n, m; 25 | cin >> m >> n; 26 | if (n == m) { 27 | cout << 1 << ' ' << n * 2 << '\n'; 28 | for (int i = 0; n or m; ++i) { 29 | if (i & 1) { 30 | cout << '.'; 31 | --n; 32 | } else { 33 | cout << '@'; 34 | --m; 35 | } 36 | } 37 | return 0; 38 | } 39 | char b = '@', w = '.'; 40 | if (n < m)swap(b, w), swap(n, m); 41 | for (auto &i: arr) 42 | for (char &j: i) 43 | j = w; 44 | int i = 0; 45 | --m, n--; 46 | while (n > 0) { 47 | arr[0][i] = arr[1][i] = b; 48 | arr[1][i + 1] = arr[1][i + 2] = b; 49 | i += 2; 50 | arr[0][i] = arr[1][i] = b; 51 | n--; 52 | } 53 | for (int j = 1; m > 0; j += 2) { 54 | arr[3][j] = b; 55 | --m; 56 | } 57 | cout << 4 << ' ' << N << '\n'; 58 | for (auto &j: arr) { 59 | for (char k: j) 60 | cout << k; 61 | cout << '\n'; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16)/A. The game of Osho.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101147/problem/A 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | int who_win(int n, int k) { 12 | if (k & 1)return n & 1; 13 | if ((n + 1) % (k + 1) == 0)return 2; 14 | if ((n - (((n + 1) / (k + 1)))) & 1)return 1; 15 | return 0; 16 | } 17 | 18 | void do_work() { 19 | int g; 20 | cin >> g; 21 | int n, k, ans = 0; 22 | for (int i = 0; i < g; ++i) { 23 | cin >> k >> n; 24 | ans ^= who_win(n, k); 25 | } 26 | if (ans) cout << 1; 27 | else cout << 2; 28 | } 29 | 30 | 31 | signed main() { 32 | ios_base::sync_with_stdio(false); 33 | cout.tie(nullptr), cin.tie(nullptr); 34 | #ifndef ONLINE_JUDGE 35 | freopen("ans.txt", "r", stdin); 36 | freopen("output.txt", "w", stdout); 37 | #else 38 | freopen("powers.in", "r", stdin); 39 | #endif 40 | int TC; 41 | cin >> TC; 42 | while (TC--) { 43 | do_work(); 44 | cout << '\n'; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16)/D. Popcorn.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101147/problem/D 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define int long long 10 | 11 | int nCr(int n, int r) { 12 | if (n == r)return 1; 13 | return nCr(n - 1, r) * n / (n - r); 14 | } 15 | 16 | signed main() { 17 | ios_base::sync_with_stdio(false); 18 | cout.tie(nullptr), cin.tie(nullptr); 19 | #ifndef ONLINE_JUDGE 20 | freopen("ans.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | #else 23 | freopen("popcorn.in", "r", stdin); 24 | #endif 25 | int TC; 26 | cin >> TC; 27 | int n, r; 28 | while (TC--) { 29 | cin >> n >> r; 30 | cout << nCr(n, r) << '\n'; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16)/G. The Galactic Olympics.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101147/problem/G 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | const int mod = 1e9 + 7; 10 | 11 | int mul(int a, int b) { 12 | return (1LL * a * b) % mod; 13 | } 14 | 15 | int add(int a, int b) { 16 | return (a + b) % mod; 17 | } 18 | 19 | const int N = 1e3 + 7; 20 | 21 | int mem[N][N]; 22 | int vis[N][N], vs; 23 | int n, k; 24 | 25 | int dp(int i, int j) { 26 | if (i == n) return (j == 0); 27 | if (j < 0)return 0; 28 | int &ret = mem[i][j]; 29 | if (vis[i][j] == vs)return ret; 30 | vis[i][j] = vs; 31 | ret = add( 32 | mul(dp(i + 1, j), k - j), 33 | mul(dp(i + 1, j - 1), j) 34 | ); 35 | return ret; 36 | } 37 | 38 | void solve() { 39 | cin >> n >> k; 40 | if (n < k) { 41 | cout << "0\n"; 42 | }else { 43 | cout << dp(0, k) << '\n'; 44 | } 45 | } 46 | 47 | signed main() { 48 | ios_base::sync_with_stdio(false); 49 | cout.tie(nullptr), cin.tie(nullptr); 50 | #ifndef ONLINE_JUDGE 51 | freopen("ans.txt", "r", stdin); 52 | freopen("output.txt", "w", stdout); 53 | #else 54 | freopen("galactic.in", "r", stdin); 55 | #endif 56 | int TC; 57 | cin >> TC; 58 | while (TC--) { 59 | vs++; 60 | solve(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16)/H. Commandos.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101147/problem/H 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define int long long 10 | 11 | int mem[11][11][11]; 12 | int arr[11][11][11]; 13 | int vis[11][11][11], vs; 14 | int vis2[11][11][11]; 15 | 16 | int dp(int i, int j, int k) { 17 | if (i < 0 or j >= 10 or k >= 10)return 0; 18 | int &ret = mem[i][j][k]; 19 | if (vis[i][j][k] == vs)return ret; 20 | vis[i][j][k] = vs; 21 | ret = max( 22 | { 23 | dp(i - 1, j, k), 24 | dp(i, j + 1, k), 25 | dp(i, j, k + 1) 26 | } 27 | ); 28 | if (vis2[i][j][k] == vs) 29 | ret += arr[i][j][k]; 30 | return ret; 31 | } 32 | 33 | void solve() { 34 | int n; 35 | cin >> n; 36 | for (int i = 0; i < n; ++i) { 37 | int x, y, z, d; 38 | cin >> x >> y >> z >> d; 39 | --x, --y, --z; 40 | assert(vis2[x][y][z] != vs); 41 | arr[x][y][z] = d; 42 | vis2[x][y][z] = vs; 43 | } 44 | cout << dp(9, 0, 0) << '\n'; 45 | } 46 | 47 | signed main() { 48 | ios_base::sync_with_stdio(false); 49 | cout.tie(nullptr), cin.tie(nullptr); 50 | #ifndef ONLINE_JUDGE 51 | freopen("ans.txt", "r", stdin); 52 | freopen("output.txt", "w", stdout); 53 | #else 54 | freopen("commandos.in", "r", stdin); 55 | #endif 56 | int TC; 57 | cin >> TC; 58 | while (TC--) { 59 | vs++; 60 | solve(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2017 JUST Programming Contest 2.0/A. On The Way to Lucky Plaza.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101343/problem/A 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define int long long 10 | const int N = 1e5 + 7, mod = 1e9 + 7; 11 | int fact[N]; 12 | 13 | void pre() { 14 | fact[0] = 1; 15 | for (int i = 1; i < N; ++i) 16 | fact[i] = (fact[i - 1] * i) % mod; 17 | } 18 | 19 | int power(int x, int y) { 20 | if (y == 0) return 1; 21 | int temp = power(x, y >> 1) % mod; 22 | temp = temp * temp % mod; 23 | if (y & 1) temp = temp * x % mod; 24 | return temp; 25 | } 26 | 27 | int modInv(int x) { 28 | return power(x, mod - 2) % mod; 29 | } 30 | 31 | int nCr(int r, int n) { 32 | if (r > n)return 0; 33 | return fact[n] * modInv(fact[r] * fact[n - r] % mod) % mod; 34 | } 35 | 36 | 37 | signed main() { 38 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 39 | #ifndef ONLINE_JUDGE 40 | freopen("ans.txt", "r", stdin); 41 | freopen("output.txt", "w", stdout); 42 | #endif 43 | pre(); 44 | int n, m, k; 45 | double p; 46 | cin >> m >> n >> k >> p; 47 | int ncr = nCr(k - 1, n - 1); 48 | int c = p * 1000; 49 | int none = 1000 - c; 50 | c = c * modInv(1000) % mod; 51 | none = none * modInv(1000) % mod; 52 | int t = 1; 53 | for (int i = 0; i < k; ++i) t = (t * c) % mod; 54 | for (int i = k; i < n; ++i) t = (t * none) % mod; 55 | cout << t * ncr % mod; 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2017 JUST Programming Contest 2.0/B. So You Think You Can Count_ .cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101343/problem/B 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | #define ll long long 11 | #define int ll 12 | 13 | const int N = 1e4 + 7, M = (1 << 11), mod = 1e9 + 7; 14 | int mem[N][M], arr[N]; 15 | int n; 16 | 17 | int dp(int i, int msk) { 18 | if (i == n)return 1; 19 | int &ret = mem[i][msk]; 20 | if (~ret)return ret; 21 | ret = 0; 22 | ret = dp(i + 1, (1 << arr[i])); 23 | if (!(msk & (1 << arr[i]))) { 24 | ret = (ret + 25 | dp(i + 1, msk | (1 << arr[i])) 26 | ) % mod; 27 | } 28 | return ret; 29 | 30 | 31 | } 32 | 33 | signed main() { 34 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 35 | #ifndef ONLINE_JUDGE 36 | freopen("ans.txt", "r", stdin); 37 | freopen("output.txt", "w", stdout); 38 | #endif 39 | memset(mem, -1, sizeof mem); 40 | cin >> n; 41 | char x; 42 | for (int i = 0; i < n; ++i) { 43 | cin >> x; 44 | arr[i] = x - '0'; 45 | } 46 | cout << dp(1, (1 << arr[0])); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2017 JUST Programming Contest 2.0/D. Husam's Bug.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101343/problem/D 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define all(X) X.begin(),X.end() 10 | 11 | signed main() { 12 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 13 | #ifndef ONLINE_JUDGE 14 | freopen("ans.txt", "r", stdin); 15 | freopen("output.txt", "w", stdout); 16 | #endif 17 | int t; 18 | cin >> t; 19 | while (t--) { 20 | string s; 21 | cin >> s; 22 | int alpha = count_if(all(s), [&](char x) { return isalpha(x); }); 23 | int num = count_if(all(s), [&](char x) { return isdigit(x); }); 24 | int none = count_if(all(s), [&](char x) { return x == '@' or x == '?' or x == '!'; }); 25 | cout << "The last character "; 26 | if (alpha < 4) cout << "must be a letter.\n"; 27 | else if (num < 4) cout << "must be a digit.\n"; 28 | else if (none < 2) cout << "must be a symbol.\n"; 29 | else cout << "can be any type.\n"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2017 JUST Programming Contest 2.0/E. Abdalrahman Ali Bugs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101343/problem/E 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define int long long 10 | string s; 11 | int f[30]; 12 | 13 | signed main() { 14 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 15 | #ifndef ONLINE_JUDGE 16 | freopen("ans.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | cin >> s; 20 | for (char &i: s) f[i - 'a']++; 21 | int ret = INT32_MAX, p = -1; 22 | for (int i = 2; i <= (int) 1e6; ++i) { 23 | int cnt = 0; 24 | for (int &j: f) cnt += ((j % i) * j); 25 | 26 | if (cnt < ret) 27 | ret = cnt, p = i; 28 | } 29 | assert(~p); 30 | cout << p; 31 | } 32 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2017 JUST Programming Contest 2.0/F. Certifications.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101343/problem/F 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 12 | #ifndef ONLINE_JUDGE 13 | freopen("ans.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | int n; 17 | cin >> n; 18 | int arr[n]; 19 | for (int &i: arr) cin >> i; 20 | sort(arr, arr + n); 21 | int q, x; 22 | cin >> q; 23 | while (q--) { 24 | cin >> x; 25 | int idx = lower_bound(arr, arr + n, x) - arr; 26 | if (idx < n) { 27 | cout << arr[idx] << '\n'; 28 | } else { 29 | cout << "Dr. Samer cannot take any offer :(.\n"; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2017 JUST Programming Contest 2.0/G. In the Chairman's office.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101343/problem/G 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | #ifndef ONLINE_JUDGE 12 | freopen("ans.txt", "r", stdin); 13 | freopen("output.txt", "w", stdout); 14 | #endif 15 | int a, b; 16 | cin >> a >> b; 17 | cout << ((b % a == 0) ? "YES" : "NO"); 18 | } 19 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2017 JUST Programming Contest 2.0/H. Give Me This Pizza.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101343/problem/H 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define all(X) X.begin(),X.end() 10 | #define int long long 11 | 12 | signed main() { 13 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 14 | #ifndef ONLINE_JUDGE 15 | freopen("ans.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int n; 19 | cin >> n; 20 | int arr[n]; 21 | vector> idx(51); 22 | for (int i = 0; i < n; ++i) { 23 | cin >> arr[i]; 24 | idx[arr[i]].emplace_back(i); 25 | } 26 | 27 | auto query = [&](int i) -> int { 28 | int ret = n, v = -1; 29 | for (int j = arr[i] + 1; j <= 50; ++j) { 30 | auto &a = idx[j]; 31 | auto x = upper_bound(all(a), i); 32 | if (x != a.end()) { 33 | if (*x < ret) { 34 | ret = *x; 35 | v = j; 36 | } 37 | } 38 | } 39 | return v; 40 | }; 41 | 42 | for (int i = 0; i < n; ++i) 43 | cout << query(i) << ' '; 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2017 JUST Programming Contest 2.0/I. Husam and the Broken Present 1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101343/problem/I 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | signed main() { 10 | #ifndef ONLINE_JUDGE 11 | freopen("ans.txt", "r", stdin); 12 | freopen("output.txt", "w", stdout); 13 | #endif 14 | int n, x; 15 | cin >> n; 16 | int ret[n]; 17 | for (int i = 0; i < n; ++i) { 18 | for (int j = 0; j < n; ++j) { 19 | cin >> x; 20 | if (i == j) ret[i] = sqrt(x); 21 | } 22 | } 23 | for (int &i: ret) cout << i << ' '; 24 | } 25 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2018, XI Samara Regional Intercollegiate Programming Contest/A. Restoring Numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101755/problem/A 5 | */ 6 | 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | #define int long long 13 | 14 | signed main() { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(nullptr), cout.tie(nullptr); 17 | #ifndef ONLINE_JUDGE 18 | freopen("ans.txt", "r", stdin); 19 | freopen("output.txt", "w", stdout); 20 | #endif 21 | int s, g; 22 | cin >> s >> g; 23 | int a = s - g; 24 | if (a % g or !a) return cout << -1, 0; 25 | cout << a << ' ' << g; 26 | } 27 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2018, XI Samara Regional Intercollegiate Programming Contest/B. Minimal Area.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101755/problem/B 5 | */ 6 | 7 | 8 | #include 9 | 10 | using namespace std; 11 | #define int long long 12 | #define double long double 13 | #define all(v) (v).begin(),(v).end() 14 | #define bs binary_search 15 | 16 | int area(int x1, int y1, int x2, int y2, int x3, int y3) { 17 | swap(x1, x3), swap(y1, y3); 18 | return ((x1 * y2) + (x2 * y3) + (x3 * y1)) 19 | - ((y1 * x2) + (x1 * y3) + (y2 * x3)); 20 | } 21 | signed main() { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(nullptr), cout.tie(nullptr); 24 | #ifndef ONLINE_JUDGE 25 | freopen("ans.txt", "r", stdin); 26 | freopen("output.txt", "w", stdout); 27 | #endif 28 | int n; 29 | cin >> n; 30 | pair arr[n]; 31 | for (auto &i: arr) cin >> i.first >> i.second; 32 | int ans = min(area(arr[1].first, arr[1].second, 33 | arr[0].first, arr[0].second, 34 | arr[n - 1].first, arr[n - 1].second), 35 | area(arr[0].first, arr[0].second, 36 | arr[n - 1].first, arr[n - 1].second, 37 | arr[n - 2].first, arr[n - 2].second) 38 | ); 39 | for (int i = 2; i < n; ++i) { 40 | ans = min(ans, 41 | area(arr[i].first, arr[i].second, 42 | arr[i - 1].first, arr[i - 1].second, 43 | arr[i - 2].first, arr[i - 2].second 44 | ) 45 | ); 46 | } 47 | cout << ans; 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2018, XI Samara Regional Intercollegiate Programming Contest/C. Third-Party Software.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101755/problem/C 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define all(v) (v).begin(),(v).end() 11 | #define int long long 12 | 13 | signed main() { 14 | ios_base::sync_with_stdio(false); 15 | cin.tie(nullptr), cout.tie(nullptr); 16 | #ifndef ONLINE_JUDGE 17 | freopen("ans.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | int n; 21 | cin >> n; 22 | set> a, b; 23 | for (int i = 0; i < n; ++i) { 24 | int x, y; 25 | cin >> x >> y; 26 | a.emplace(y, x); 27 | b.emplace(x, y); 28 | } 29 | 30 | vector ret; 31 | while (a.size()) { 32 | auto it = a.begin(); 33 | ret.emplace_back(it->first); 34 | stack> s; 35 | while (b.size() and b.begin()->first <= it->first) { 36 | s.emplace(*b.begin()); 37 | b.erase(b.begin()); 38 | } 39 | while (s.size()) 40 | a.erase({s.top().second, s.top().first}), s.pop(); 41 | } 42 | cout << ret.size() << '\n'; 43 | for (auto &iy: ret)cout << iy << ' '; 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2018, XI Samara Regional Intercollegiate Programming Contest/E. Substring Reverse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101755/problem/E 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false); 12 | cin.tie(nullptr), cout.tie(nullptr); 13 | #ifndef ONLINE_JUDGE 14 | freopen("ans.txt", "r", stdin); 15 | freopen("output.txt", "w", stdout); 16 | #endif 17 | string s, t; 18 | cin >> s >> t; 19 | int n = s.length(); 20 | if (s.length() != t.length()) return cout << "NO", 0; 21 | if (s == t) return cout << "YES", 0; 22 | int i = 0, j = n - 1; 23 | for (int k = 0; k < n; ++k) { 24 | if (s[i] == t[i]) i++; 25 | if (t[j] == s[j]) --j; 26 | } 27 | assert(i <= j); 28 | reverse(s.begin() + i, s.begin() + j + 1); 29 | cout << (s == t ? "YES" : "NO"); 30 | } 31 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2018, XI Samara Regional Intercollegiate Programming Contest/J. Parallelograms.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101755/problem/J 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define int long long 11 | 12 | signed main() { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(nullptr), cout.tie(nullptr); 15 | #ifndef ONLINE_JUDGE 16 | freopen("ans.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | int n; 20 | cin >> n; 21 | map mp; 22 | int x; 23 | while (n--) { 24 | cin >> x; 25 | mp[x]++; 26 | } 27 | int ret = 0; 28 | for (auto &it: mp) 29 | ret += (it.second & (~1)); 30 | cout << (ret >> 2); 31 | } 32 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2018, XI Samara Regional Intercollegiate Programming Contest/K. Video Reviews.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101755/problem/K 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | #define int long long 10 | 11 | signed main() { 12 | ios_base::sync_with_stdio(false); 13 | cin.tie(nullptr), cout.tie(nullptr); 14 | #ifndef ONLINE_JUDGE 15 | freopen("ans.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int n, k; 19 | cin >> n >> k; 20 | int arr[n]; 21 | for (int &i: arr) cin >> i; 22 | 23 | auto okay = [&](int x) -> bool { 24 | int ret = 0, cnt = 0; 25 | for (int i = 0; i < n; ++i) { 26 | if (arr[i] <= cnt) ++cnt; 27 | else if (x) cnt++, --x; 28 | } 29 | return cnt >= k; 30 | }; 31 | 32 | int s = 0, e = k, m, ans = 0; 33 | while (s <= e) { 34 | m = (e - s) / 2 + s; 35 | if (okay(m)) ans = m, e = m - 1; 36 | else s = m + 1; 37 | } 38 | 39 | cout << ans; 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/2018, XI Samara Regional Intercollegiate Programming Contest/L. Queries on a String.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101755/problem/L 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define all(v) (v).begin(),(v).end() 11 | 12 | signed main() { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(nullptr), cout.tie(nullptr); 15 | #ifndef ONLINE_JUDGE 16 | freopen("ans.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | string s, tmp; 20 | cin >> s; 21 | int lenS = (int) s.length(), q; 22 | vector> idx(300); 23 | for (int i = 0; i < lenS; ++i) idx[s[i]].emplace_back(i); 24 | stack lstIdx; 25 | lstIdx.emplace(-2); 26 | cin >> q; 27 | char ch; 28 | while (q--) { 29 | cin >> tmp; 30 | if (tmp == "push") { 31 | cin >> ch; 32 | auto it = idx[ch].end(); 33 | if (~lstIdx.top()) 34 | it = upper_bound(all(idx[ch]), lstIdx.top()); 35 | 36 | if (it == idx[ch].end()) lstIdx.emplace(-1); 37 | else lstIdx.emplace(*it); 38 | } else 39 | lstIdx.pop(); 40 | 41 | cout << (~lstIdx.top() ? "YES\n" : "NO\n"); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)/B - Friends and Cookies.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101810/problem/B 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define int long long 11 | 12 | 13 | signed main() { 14 | ios_base::sync_with_stdio(false); 15 | cin.tie(nullptr), cout.tie(nullptr); 16 | #ifndef ONLINE_JUDGE 17 | freopen("ans.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | 21 | int t; 22 | cin >> t; 23 | while (t--) { 24 | int n, x; 25 | cin >> x >> n; 26 | if (n == 1) { 27 | cout << x << '\n'; 28 | continue; 29 | } 30 | int ret[n]; 31 | memset(ret, 0, sizeof ret); 32 | int g = 2 + 2 * (n - 2); 33 | int a = x / g; 34 | for (int i = 1; i < n - 1; ++i) 35 | ret[i] += a * 2; 36 | ret[0] = ret[n - 1] = a; 37 | int rem = x % g; 38 | bool flag = true; 39 | int j = 0; 40 | for (int i = 0; i < rem; ++i) { 41 | ret[j]++; 42 | if (flag)j++; 43 | else j--; 44 | if (j == n) j -= 2, flag = !flag; 45 | if (!~j)j += 2, flag = !flag; 46 | } 47 | for (int i = 0; i < n; ++i) { 48 | cout << ret[i] << " \n"[i + 1 == n]; 49 | } 50 | } 51 | 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)/C - Flip the Bits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101810/problem/C 5 | */ 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define int long long 11 | 12 | signed main() { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(nullptr), cout.tie(nullptr); 15 | #ifndef ONLINE_JUDGE 16 | freopen("ans.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | 20 | int t; 21 | cin >> t; 22 | while (t--) { 23 | int n; 24 | cin >> n; 25 | int i = 0; 26 | for (; !(n & (1 << i)); ++i); 27 | cout << (i + 1) << '\n'; 28 | } 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)/D - Magic Sticks.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101810/problem/D 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | #define int long long 12 | 13 | int fun(int n, int m) { 14 | int r1 = m / 2; 15 | int r2 = m - r1; 16 | int c1 = (n + 1) / 2; 17 | int c2 = (n + 2) / 2; 18 | return min( 19 | r1 * c1 + r2 * c2, 20 | r1 * c2 + r2 * c1 21 | ); 22 | } 23 | 24 | int solve() { 25 | int n, m; 26 | cin >> n >> m; 27 | return min(fun(n, m), fun(m, n)); 28 | } 29 | 30 | signed main() { 31 | ios_base::sync_with_stdio(false); 32 | cin.tie(nullptr), cout.tie(nullptr); 33 | #ifndef ONLINE_JUDGE 34 | freopen("ans.txt", "r", stdin); 35 | freopen("output.txt", "w", stdout); 36 | #endif 37 | int t; 38 | cin >> t; 39 | while (t--) 40 | cout << solve() << '\n'; 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)/F - Minimum Sum of Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101810/problem/F 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | #define int long long 12 | const int N = 1e6 + 7; 13 | 14 | int vis[N], val[N]; 15 | 16 | signed main() { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(nullptr), cout.tie(nullptr); 19 | #ifndef ONLINE_JUDGE 20 | freopen("ans.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | #endif 23 | int t; 24 | cin >> t; 25 | for (int vs = 1; vs <= t; ++vs) { 26 | int n, sum = 0; 27 | cin >> n; 28 | int arr[n]; 29 | for (int &i: arr) cin >> i; 30 | sort(arr, arr + n); 31 | if (arr[0] == 1) { 32 | cout << n << '\n'; 33 | continue; 34 | } 35 | for (int i = 0; i < n; ++i) { 36 | int x = arr[i]; 37 | if (vis[x] != vs) 38 | for (int j = x; j < N; j += x) 39 | if (vis[j] != vs) 40 | vis[j] = vs, val[j] = x; 41 | 42 | 43 | sum += val[x]; 44 | } 45 | cout << sum << '\n'; 46 | } 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)/H - Making Friends.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101810/problem/H 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | #define ll long long 12 | #define all(x) (x).begin(), (x).end() 13 | #define sz(x) ((int)(x).size()) 14 | #define int long long 15 | const int N = 1e5 + 7; 16 | int arr[N]; 17 | 18 | signed main() { 19 | ios_base::sync_with_stdio(false); 20 | cin.tie(nullptr), cout.tie(nullptr); 21 | #ifndef ONLINE_JUDGE 22 | freopen("ans.txt", "r", stdin); 23 | freopen("output.txt", "w", stdout); 24 | 25 | #endif 26 | int t; 27 | cin >> t; 28 | int n; 29 | while (t--) { 30 | cin >> n; 31 | n <<= 1; 32 | for (int i = 1; i <= n; ++i) { 33 | cin >> arr[i]; 34 | } 35 | int ret = 0; 36 | for (int i = 1; i <= n; ++i) { 37 | int j = n - i + 1; 38 | if (j <= n) 39 | ret = max(ret, arr[i] + arr[j]); 40 | } 41 | cout << ret << '\n'; 42 | } 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)/I - Split the Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author : Abdelaleem Ahmed 3 | Codeforces Handle : [Abdelaleem](https://codeforces.com/profile/Abdelaleem) 4 | Link To problem : https://codeforces.com/gym/101810/problem/I 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | #define all(x) (x).begin(), (x).end() 12 | #define int long long 13 | 14 | signed main() { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(nullptr), cout.tie(nullptr); 17 | #ifndef ONLINE_JUDGE 18 | freopen("ans.txt", "r", stdin); 19 | freopen("output.txt", "w", stdout); 20 | #endif 21 | 22 | int t; 23 | cin >> t; 24 | while (t--) { 25 | int n, x; 26 | cin >> x >> n; 27 | if (n > x) { 28 | cout << -1 << '\n'; 29 | continue; 30 | } 31 | int rem = (x % n); 32 | int num = x / n; 33 | vector ret; 34 | while (x > 0) { 35 | ret.emplace_back(num + !!(rem)); 36 | x -= num + !!(rem); 37 | if (rem)rem--; 38 | assert(x >= 0); 39 | } 40 | sort(all(ret)); 41 | for (int i = 0; i < ret.size(); ++i) { 42 | cout << ret[i] << " \n"[i + 1 == ret.size()]; 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/JU Flash Contest/A. N integers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link : https://codeforces.com/gym/102035/problem/A 3 | Author : Abdelaleleem 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | #define ll long long 9 | #define all(v) (v).begin(),(v).end() 10 | 11 | inline void solve() { 12 | 13 | 14 | } 15 | 16 | 17 | int main() { 18 | ios_base::sync_with_stdio(false); 19 | cin.tie(nullptr), cout.tie(nullptr); 20 | int n; 21 | cin >> n; 22 | cout << n * 4; 23 | } 24 |   25 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/JU Flash Contest/B. Mahmoud the Thief.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link : https://codeforces.com/gym/102035/problem/B 3 | Author : Abdelaleleem 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | #define ll long long 10 | #define int ll 11 | #define all(v) (v).begin(),(v).end() 12 | 13 | 14 | signed main() { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(nullptr), cout.tie(nullptr); 17 | #ifndef ONLINE_JUDGE 18 | freopen("ans.txt", "r", stdin); 19 | freopen("output.txt", "w", stdout); 20 | #endif 21 | int n, m; 22 | cin >> n >> m; 23 | map mp; 24 | int arr[n + 1]; 25 | for (int i = 1; i <= n; ++i) { 26 | cin >> arr[i]; 27 | arr[i] %= m; 28 | mp[arr[i]]++; 29 | } 30 | for (int i = 0, x; i < n; ++i) { 31 | if (mp.size() == 1) return cout << i, 0; 32 | cin >> x; 33 | mp[arr[x]]--; 34 | if (mp[arr[x]] == 0) mp.erase(arr[x]); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/JU Flash Contest/D. Coach Ayoub.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link : https://codeforces.com/gym/102035/problem/D 3 | Author : Abdelaleleem 4 | */ 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | ios_base::sync_with_stdio(false); 11 | cin.tie(nullptr), cout.tie(nullptr); 12 | #ifndef ONLINE_JUDGE 13 | freopen("ans.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #else 16 | //freopen("pyramid.in", "r", stdin); 17 | #endif 18 | int n, a, b, s; 19 | cin >> n >> a >> b >> s; 20 | 21 | if (n == 1) { 22 | cout << ((a == s and a == b) ? "YES" : "NO"); 23 | } else if (n == 2) { 24 | cout << (a + b == s ? "YES" : "NO"); 25 | } else { 26 | int rem = (s - ((n - 1) * a + b)); 27 | if (rem < 0) cout << "NO"; 28 | else if (!rem) { 29 | cout << "YES"; 30 | } else { 31 | rem = (rem / (n - 2)) + (!!(rem % (n - 2))); 32 | cout << ((rem + a) <= b ? "YES" : "NO"); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/JU Flash Contest/E. New Max.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link : https://codeforces.com/gym/102035/problem/E 3 | Author : Abdelaleleem 4 | */ 5 | #include 6 | 7 | using namespace std; 8 | #define ll long long 9 | #define int ll 10 | #define all(v) (v).begin(),(v).end() 11 | 12 | 13 | signed main() { 14 | ios_base::sync_with_stdio(false); 15 | cin.tie(nullptr), cout.tie(nullptr); 16 | #ifndef ONLINE_JUDGE 17 | freopen("ans.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | int n, m, k, x; 21 | cin >> n >> m >> k; 22 | int found = 0, cnt = 0; 23 | for (int i = 0; i < n; ++i) { 24 | cin >> x; 25 | if (x == m)found = 1; 26 | if (x > m)cnt++; 27 | } 28 | if (found == 0 and cnt == 0) cnt = 1; 29 | k -= cnt; 30 | cout << (0 <= k ? "YES" : "NO"); 31 | } 32 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/JU Flash Contest/I. Abu Tahun Mod problem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link : https://codeforces.com/gym/102035/problem/I 3 | Author : Abdelaleleem 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | #define int long long 10 | 11 | signed main() { 12 | ios_base::sync_with_stdio(false), cin.tie(nullptr); 13 | int n; 14 | cin >> n; 15 | int arr[n]; 16 | for (int &i: arr) cin >> i; 17 | int i = 0, j = n - 1; 18 | vector ret; 19 | while (i < j) ret.emplace_back(abs(arr[i++] - arr[j--])); 20 | int ans = 0; 21 | for (int &ele: ret) ans = __gcd(ans, ele); 22 | cout << (ans ? ans : -1) << '\n'; 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/JU Flash Contest/K. Scientest Ayoub (A).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link : https://codeforces.com/gym/102035/problem/K 3 | Author : Abdelaleleem 4 | */ 5 | #include 6 | 7 | using namespace std; 8 | #define ll long long 9 | #define int ll 10 | #define all(v) (v).begin(),(v).end() 11 | 12 | 13 | signed main() { 14 | ios_base::sync_with_stdio(false); 15 | cin.tie(nullptr), cout.tie(nullptr); 16 | #ifndef ONLINE_JUDGE 17 | freopen("ans.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | int n; 21 | cin >> n; 22 | int ret = 1; 23 | for (int i = 1; i <= n; ++i) { 24 | cout << ret << ' '; 25 | ret *= 2; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/JU Flash Contest/L. Scientist Ayoub (B).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link : https://codeforces.com/gym/102035/problem/L 3 | Author : Abdelaleleem 4 | */ 5 | #include 6 | 7 | using namespace std; 8 | #define int long long 9 | 10 | signed main() { 11 | ios_base::sync_with_stdio(false); 12 | cin.tie(nullptr), cout.tie(nullptr); 13 | #ifndef ONLINE_JUDGE 14 | freopen("ans.txt", "r", stdin); 15 | freopen("output.txt", "w", stdout); 16 | #endif 17 | int mx = 2e8, n; 18 | cin >> n; 19 | int cnt = 1; 20 | for (int i = 0; i < n; ++i) { 21 | if (i % 2 == 0) { 22 | cout << cnt++ << ' '; 23 | } else { 24 | cout << mx << ' '; 25 | assert(mx <= 1e9); 26 | mx += (cnt + cnt); 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/JU Flash Contest/M. Ahmad Jaber Rectangles.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link : https://codeforces.com/gym/102035/problem/M 3 | Author : Abdelaleleem 4 | */ 5 | #include 6 | 7 | using namespace std; 8 | #define ll long long 9 | #define int ll 10 | 11 | 12 | signed main() { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(nullptr), cout.tie(nullptr); 15 | #ifndef ONLINE_JUDGE 16 | freopen("ans.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | int a, b, x, y, h; 20 | cin >> a >> b >> x >> y >> h; 21 | if (b < x or a > y) return cout << 0, 0; 22 | if (b <= y and a >= x) return cout << (b - a) * h, 0; 23 | if (y <= b and x >= a) return cout << (y - x) * h, 0; 24 | x = max(x, a); 25 | y = min(b, y); 26 | cout << (y - x) * h; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/NCD 2019/C - Hasan and his lazy students.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long 6 | const int N = 1e3 + 5, mod = 1e9 + 7; 7 | int a[N], n; 8 | int mem[N][N], vis[N][N], vs; 9 | int mem2[N][N], vis2[N][N]; 10 | 11 | int dpMax(int i, int j) { 12 | if (i == n)return 0; 13 | int &ret = mem[i][j]; 14 | if (vis[i][j] == vs)return ret; 15 | vis[i][j] = vs; 16 | ret = dpMax(i + 1, j); 17 | if (a[i] > a[j]) 18 | ret = max(ret, 1 + dpMax(i + 1, i)); 19 | return ret; 20 | } 21 | 22 | int dpCount(int i, int j) { 23 | if (i == n)return 1; 24 | int &ret = mem2[i][j]; 25 | if (vis2[i][j] == vs)return ret; 26 | vis2[i][j] = vs; 27 | ret = 0; 28 | int mx = dpMax(i, j); 29 | int op1 = dpMax(i + 1, j), op2 = -1; 30 | if (a[i] > a[j]) 31 | op2 = 1 + dpMax(i + 1, i); 32 | if (op1 == mx) 33 | ret = dpCount(i + 1, j); 34 | if (op2 == mx) { 35 | ret += dpCount(i + 1, i); 36 | if (ret >= mod)ret -= mod; 37 | } 38 | return ret; 39 | } 40 | 41 | void solve() { 42 | cin >> n; 43 | for (int i = 0; i < n; ++i) { 44 | cin >> a[i]; 45 | } 46 | ++vs; 47 | int ret = 0; 48 | 49 | for (int i = 0; i < n; ++i) 50 | ret = max(ret, 1 + dpMax(i + 1, i)); 51 | int cnt = 0; 52 | for (int i = 0; i < n; ++i) 53 | if (1 + dpMax(i + 1, i) == ret) { 54 | cnt += dpCount(i + 1, i); 55 | if (cnt >= mod)cnt -= mod; 56 | } 57 | cout << ret << ' ' << cnt; 58 | } 59 | 60 | signed main() { 61 | ios_base::sync_with_stdio(false); 62 | cin.tie(nullptr), cout.tie(nullptr); 63 | int tc = 1; 64 | cin >> tc; 65 | while (tc--) { 66 | solve(); 67 | cout << '\n'; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/NCD 2019/D - Football Cup.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long 6 | 7 | 8 | void solve() { 9 | int x, y; 10 | cin >> x >> y; 11 | if (x == y) { 12 | cout << "Iskandar"; 13 | } else if (x > y) { 14 | cout << "Bashar"; 15 | } else { 16 | cout << "Hamada"; 17 | } 18 | 19 | } 20 | 21 | signed main() { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(nullptr), cout.tie(nullptr); 24 | int tc = 1; 25 | cin >> tc; 26 | while (tc--) { 27 | solve(); 28 | cout << '\n'; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/NCD 2019/F - Research projects.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long 6 | 7 | 8 | void solve() { 9 | int n, k; 10 | cin >> n >> k; 11 | cout << (n - k + 5) / 6; 12 | 13 | } 14 | 15 | signed main() { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(nullptr), cout.tie(nullptr); 18 | int tc = 1; 19 | cin >> tc; 20 | while (tc--) { 21 | solve(); 22 | cout << '\n'; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/NCD 2019/H - Mr. Hamra and his quantum particles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long 6 | const int N = 1e5 + 5; 7 | int par[N]; 8 | int vis[N], vs; 9 | 10 | int findLeader(int u) { 11 | if (vis[u] != vs) { 12 | vis[u] = vs, par[u] = u; 13 | } 14 | if (u == par[u])return u; 15 | return par[u] = findLeader(par[u]); 16 | } 17 | 18 | void join(int u, int v) { 19 | u = findLeader(u), v = findLeader(v); 20 | par[u] = v; 21 | } 22 | 23 | void solve() { 24 | ++vs; 25 | int n, m, q; 26 | cin >> n >> m >> q; 27 | for (int i = 0, u, v; i < m; ++i) { 28 | cin >> u >> v; 29 | assert(u <= n); 30 | join(u, v); 31 | } 32 | int u, v; 33 | while (q--) { 34 | cin >> u >> v; 35 | cout << (findLeader(u) == findLeader(v)); 36 | } 37 | 38 | 39 | } 40 | 41 | signed main() { 42 | ios_base::sync_with_stdio(false); 43 | cin.tie(nullptr), cout.tie(nullptr); 44 | int tc = 1; 45 | cin >> tc; 46 | while (tc--) { 47 | solve(); 48 | cout << '\n'; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/NCD 2019/J - Bashar and daylight saving time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long 6 | const int N = 1e5 + 5; 7 | 8 | 9 | void solve() { 10 | int m, n; 11 | cin >> m >> n; 12 | int a[n]; 13 | vector pref(m + 5); 14 | for (int &x: a) cin >> x, --x; 15 | for (int i = 0, x; i < n; ++i) { 16 | cin >> x; 17 | int s = a[i], &e = a[i]; 18 | e += x; 19 | if (x >= 0) { 20 | if (e >= m) { 21 | ++s, ++e; 22 | ++pref[s], ++pref[1], --pref[e - m + 1]; 23 | } else { 24 | ++s, ++e; 25 | ++pref[s], --pref[e + 1]; 26 | } 27 | } else { 28 | if (e >= 0) { 29 | ++s, ++e; 30 | ++pref[e], --pref[s + 1]; 31 | } else { 32 | ++s, ++e; 33 | --pref[s + 1], ++pref[1], ++pref[e + m]; 34 | } 35 | } 36 | } 37 | int mx = -1, ans; 38 | for (int i = 1; i <= m; ++i) { 39 | pref[i] += pref[i - 1]; 40 | if (pref[i] > mx) 41 | mx = pref[i], ans = i; 42 | } 43 | cout << ans << ' ' << mx; 44 | } 45 | 46 | signed main() { 47 | ios_base::sync_with_stdio(false); 48 | cin.tie(nullptr), cout.tie(nullptr); 49 | int tc = 1; 50 | cin >> tc; 51 | while (tc--) { 52 | solve(); 53 | cout << '\n'; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/NCD 2019/K - Masaoud LOVES PIZZAS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long 6 | const int N = 1e5 + 5; 7 | 8 | 9 | void solve() { 10 | int n, k; 11 | cin >> n >> k; 12 | vector pref = {0}; 13 | int res = 0; 14 | for (int i = 0, x; i < n; ++i) { 15 | cin >> x; 16 | pref.emplace_back(x + pref.back()); 17 | int s = 0, e = i + 1, md, ans = -1; 18 | while (s <= e) { 19 | md = (e - s) / 2 + s; 20 | if (pref.back() - pref[md] < k) { 21 | ans = md, e = md - 1; 22 | } else { 23 | s = md + 1; 24 | } 25 | } 26 | if (~ans) 27 | res += i + 1 - ans; 28 | } 29 | cout << res; 30 | } 31 | 32 | signed main() { 33 | ios_base::sync_with_stdio(false); 34 | cin.tie(nullptr), cout.tie(nullptr); 35 | int tc = 1; 36 | cin >> tc; 37 | while (tc--) { 38 | solve(); 39 | cout << '\n'; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/NCD 2019/L - Chemistry Exam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long 6 | 7 | 8 | void solve() { 9 | int n;cin >> n; 10 | for (int i = 0,x; i < n; ++i) { 11 | cin >> x; 12 | cout << __builtin_popcount(x) << ' '; 13 | } 14 | 15 | } 16 | 17 | signed main() { 18 | ios_base::sync_with_stdio(false); 19 | cin.tie(nullptr), cout.tie(nullptr); 20 | int tc = 1; 21 | cin >> tc; 22 | while (tc--) { 23 | solve(); 24 | cout << '\n'; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Solutions/Codeforces Gym Solutions/NCD 2019/M - NCD Salary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long 6 | const int N = 1e5 + 5; 7 | 8 | 9 | void solve() { 10 | int a, b, x, y; 11 | cin >> a >> b >> x >> y; 12 | long double f; 13 | if (a) f = (long double) b * log(a); 14 | else f = -1; 15 | long double s; 16 | if (x) s = (long double) y * log(x); 17 | else s = -1; 18 | if (fabs(f - s) < 1e-9) { 19 | cout << "Lazy"; 20 | } else if (f > s) { 21 | cout << "HaHa"; 22 | } else { 23 | cout << "Congrats"; 24 | } 25 | } 26 | 27 | signed main() { 28 | ios_base::sync_with_stdio(false); 29 | cin.tie(nullptr), cout.tie(nullptr); 30 | int tc = 1; 31 | cin >> tc; 32 | while (tc--) { 33 | solve(); 34 | cout << '\n'; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/A. Arrayland's Challenge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | const int N = 1e4; 6 | int dp[N][N]; 7 | 8 | int32_t main() { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(nullptr); 11 | int n; 12 | cin >> n; 13 | int a[n]; 14 | for (int &x: a) { 15 | cin >> x; 16 | } 17 | for (int i = 0; i < n; ++i) { 18 | dp[i][i] = 2e6; 19 | } 20 | for (int sz = 2, l, r; sz <= n; ++sz) { 21 | for (l = 0, r = sz - 1; r < n; ++l, ++r) { 22 | dp[l][r] = min({ 23 | dp[l][r - 1], 24 | dp[l + 1][r], 25 | abs(a[l] - a[r]) 26 | }); 27 | } 28 | } 29 | int q, l, r; 30 | cin >> q; 31 | while (q--) { 32 | cin >> l >> r; 33 | cout << dp[--l][--r] << '\n'; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/B. Bacterial Sampling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | typedef long long ll; 5 | typedef vector row; 6 | typedef vector mat; 7 | 8 | const int mod = 1e9 + 7; 9 | 10 | int add(int a, int b) { return (a + b) % mod; } 11 | 12 | int mul(int a, int b) { return a * 1LL * b % mod; } 13 | 14 | mat operator*(const mat &a, const mat &b) { 15 | int r1 = a.size(), r2 = b.size(), c2 = b[0].size(); 16 | mat ret(r1, row(c2)); 17 | 18 | for (int i = 0; i < r1; i++) 19 | for (int j = 0; j < c2; j++) 20 | for (int k = 0; k < r2; k++) 21 | ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j])); 22 | 23 | return ret; 24 | } 25 | 26 | mat operator^(const mat &a, ll k) { 27 | mat ret(a.size(), row(a.size())); 28 | for (int i = 0; i < a.size(); i++)ret[i][i] = 1; 29 | for (mat tmp = a; k; tmp = tmp * tmp, k /= 2) if (k & 1) ret = ret * tmp; 30 | return ret; 31 | } 32 | 33 | const int N = 10; 34 | 35 | int32_t main() { 36 | ios_base::sync_with_stdio(false); 37 | cin.tie(nullptr); 38 | int tc; 39 | cin >> tc; 40 | while (tc--) { 41 | mat trans(N, row(N)), init(N, row(1)); 42 | init[0][0] = 1; 43 | for (int i = 2; i < N; i += 2) { 44 | trans[0][i] = 3; 45 | } 46 | for (int i = 1; i < N; ++i) { 47 | trans[i][i - 1] = 1; 48 | } 49 | int n; 50 | cin >> n; 51 | n /= 2; 52 | trans = trans ^ n; 53 | auto ans = trans * init; 54 | int res{}; 55 | for (auto &x: ans) { 56 | res = add(res, x[0]); 57 | } 58 | cout << res << '\n'; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/C. Chocolate Packing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | 6 | int32_t main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int a[3], b[3]; 10 | for (int &x: a) cin >> x; 11 | for (int &x: b) cin >> x; 12 | sort(b, b + 3); 13 | int ans = 0; 14 | do { 15 | int cur = 1; 16 | for (int i = 0; i < 3; ++i) { 17 | cur *= a[i] / b[i]; 18 | } 19 | ans = max(ans, cur); 20 | } while (next_permutation(b, b + 3)); 21 | 22 | cout << ans; 23 | } 24 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/E. Evaluating Linear Expressions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | signed main() { 6 | ios_base::sync_with_stdio(0); 7 | cin.tie(0); 8 | int a, b, k; 9 | cin >> a >> b >> k; 10 | for (int x = 1; x <= k; ++x) { 11 | cout << a * x + b << ' '; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/F. Factory TikTak Trend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define all(x) x.begin(),x.end() 4 | #define sz(x) (int)((x).size()) 5 | using namespace std; 6 | using ll = long long; 7 | 8 | struct SuffixArray { 9 | string S; 10 | int N; 11 | vector sa, isa; 12 | 13 | void init(const string &s) { 14 | S = s; 15 | N = sz(S) + 1; 16 | genSa(); 17 | } 18 | 19 | void genSa() { 20 | sa = isa = vector(N); 21 | sa[0] = N - 1; 22 | iota(1 + all(sa), 0); 23 | sort(1 + all(sa), [&](int a, int b) { return S[a] < S[b]; }); 24 | for (int i = 1; i < N; ++i) { 25 | int a = sa[i - 1], b = sa[i]; 26 | isa[b] = i > 1 && S[a] == S[b] ? isa[a] : i; 27 | } 28 | for (int len = 1; len < N; len *= 2) { 29 | vector s(sa), is(isa), pos(N); 30 | iota(all(pos), 0); 31 | for (auto &t: s) { 32 | int T = t - len; 33 | if (T >= 0)sa[pos[isa[T]]++] = T; 34 | } 35 | for (int i = 1; i < N; ++i) { 36 | int a = sa[i - 1], b = sa[i]; 37 | isa[b] = is[a] == is[b] && is[a + len] == is[b + len] ? isa[a] : i; 38 | } 39 | } 40 | } 41 | }; 42 | 43 | int32_t main() { 44 | ios::sync_with_stdio(false); 45 | cin.tie(nullptr); 46 | int n; 47 | string s, t; 48 | cin >> n >> s >> t; 49 | 50 | SuffixArray sa; 51 | sa.init(t + t + '{' + s + s); 52 | 53 | vector s_ids; 54 | for (int i = 0; i < n; ++i) { 55 | s_ids.emplace_back(sa.isa[2 * n + 1 + i]); 56 | } 57 | sort(all(s_ids)); 58 | ll res = 0; 59 | for (int i = 0; i < n; ++i) { 60 | res += s_ids.end() - upper_bound(all(s_ids), sa.isa[i]); 61 | } 62 | cout << n * 1LL * n - res; 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/G. Granitus Stone Towers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | signed main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr), cout.tie(nullptr); 8 | int n; 9 | cin >> n; 10 | vector a(n + 1); 11 | for (int i = 1; i <= n; ++i) { 12 | cin >> a[i]; 13 | } 14 | sort(a.begin(), a.end()); 15 | a.erase(unique(a.begin(), a.end()), a.end()); 16 | n = a.size(); 17 | vector dp(n); 18 | dp[n - 1] = 1; 19 | for (int i = n - 2; i >= 0; --i) { 20 | dp[i] = !dp[i + 1]; 21 | if ((a[i] - a[i - 1]) > 1) 22 | dp[i] |= dp[i + 1]; 23 | } 24 | cout << (dp[1] ? "Alicius\n" : "Bobius\n"); 25 | } 26 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/J. Journey To Stringland.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | const int N = 500; 5 | int mem[N][N][N + 1]; 6 | int n; 7 | string s; 8 | 9 | int dp(int l, int r, int rem) { 10 | if (rem == 0)return 0; 11 | if (r < l or (r - l + 1) < rem)return 1e9; 12 | int &res = mem[l][r][rem]; 13 | if (~res)return res; 14 | res = min(dp(l + 1, r, rem), dp(l, r - 1, rem)); 15 | res = min( 16 | res, 17 | (s[l] != s[r]) + dp(l + 1, r - 1, rem - 1 - (l != r)) 18 | ); 19 | return res; 20 | } 21 | 22 | int32_t main() { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(nullptr); 25 | int k; 26 | cin >> n >> k >> s; 27 | memset(mem, -1, sizeof mem); 28 | cout << dp(0, n - 1, k); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/K. Knights In The Board.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | vector> adj; 5 | vector mt, vis; 6 | int vid; 7 | 8 | bool can(int u) { 9 | if (vis[u] == vid)return false; 10 | vis[u] = vid; 11 | for (int v: adj[u]) { 12 | if (mt[v] == -1 or can(mt[v])) { 13 | mt[v] = u; 14 | return true; 15 | } 16 | } 17 | return false; 18 | } 19 | 20 | signed main() { 21 | ios::sync_with_stdio(false); 22 | cin.tie(nullptr); 23 | int n, k; 24 | cin >> n >> k; 25 | 26 | adj.assign(n * n, {}); 27 | mt.assign(n * n, -1); 28 | vis.assign(n * n, {}); 29 | 30 | auto inside = [&](int i, int j) { 31 | return i >= 0 and j < n and j >= 0 and i < n; 32 | }; 33 | 34 | vector> a(n, vector(n)); 35 | for (int i = 0, x, y; i < k; ++i) { 36 | cin >> x >> y; 37 | a[--x][--y] = true; 38 | } 39 | 40 | for (int i = 0; i < n; ++i) 41 | for (int j = 0; j < n; ++j) { 42 | if (!a[i][j] or (i + j) & 1) continue; 43 | int u = i * n + j; 44 | for (int ii = -2; ii <= 2; ++ii) 45 | for (int jj = -2; jj <= 2; ++jj) { 46 | int x = i + ii, y = j + jj; 47 | if (!ii or !jj or abs(ii) == abs(jj))continue; 48 | if (!inside(x, y) or !a[x][y])continue; 49 | int v = x * n + y; 50 | adj[u].emplace_back(v); 51 | adj[v].emplace_back(u); 52 | } 53 | } 54 | 55 | int res{}; 56 | for (int i = 0; i < n; ++i) { 57 | for (int j = 0; j < n; ++j) { 58 | if (!a[i][j] or (i + j) & 1)continue; 59 | ++vid; 60 | int u = i * n + j; 61 | res += can(u); 62 | } 63 | } 64 | cout << res; 65 | } 66 | -------------------------------------------------------------------------------- /Solutions/ECPC 2024 - Are You Ready/Team Contest #1/L. Lost Land of Numeralia.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | const int N = 1e3 + 1; 6 | 7 | signed main() { 8 | ios::sync_with_stdio(false); 9 | cin.tie(nullptr); 10 | int n, q, sum, p; 11 | cin >> n >> q; 12 | while (q--) { 13 | cin >> sum >> p; 14 | int limit = n / p; 15 | ll res{}; 16 | for (int sz = 2; sz <= N; ++sz) { 17 | ll ss = (sz - 1) * sz / 2; 18 | ll a = sum / p - ss; 19 | a = max(1ll, (a + sz - 1) / sz); 20 | if (p * (ss + a * sz) < sum)++a; 21 | ll r = (limit - sz + 1); 22 | if (r <= 0)break; 23 | res += max(0ll, r - a + 1); 24 | } 25 | cout << res << '\n'; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Solutions/Google-Kick-Start-2022/Round A/Challenge Nine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | #define NeedForSpeed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 6 | 7 | 8 | void solve() { 9 | string s; 10 | ll sum = 0; 11 | cin >> s; 12 | for (char &i: s) sum += (i - '0'); 13 | sum = ((sum - (sum % 9) + 9) - sum) % 9; 14 | bool printed = false, first = true; 15 | for (char &i: s) { 16 | if (printed or first and sum == 0) { 17 | cout << i; 18 | } else if (i - '0' > sum) { 19 | cout << sum << i; 20 | printed = true; 21 | } else 22 | cout << i; 23 | 24 | first = false; 25 | } 26 | if (!printed) cout << sum; 27 | } 28 | 29 | int main() { 30 | NeedForSpeed; 31 | int t; 32 | cin >> t; 33 | for (int i = 1; i <= t; ++i) { 34 | cout << "Case #" << i << ": "; 35 | solve(); 36 | if (t) cout << '\n'; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Solutions/Google-Kick-Start-2022/Round A/Interesting Integers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define NeedForSpeed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 5 | #define ll long long 6 | int num[15], sz = 0, v, mod; 7 | ll mem[13][120][120][2][2]; 8 | ll vis[13][120][120][2][2]; 9 | 10 | ll dp(int idx, int remSum, int m, bool s = false, bool st = false) { 11 | if (remSum < 0) return 0; 12 | if (idx == sz) return (remSum == 0 and m == 0); 13 | int mx = s ? 9 : num[idx]; 14 | ll &ans = mem[idx][remSum][m][s][st]; 15 | if (vis[idx][remSum][m][s][st] == v) return ans; 16 | vis[idx][remSum][m][s][st] = v; 17 | ans = 0; 18 | for (int d = 0; d <= mx; ++d) { 19 | int M = d % mod; 20 | if (st) M = (d * m) % mod; 21 | ans += dp(idx + 1, remSum - d, M, s or d < num[idx], d or st); 22 | } 23 | return ans; 24 | } 25 | 26 | ll solve(ll x) { 27 | if (x == 0) return 0; 28 | sz = log10(x) + 1; 29 | for (int i = sz - 1; i >= 0; --i) { 30 | num[i] = x % 10; 31 | x /= 10; 32 | } 33 | ll ans = 0; 34 | for (mod = 1, v++; mod <= 9 * sz; mod++, v++) 35 | ans += dp(0, mod, 0); 36 | 37 | return ans; 38 | } 39 | 40 | int main() { 41 | NeedForSpeed; 42 | int t = 1; 43 | cin >> t; 44 | ll l, r; 45 | for (int i = 1; i <= t; ++i) { 46 | cout << "Case #" << i << ": "; 47 | 48 | cin >> l >> r; 49 | cout << solve(r) - solve(l - 1); 50 | 51 | if (t) cout << "\n"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Solutions/Google-Kick-Start-2022/Round A/Palindrome Free Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | #define all(X) (X).begin(),(X).end() 6 | #define sz(X) ((int)(X).size()) 7 | 8 | 9 | const int N = 5e4 + 7, M = (1 << 6) - 1; 10 | bool mem[N][M + 1]; 11 | int mem2[M + 1], vis[N][M + 1], vs; 12 | char str[N]; 13 | 14 | bool havePalindrome(string s) { 15 | string x = s; 16 | reverse(s.begin(), s.end()); 17 | return (x == s); 18 | } 19 | 20 | bool check(int msk) { 21 | int &res = mem2[msk]; 22 | if (~res) return res; 23 | string s; 24 | for (int i = 5; i >= 0; --i) s += ((msk >> i) & 1)+'0'; 25 | res = (havePalindrome(s) or havePalindrome(s.substr(0, 5)) or havePalindrome(s.substr(1))); 26 | return res; 27 | } 28 | 29 | bool BASE(int msk) { 30 | string s; 31 | for (int i = 4; i >= 0; --i) s += (((msk >> i) & 1) ? '1' : '0'); 32 | return havePalindrome(s); 33 | } 34 | 35 | bool solve(int i, int msk) { 36 | if (i == 5 and BASE(msk)) return false; 37 | if (i > 5 and check(msk)) return false; 38 | if (!str[i]) return true; 39 | bool &ret = mem[i][msk]; 40 | if (vis[i][msk] == vs)return ret; 41 | vis[i][msk] = vs; 42 | if (str[i] != '?') ret = solve(i + 1, ((msk << 1) + (str[i] == '1')) & M); 43 | else { 44 | ret = solve(i + 1, ((msk << 1) + 1) & M); 45 | ret |= solve(i + 1, (msk << 1) & M); 46 | } 47 | return ret; 48 | } 49 | 50 | signed main() { 51 | memset(mem2, -1, sizeof mem2); 52 | int t; 53 | scanf("%d", &t); 54 | for (int i = 1, x; i <= t and ++vs; ++i) { 55 | scanf("%d%s", &x, str); 56 | printf("Case #%d: %s\n", i, (solve(0, 0) ? "POSSIBLE" : "IMPOSSIBLE")); 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /Solutions/Google-Kick-Start-2022/Round A/Speed Typing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define NeedForSpeed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 6 | 7 | void solve() { 8 | string a, b; 9 | cin >> a >> b; 10 | int r = 0, cnt = 0; 11 | int n = a.length(), m = b.length(); 12 | for (int i = 0; i < n; ++i) { 13 | while (r < m and b[r] != a[i]) 14 | r++, cnt++; 15 | if (r == m) { 16 | cout << "IMPOSSIBLE"; 17 | return; 18 | } 19 | r++; 20 | } 21 | cout << cnt + (m - r); 22 | } 23 | 24 | int main() { 25 | NeedForSpeed; 26 | int t; 27 | cin >> t; 28 | for (int i = 1; i <= t; ++i) { 29 | cout << "Case #" << i << ": "; 30 | solve(); 31 | if (t) cout << '\n'; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Solutions/Google-Kick-Start-2022/Round B/Infinity Area.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | #define NeedForSpeed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 6 | 7 | 8 | const double PI = acos(-1); 9 | #define ll long long 10 | #define double long double 11 | #define int ll 12 | 13 | void solve() { 14 | int r, a, b; 15 | cin >> r >> a >> b; 16 | double res = 0; 17 | bool x = false; 18 | while (r > 0) { 19 | res += PI * r * r; 20 | (x ? r /= b : r *= a); 21 | x = !x; 22 | } 23 | cout << fixed << setprecision(6) << res; 24 | 25 | } 26 | 27 | signed main() { 28 | NeedForSpeed; 29 | int t; 30 | cin >> t; 31 | for (int i = 1; i <= t; ++i) { 32 | cout << "Case #" << i << ": "; 33 | solve(); 34 | if (t) cout << '\n'; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Solutions/Google-Kick-Start-2022/Round B/Palindromic Factors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | #define NeedForSpeed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 6 | 7 | 8 | #define ll long long 9 | #define int ll 10 | 11 | void solve() { 12 | int a; 13 | cin >> a; 14 | int sz = 0; 15 | 16 | auto check = [&](int x) -> bool { 17 | string s = to_string(x); 18 | string l = s; 19 | reverse(s.begin(), s.end()); 20 | return l == s; 21 | }; 22 | 23 | for (int i = 1; i * i <= a; ++i) { 24 | if (a % i == 0) { 25 | sz += check(i); 26 | sz += i * i != a and check(a / i); 27 | } 28 | } 29 | cout << sz; 30 | } 31 | 32 | signed main() { 33 | NeedForSpeed; 34 | int t; 35 | cin >> t; 36 | for (int i = 1; i <= t; ++i) { 37 | cout << "Case #" << i << ": "; 38 | solve(); 39 | if (t) cout << '\n'; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Solutions/Google-Kick-Start-2022/Round C/New Password.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define all(x) (x).begin(),(x).end() 5 | 6 | void solve() { 7 | 8 | int n; 9 | string s; 10 | cin >> n >> s; 11 | if (find_if(all(s), [&](char x) { 12 | return isdigit(x); 13 | }) == s.end()) { 14 | s += '1'; 15 | } 16 | if (find_if(all(s), [&](char x) { 17 | return x == '#' or x == '@' or x == '*' or x == '&'; 18 | }) == s.end()) { 19 | s += '@'; 20 | } 21 | if (find_if(all(s), [&](char x) { 22 | return isalpha(x) and islower(x); 23 | }) == s.end()) { 24 | s += 'a'; 25 | } 26 | if (find_if(all(s), [&](char x) { 27 | return isalpha(x) and isupper(x); 28 | }) == s.end()) { 29 | s += 'A'; 30 | } 31 | while (s.length() < 7) { 32 | s += 'a'; 33 | } 34 | cout << s; 35 | } 36 | 37 | signed main() { 38 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 39 | int t; 40 | cin >> t; 41 | for (int i = 1; i <= t; ++i) { 42 | cout << "Case #" << i << ": "; 43 | solve(); 44 | if (t) cout << '\n'; 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Solutions/Google-Kick-Start-2022/Round C/Range Partition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define ll long long 5 | #define NeedForSpeed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) 6 | #define ll long long 7 | #define int ll 8 | int num[5004]; 9 | 10 | void solve() { 11 | int n, x, y; 12 | cin >> n >> x >> y; 13 | int s1 = x + y; 14 | int s2 = n * (n + 1) / 2; 15 | if (s2 % s1 == 0) { 16 | cout << "POSSIBLE\n"; 17 | set st(num, num + n); 18 | vector ans; 19 | ll x1 = s2 / s1 * x; 20 | while (x1 > 0) { 21 | auto l = st.upper_bound(x1); 22 | assert(st.begin() != l); 23 | l--; 24 | x1 -= *l; 25 | ans.emplace_back(*l); 26 | st.erase(l); 27 | } 28 | cout << ans.size() << '\n'; 29 | for (int &i: ans) 30 | cout << i << ' '; 31 | } else { 32 | cout << "IMPOSSIBLE"; 33 | } 34 | 35 | } 36 | 37 | signed main() { 38 | NeedForSpeed; 39 | iota(num, num + 5007, 1); 40 | int t; 41 | cin >> t; 42 | for (int i = 1; i <= t; ++i) { 43 | cout << "Case #" << i << ": "; 44 | solve(); 45 | if (t) cout << '\n'; 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /templates/Adaptive Simpson's.cpp: -------------------------------------------------------------------------------- 1 | double f(double a) { 2 | 3 | } 4 | 5 | double adaptiveSimpsonsAux(double a, 6 | double b, 7 | double epsilon, double S, double fa, double fb, double 8 | fc, int bottom) { 9 | double c = (a + b) / 2, h = b - a; 10 | double d = (a + c) / 2, e = (c + b) / 2; 11 | double fd = f(d), fe = f(e); 12 | double Sleft = (h / 12) * (fa + 4 * fd + fc); 13 | double Sright = (h / 12) * (fc + 4 * fe + fb); 14 | double S2 = Sleft + Sright; 15 | if (bottom <= 0 || fabs(S2 - S) <= 15 * epsilon) 16 | return S2 + (S2 - S) / 15; 17 | return adaptiveSimpsonsAux(a, c, 18 | epsilon / 2, Sleft, fa, 19 | fc, fd, 20 | bottom - 1) + 21 | adaptiveSimpsonsAux(c, b, 22 | epsilon / 2, Sright, 23 | fc, fb, fe, bottom - 1); 24 | } 25 | 26 | // 27 | // Adaptive Simpson's Rule 28 | // 29 | double adaptiveSimpsons(double a, double b, // interval [a,b] 30 | double epsilon, // error tolerance 31 | int maxRecursionDepth) { // recursion cap 32 | double c = (a + b) / 2, h = b - a; 33 | double fa = f(a), fb = f(b), fc = f(c); 34 | double S = (h / 6) * (fa + 4 * fc + fb); 35 | return adaptiveSimpsonsAux(a, b, epsilon, S, fa, fb, fc, maxRecursionDepth); 36 | } 37 | -------------------------------------------------------------------------------- /templates/Combinatorics/Catalan Numbers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/templates/Combinatorics/Catalan Numbers.pdf -------------------------------------------------------------------------------- /templates/Combinatorics/Efficient combinations.cpp: -------------------------------------------------------------------------------- 1 | void Divbygcd(ll &a, ll &b) { 2 | ll g = gcd(a, b); 3 | a /= g, b /= g; 4 | } 5 | 6 | ll nCr(int n, int k) { 7 | if (n < k) return 0; 8 | if (k > n / 2) k = n - k; // use smaller k 9 | ll numerator = 1, denominator = 1, toMul, toDiv, i; 10 | for (i = k; i; i--) { 11 | toMul = n - k + i; 12 | toDiv = i; 13 | Divbygcd(toMul, toDiv); // always divide before multiply 14 | Divbygcd(numerator, toDiv); 15 | Divbygcd(toMul, denominator); 16 | numerator *= toMul; 17 | denominator *= toDiv; 18 | } 19 | return numerator / denominator; 20 | } 21 | -------------------------------------------------------------------------------- /templates/Combinatorics/combinations O(N^2).cpp: -------------------------------------------------------------------------------- 1 | const int MAX = 31; 2 | int comb[MAX][MAX]; 3 | 4 | void pre() { 5 | comb[0][0] = 1; 6 | for (int i = 1; i <= MAX; i++) { 7 | comb[i][0] = comb[i][i] = 1; 8 | for (int j = 1; j < i; j++) 9 | comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1]; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /templates/DS/2D sparse Table.cpp: -------------------------------------------------------------------------------- 1 | const int LG = 11; 2 | 3 | template> 4 | struct SP2d { 5 | CMP func; 6 | int m, n; 7 | vector> sp[LG][LG]; 8 | 9 | SP2d(const vector> &arr, const CMP &f) : func(f) { 10 | n = arr.size(), m = arr[0].size(); 11 | for (int i = 0; i < LG; ++i) { 12 | for (int j = 0; j < LG; ++j) { 13 | sp[i][j].assign(n, vector(m)); 14 | } 15 | } 16 | for (int ir = 0; ir < n; ++ir) { 17 | for (int ic = 0; ic < m; ++ic) 18 | sp[0][0][ir][ic] = arr[ir][ic]; 19 | } 20 | for (int a = 0; a < LG; a++) { 21 | for (int b = 0; b < LG; b++) { 22 | if (a + b == 0) continue; 23 | for (int i = 0; i + (1 << a) <= n; i++) { 24 | for (int j = 0; j + (1 << b) <= m; j++) { 25 | if (!a) { 26 | sp[a][b][i][j] = func( 27 | sp[a][b - 1][i][j], 28 | sp[a][b - 1][i][j + (1 << (b - 1))] 29 | ); 30 | } else { 31 | sp[a][b][i][j] = func( 32 | sp[a - 1][b][i][j], 33 | sp[a - 1][b][i + (1 << (a - 1))][j] 34 | ); 35 | } 36 | } 37 | } 38 | } 39 | } 40 | } 41 | 42 | T query(int x1, int y1, int x2, int y2) { 43 | int lgX = __lg(x2 - x1 + 1); 44 | int lgY = __lg(y2 - y1 + 1); 45 | T val1 = func( 46 | sp[lgX][lgY][x1][y1], 47 | sp[lgX][lgY][x1][y2 - (1 << lgY) + 1] 48 | ); 49 | T val2 = func( 50 | sp[lgX][lgY][x2 - (1 << lgX) + 1][y1], 51 | sp[lgX][lgY][x2 - (1 << lgX) + 1][y2 - (1 << lgY) + 1] 52 | ); 53 | return func(val1, val2); 54 | } 55 | }; 56 | -------------------------------------------------------------------------------- /templates/DS/BIT.cpp: -------------------------------------------------------------------------------- 1 | /// point update - range query 2 | struct BIT { 3 | vector tree; 4 | 5 | BIT(int n) { tree.assign(n + 5, {}); } 6 | 7 | int query(int l, int r) { 8 | ++r; 9 | int res = 0; 10 | while (r > 0) { 11 | res += tree[r], r -= r & -r; 12 | } 13 | while (l > 0) { 14 | res -= tree[l], l -= l & -l; 15 | } 16 | return res; 17 | } 18 | 19 | void update(int i, int v) { 20 | ++i; 21 | while (i < tree.size()) { 22 | tree[i] += v; 23 | i += i & -i; 24 | } 25 | } 26 | 27 | }; -------------------------------------------------------------------------------- /templates/DS/BIT2DWithUpdate.cpp: -------------------------------------------------------------------------------- 1 | #define vi vector 2 | #define vvi vector 3 | #define vvvi vector 4 | 5 | struct BIT2D { 6 | private: 7 | vvvi M, A; 8 | 9 | void upd2(vvvi &t, int x, int y, ll mul, ll add) { 10 | for (int i = x; i < t.size(); i += i & -i) { 11 | for (int j = y; j < t[0].size(); j += j & -j) { 12 | t[i][j][0] += mul; 13 | t[i][j][1] += add; 14 | } 15 | } 16 | } 17 | 18 | void upd1(int x, int y1, int y2, ll mul, ll add) { 19 | upd2(M, x, y1, mul, -mul * (y1 - 1)); 20 | upd2(M, x, y2, -mul, mul * y2); 21 | upd2(A, x, y1, add, -add * (y1 - 1)); 22 | upd2(A, x, y2, -add, add * y2); 23 | } 24 | 25 | 26 | ll query2(vvvi &t, int x, int y) { 27 | ll mul = 0, add = 0; 28 | for (int i = y; i > 0; i -= i & -i) { 29 | mul += t[x][i][0]; 30 | add += t[x][i][1]; 31 | } 32 | return mul * y + add; 33 | } 34 | 35 | ll query1(int x, int y) { 36 | ll mul = 0, add = 0; 37 | for (int i = x; i > 0; i -= i & -i) { 38 | mul += query2(M, i, y); 39 | add += query2(A, i, y); 40 | } 41 | return mul * x + add; 42 | } 43 | 44 | public: 45 | BIT2D(int n, int m) { 46 | M.assign(n + 1, vvi(m + 1, vi(2, 0))); 47 | A = M; 48 | } 49 | 50 | ll query(int x1, int y1, int x2, int y2) { 51 | return query1(x2, y2) - 52 | query1(x1 - 1, y2) - 53 | query1(x2, y1 - 1) + 54 | query1(x1 - 1, y1 - 1); 55 | } 56 | 57 | void update(int x1, int y1, int x2, int y2, ll val) { 58 | upd1(x1, y1, y2, val, -val * (x1 - 1)); 59 | upd1(x2, y1, y2, -val, val * x2); 60 | } 61 | 62 | }; 63 | -------------------------------------------------------------------------------- /templates/DS/BIT2d.cpp: -------------------------------------------------------------------------------- 1 | /// point update / sub-rectangle query 2 | struct BIT2D { 3 | int n, m; 4 | vector> bit; 5 | 6 | BIT2D(int n, int m) : n(n), m(m) { 7 | bit.assign(n + 2, vector(m + 2)); 8 | } 9 | 10 | void update(int x, int y, int val) { 11 | ++x, ++y; 12 | for (; x <= n; x += x & -x) { 13 | for (int i = y; i <= m; i += i & -i) { 14 | bit[x][i] += val; 15 | } 16 | } 17 | } 18 | 19 | int prefix(int x, int y) { 20 | ++x, ++y; 21 | int res = 0; 22 | for (; x > 0; x -= x & -x) { 23 | for (int i = y; i > 0; i -= i & -i) { 24 | res += bit[x][i]; 25 | } 26 | } 27 | return res; 28 | } 29 | 30 | int query(int sx, int sy, int ex, int ey) { 31 | int ans = 0; 32 | ans += prefix(ex, ey); 33 | ans -= prefix(ex, sy - 1); 34 | ans -= prefix(sx - 1, ey); 35 | ans += prefix(sx - 1, sy - 1); 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /templates/DS/BITUpdateRange.cpp: -------------------------------------------------------------------------------- 1 | struct BIT { 2 | private: 3 | int n; 4 | vector B1, B2; 5 | 6 | void add(vector &b, int idx, int x) { 7 | ++idx; 8 | while (idx <= n) { 9 | b[idx] += x; 10 | idx += idx & -idx; 11 | } 12 | } 13 | 14 | int sum(vector &b, int idx) { 15 | idx++; 16 | int total = 0; 17 | while (idx > 0) { 18 | total += b[idx]; 19 | idx -= idx & -idx; 20 | } 21 | return total; 22 | } 23 | 24 | int prefix(int idx) { 25 | return sum(B1, idx) * idx - sum(B2, idx); 26 | } 27 | 28 | public: 29 | 30 | BIT(int n) : n(n) { 31 | B1.assign(n + 1, {}); 32 | B2.assign(n + 1, {}); 33 | } 34 | 35 | void update(int l, int r, int x) { 36 | add(B1, l, x); 37 | add(B1, r + 1, -x); 38 | add(B2, l, x * (l - 1)); 39 | add(B2, r + 1, -x * r); 40 | } 41 | 42 | int query(int i) { 43 | return prefix(i) - prefix(i - 1); 44 | } 45 | 46 | int query(int l, int r) { 47 | return prefix(r) - prefix(l - 1); 48 | } 49 | 50 | }; -------------------------------------------------------------------------------- /templates/DS/BSFenwickTree.cpp: -------------------------------------------------------------------------------- 1 | struct BSFenwickTree { 2 | public: 3 | vector tree, vis; 4 | int n; 5 | 6 | BSFenwickTree(int n, int v = 0) { 7 | this->n = 1 << (__lg(n) + !!(n & (n - 1))); 8 | tree.assign(this->n + 1, v); 9 | vis.assign(this->n + 1, 0); 10 | } 11 | 12 | void update(int x, int v) { 13 | for (++x; x <= n; x += (x) & (-x)) { 14 | tree[x - 1] += v; 15 | vis[x - 1] += v < 0 ? -1 : 1; 16 | } 17 | } 18 | 19 | int query(int l, int r) { 20 | ++r; 21 | int res = 0; 22 | while (r > 0) { 23 | res += tree[r - 1]; 24 | r -= r & -r; 25 | } 26 | while (l > 0) { 27 | res -= tree[l - 1]; 28 | l -= l & -l; 29 | } 30 | return res; 31 | } 32 | 33 | /// max prefix [size] that has sum <= v 34 | int search(int v) { 35 | int p = 0, idx = 0; 36 | for (int sz = n >> 1; sz; sz >>= 1) { 37 | if (tree[p + sz - 1] <= v) { 38 | v -= tree[p + sz - 1]; 39 | idx += vis[p + sz - 1]; 40 | p += sz; 41 | } 42 | } 43 | return idx; 44 | } 45 | }; 46 | 47 | -------------------------------------------------------------------------------- /templates/DS/BinaryTrie.cpp: -------------------------------------------------------------------------------- 1 | struct node { 2 | int ch[2]{}, sz = 0; 3 | 4 | int &operator[](int x) { 5 | return ch[x]; 6 | } 7 | }; 8 | 9 | template 10 | struct BT { 11 | vector nodes; 12 | 13 | int newNode() { return nodes.emplace_back(), nodes.size() - 1; } 14 | 15 | void init() { nodes.clear(), newNode(); } 16 | 17 | BT() { init(); } 18 | 19 | /// +1 to add, -1 to delete 20 | void update(ll val, int op) { 21 | int u = 0; 22 | for (int i = M - 1; i >= 0; --i) { 23 | int v = val >> i & 1; 24 | if (!nodes[u][v]) { 25 | nodes[u][v] = newNode(); 26 | } 27 | u = nodes[u][v]; 28 | nodes[u].sz += op; 29 | } 30 | } 31 | 32 | /// Count the number of integers such that a[i] XOR num >= l 33 | ll query(ll num, ll l) { 34 | int u = 0; 35 | ll ans = 0; 36 | for (int i = M - 1; i >= 0; i--) { 37 | int btP = num >> i & 1, btL = l >> i & 1; 38 | if (btL) { 39 | u = nodes[u][!btP]; 40 | } else { 41 | ans += nodes[nodes[u][!btP]].sz; 42 | u = nodes[u][btP]; 43 | } 44 | if (!u)return ans; 45 | } 46 | return ans + nodes[u].sz; 47 | } 48 | 49 | /// maximum value of a[i] XOR x 50 | ll query(int x) { 51 | ll ans = 0; 52 | int u = 0; 53 | for (int i = M - 1; i >= 0; --i) { 54 | int bt = x >> i & 1; 55 | if (nodes[nodes[u][!bt]].sz) { 56 | ans |= (1ll << i); 57 | u = nodes[u][!bt]; 58 | } else { 59 | u = nodes[u][bt]; 60 | } 61 | } 62 | return ans; 63 | } 64 | 65 | }; 66 | -------------------------------------------------------------------------------- /templates/DS/Hilbert Order .cpp: -------------------------------------------------------------------------------- 1 | inline int64_t hilbertOrder(int x, int y, int pow, int rotate) { 2 | if (pow == 0) { 3 | return 0; 4 | } 5 | int hpow = 1 << (pow - 1); 6 | int seg = (x < hpow) ? ( 7 | (y < hpow) ? 0 : 3 8 | ) : ( 9 | (y < hpow) ? 1 : 2 10 | ); 11 | seg = (seg + rotate) & 3; 12 | const int rotateDelta[4] = {3, 0, 0, 1}; 13 | int nx = x & (x ^ hpow), ny = y & (y ^ hpow); 14 | int nrot = (rotate + rotateDelta[seg]) & 3; 15 | int64_t subSquareSize = int64_t(1) << (2 * pow - 2); 16 | int64_t ans = seg * subSquareSize; 17 | int64_t add = hilbertOrder(nx, ny, pow - 1, nrot); 18 | ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1); 19 | return ans; 20 | } 21 | 22 | struct Query { 23 | int l, r, iq; 24 | int64_t ord = -1; 25 | 26 | /// make sure to call it 27 | inline void calcOrder() { 28 | if (ord == -1) 29 | ord = hilbertOrder(l, r, 21, 0); 30 | } 31 | 32 | bool operator<(const Query &other) const { 33 | return ord < other.ord; 34 | } 35 | }; -------------------------------------------------------------------------------- /templates/DS/LineContainer.cpp: -------------------------------------------------------------------------------- 1 | struct Line { 2 | mutable int k, m, p; 3 | 4 | bool operator<(const Line &o) const { return k < o.k; } 5 | 6 | bool operator<(int x) const { return p < x; } 7 | }; 8 | 9 | struct LineContainer : multiset> { 10 | // (for doubles, use inf = 1/.0, div(a,b) = a/b) 11 | static const int inf = LLONG_MAX; 12 | 13 | int div(int a, int b) { // floored division 14 | return a / b - ((a ^ b) < 0 && a % b); 15 | } 16 | 17 | bool isect(iterator x, iterator y) { 18 | if (y == end()) return x->p = inf, 0; 19 | if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; 20 | else x->p = div(y->m - x->m, x->k - y->k); 21 | return x->p >= y->p; 22 | } 23 | 24 | 25 | /// y = a(k) * x + b(m) 26 | void add(int k, int m) { 27 | /// to get the minimum k *= -1 , m *= -1; 28 | auto z = insert({k, m, 0}), y = z++, x = y; 29 | while (isect(y, z)) z = erase(z); 30 | if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); 31 | while ((y = x) != begin() && (--x)->p >= y->p) 32 | isect(x, erase(y)); 33 | } 34 | 35 | /// y = a(k) * x + b(m) => max y 36 | /// !! DON'T call it with negative (x) 37 | int query(int x) { 38 | assert(!empty()); 39 | auto l = *lower_bound(x); 40 | /// to get the minimum return - answer; 41 | return l.k * x + l.m; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /templates/DS/MO with DSU.cpp: -------------------------------------------------------------------------------- 1 | void solve(){ 2 | int n, m; 3 | cin >> n >> m; 4 | const int SQ = ceil(sqrt(m)); 5 | vector> edges(m); 6 | for (auto &[u, v]: edges) { 7 | cin >> u >> v, --u, --v; 8 | } 9 | DSU dsu; 10 | dsu.init(n); 11 | vector>> queries(SQ); 12 | int q; 13 | cin >> q; 14 | int ans[q]; 15 | for (int i = 0, l, r; i < q; ++i) { 16 | cin >> l >> r; 17 | --l, --r; 18 | if (r - l + 1 <= SQ) { 19 | dsu.snapshot(); 20 | while (l <= r) { 21 | dsu.merge(edges[l].first, edges[l].second); 22 | ++l; 23 | } 24 | ans[i] = dsu.comp; 25 | dsu.rollback(); 26 | } else { 27 | queries[l / SQ].push_back({r, l, i}); 28 | } 29 | } 30 | for (auto &x: queries) { 31 | sort(x.begin(), x.end()); 32 | } 33 | for (int b = 1; b <= SQ; ++b) { 34 | if (queries[b - 1].empty())continue; 35 | dsu.snapshot(); 36 | int R = min(b * SQ, m) - 1; 37 | dsu.merge(edges[R].first, edges[R].second); 38 | for (const auto [rq, lq, iq]: queries[b - 1]) { 39 | while (R < rq) { 40 | ++R; 41 | dsu.merge(edges[R].first, edges[R].second); 42 | } 43 | dsu.snapshot(); 44 | int L = min(m, b * SQ) - 1; 45 | while (lq < L) { 46 | --L; 47 | dsu.merge(edges[L].first, edges[L].second); 48 | } 49 | ans[iq] = dsu.comp; 50 | dsu.rollback(); 51 | } 52 | dsu.rollback(); 53 | } 54 | for (int &x: ans) { 55 | cout << x << '\n'; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /templates/DS/MO with SQRT trick.cpp: -------------------------------------------------------------------------------- 1 | const int SQ, N; 2 | int blk[SQ + 1], frq[N + 1]; 3 | 4 | struct Query { 5 | int l, r, iq; 6 | 7 | bool operator<(const Query &other) const { 8 | int n1 = l / SQ, n2 = other.l / SQ; 9 | if (n1 != n2) return n1 < n2; 10 | return (n1 % 2) ? r > other.r : r < other.r; 11 | } 12 | }; 13 | 14 | void update(int val) { 15 | if (frq[val] == 1) { 16 | blk[val / SQ] += 1; 17 | } else if (frq[val] == 0) { 18 | blk[val / SQ] -= 1; 19 | } 20 | } 21 | 22 | void add(int i) { 23 | i = arr[i]; 24 | frq[i]++; 25 | update(i); 26 | } 27 | 28 | void rem(int i) { 29 | i = arr[i]; 30 | frq[i]--; 31 | update(i); 32 | } 33 | 34 | int getMax() { 35 | int idx = SQ - 1; 36 | while (~idx && !blk[idx])--idx; 37 | if (idx == -1) return 0; /// empty subarray 38 | idx = (idx + 1) * SQ - 1; 39 | while (!frq[idx])--idx; 40 | return idx; 41 | } 42 | 43 | int getMin() { 44 | int idx = 0; 45 | while (!blk[idx]) ++idx; 46 | idx *= SQ; 47 | while (!frq[idx])++idx; 48 | return idx; 49 | } 50 | 51 | int getMex() { 52 | int idx = 0; 53 | while (blk[idx] == SQ) ++idx; 54 | idx *= SQ; 55 | while (frq[idx])++idx; 56 | return idx; 57 | } 58 | 59 | vector MO(vector &queries) { 60 | sort(queries.begin(), queries.end()); 61 | vector res(queries.size()); 62 | int l = queries[0].l, r = queries[0].l; 63 | add(l); 64 | for (const auto &[lq, rq, iq]: queries) { 65 | while (r < rq) add(++r); 66 | while (l > lq) add(--l); 67 | while (r > rq) rem(r--); 68 | while (l < lq) rem(l++); 69 | res[iq] = getMex(); 70 | } 71 | return res; 72 | } 73 | -------------------------------------------------------------------------------- /templates/DS/MergeSortTree.cpp: -------------------------------------------------------------------------------- 1 | #define all(V) V.begin(), V.end() 2 | 3 | struct MergeSortTree { 4 | private: 5 | #define md ((lx+rx)>>1) 6 | #define LF (x*2+1) 7 | #define RT (x*2+2) 8 | vector> seg; 9 | int sz; 10 | 11 | void build(vector &arr, int x, int lx, int rx) { 12 | if (rx == lx) { 13 | seg[x] = {arr[lx]}; 14 | return; 15 | } 16 | build(arr, LF, lx, md); 17 | build(arr, RT, md + 1, rx); 18 | seg[x].resize(seg[LF].size() + seg[RT].size()); 19 | merge(all(seg[LF]), all(seg[RT]), seg[x].begin()); 20 | } 21 | 22 | int query(int l, int r, int v, int x, int lx, int rx) { 23 | if (r < lx or rx < l)return 0; 24 | if (l <= lx and rx <= r) { 25 | return lower_bound(all(seg[x]), v) - seg[x].begin(); //// TODO 26 | } 27 | return query(l, r, v, LF, lx, md) + 28 | query(l, r, v, RT, md + 1, rx); 29 | } 30 | 31 | public: 32 | void build(vector &arr) { 33 | sz = arr.size(); 34 | seg.assign(sz * 4, {}); 35 | build(arr, 0, 0, sz - 1); 36 | } 37 | 38 | int query(int l, int r, int v) { 39 | return query(l, r, v, 0, 0, sz - 1); 40 | } 41 | 42 | #undef md 43 | #undef LF 44 | #undef RT 45 | }; 46 | -------------------------------------------------------------------------------- /templates/DS/Monotonic Queue.cpp: -------------------------------------------------------------------------------- 1 | struct mono_queue { 2 | private: 3 | queue main_queue; 4 | deque min_queue; 5 | public: 6 | void push(int x) { 7 | main_queue.push(x); 8 | while (!min_queue.empty() && x < min_queue.back()) { 9 | min_queue.pop_back(); 10 | } 11 | min_queue.push_back(x); 12 | } 13 | 14 | int pop() { 15 | int n = main_queue.front(); 16 | if (n == min_queue.front()) { 17 | min_queue.pop_front(); 18 | } 19 | main_queue.pop(); 20 | return n; 21 | } 22 | 23 | int get_min() { return min_queue.front(); } 24 | 25 | int front() { return main_queue.front(); } 26 | 27 | int size() { return main_queue.size(); } 28 | }; 29 | -------------------------------------------------------------------------------- /templates/DS/Multiset.cpp: -------------------------------------------------------------------------------- 1 | class Multiset { 2 | private: 3 | //use with the positive number only and limited 4 | // so if you have a big or non-positive number you have to compress them 5 | vector Bit; 6 | int SZ, size_set; 7 | 8 | void add(int pos, int val) { 9 | for (++pos; pos <= SZ; pos += pos & -pos) 10 | Bit[pos - 1] += val; 11 | } 12 | 13 | int get(int pos) { 14 | int ret = 0; 15 | for (++pos; pos; pos -= pos & -pos) 16 | ret += Bit[pos - 1]; 17 | return ret; 18 | } 19 | 20 | int BS(int val) { 21 | int s = 0; 22 | for (int sz = SZ >> 1; sz; sz >>= 1) { 23 | if (Bit[s + sz - 1] < val) 24 | val -= Bit[(s += sz) - 1]; 25 | } 26 | return s; 27 | } 28 | 29 | public: 30 | Multiset() : size_set(0), SZ(1 << 20) { 31 | Bit.resize(SZ); 32 | add(0, -1); 33 | } 34 | 35 | void insert(int val) { 36 | ++size_set; 37 | add(val, 1); 38 | } 39 | 40 | int count(int val) { 41 | return get(val) - get(val - 1); 42 | } 43 | 44 | /// erase all occurrence of val in the multiset 45 | void erase_all(int val) { 46 | int c = count(val); 47 | size_set -= c; 48 | add(val, -c); 49 | } 50 | 51 | void erase_idx(int index) { 52 | --size_set; 53 | add(BS(index), -1); 54 | } 55 | 56 | int order_of_key(int val) {//get_val_idx //freq(numbers)l = this->r = nullptr, h = 0; 12 | if (l) this->l = l, h = pw1[lvl] * l->h; 13 | if (r) this->r = r, h += pw2[lvl] * r->h; 14 | } 15 | 16 | node(ull val) { 17 | l = r = nullptr; 18 | h = ((ull) (1e9 + 345) ^ val) * (3 * val + 654); 19 | } 20 | 21 | }; 22 | 23 | node *update(int val, node *x, int lvl) { 24 | if (!~lvl) { 25 | if (x)return nullptr; 26 | return new node(val); 27 | } 28 | if (val & (1 << lvl)) { 29 | return new node( 30 | x ? x->l : nullptr, 31 | update(val, x ? x->r : nullptr, lvl - 1), 32 | lvl + 1 33 | ); 34 | } 35 | return new node( 36 | update(val, x ? x->l : nullptr, lvl - 1), 37 | x ? x->r : nullptr, 38 | lvl + 1 39 | ); 40 | } 41 | 42 | int query(node *l, node *r, int msk, int lvl) { 43 | if ((l ? l->h : 0) == (r ? r->h : 0)) return -1; 44 | if (!~lvl) return msk; 45 | int lf = query(l ? l->l : l, r ? r->l : r, msk, lvl - 1); 46 | if (~lf) return lf; 47 | return query(l ? l->r : l, r ? r->r : r, msk | (1 << lvl), lvl - 1); 48 | } 49 | 50 | void pre() { 51 | pw1[0] = pw2[0] = 1; 52 | for (int i = 1; i < 32; ++i) { 53 | pw1[i] = pw1[i - 1] * 31; 54 | pw2[i] = pw2[i - 1] * 53; 55 | } 56 | } -------------------------------------------------------------------------------- /templates/DS/Persistent Segment Tree.cpp: -------------------------------------------------------------------------------- 1 | namespace PSegTree { 2 | #define md ((lx+rx)>>1) 3 | 4 | struct Node { 5 | Node *l = 0, *r = 0; 6 | int val = 0; 7 | 8 | Node(int x) : val(x), l(nullptr), r(nullptr) {} 9 | 10 | Node(Node *l, Node *r) { 11 | this->l = l, this->r = r; 12 | val = 0; 13 | if (l) val += l->val; 14 | if (r) val += r->val; 15 | } 16 | 17 | Node(Node *cp) : val(cp->val), l(cp->l), r(cp->r) {} 18 | }; 19 | 20 | int n; 21 | 22 | Node *build(const vector &a, int lx = 0, int rx = n - 1) { 23 | if (lx == rx) return new Node(a[lx]); 24 | return new Node(build(a, lx, md), build(a, md + 1 + 1, rx)); 25 | } 26 | 27 | Node *update(Node *node, int val, int pos, int lx = 0, int rx = n - 1) { 28 | if (lx == rx) return new Node(val); 29 | if (pos > md) { 30 | return new Node(node->l, update(node->r, val, pos, md + 1, rx)); 31 | } 32 | return new Node(update(node->l, val, pos, lx, md), node->r); 33 | } 34 | 35 | int query(Node *node, int l, int r, int lx = 0, int rx = n - 1) { 36 | if (r < lx || rx < l) return 0; 37 | if (l <= lx && rx <= r) return node->val; 38 | return query(node->l, l, r, lx, md) + query(node->r, l, r, md + 1, rx); 39 | } 40 | 41 | #undef md 42 | } 43 | -------------------------------------------------------------------------------- /templates/DS/SegTree.cpp: -------------------------------------------------------------------------------- 1 | struct SegTree { 2 | #define LF (x*2+1) 3 | #define RT (x*2+2) 4 | #define md ((lx+rx) >> 1) 5 | vector seg, lazy; 6 | int ign; 7 | int sz; 8 | 9 | SegTree(int n) { 10 | sz = n; 11 | ign = 2e9; /// TODO::update this 12 | seg.assign(4 * n, {}); 13 | lazy.assign(4 * n, {}); 14 | } 15 | 16 | int merge(const int &lf, const int &rt) { 17 | return min(lf, rt); /// update this 18 | } 19 | 20 | void propagate(int x, int lx, int rx) { 21 | if (!lazy[x])return; 22 | if (lx != rx) { 23 | lazy[LF] += lazy[x]; 24 | lazy[RT] += lazy[x]; 25 | } 26 | seg[x] += lazy[x]; //// add lazy to the node 27 | lazy[x] = 0; //// set lazy value 28 | } 29 | 30 | void update(int l, int r, int v, int x = 0, int lx = 0, int rx = -1) { 31 | if (rx == -1)rx = sz - 1; 32 | propagate(x, lx, rx); 33 | if (l <= lx and rx <= r) { 34 | lazy[x] += v; //// add lazy to the node 35 | propagate(x, lx, rx); 36 | return; 37 | } 38 | if (r < lx or rx < l)return; 39 | update(l, r, v, LF, lx, md); 40 | update(l, r, v, RT, md + 1, rx); 41 | seg[x] = merge(seg[LF], seg[RT]); 42 | } 43 | 44 | int query(int l, int r, int x = 0, int lx = 0, int rx = -1) { 45 | if (rx == -1)rx = sz - 1; 46 | propagate(x, lx, rx); 47 | if (l <= lx and rx <= r) return seg[x]; 48 | if (r < lx or rx < l) return ign; 49 | return merge( 50 | query(l, r, LF, lx, md), 51 | query(l, r, RT, md + 1, rx) 52 | ); 53 | } 54 | 55 | #undef md 56 | #undef LF 57 | #undef RT 58 | }; 59 | -------------------------------------------------------------------------------- /templates/DS/SegTree2D.cpp: -------------------------------------------------------------------------------- 1 | struct SegTree2d { 2 | int n, m; 3 | vector> seg; 4 | 5 | int neutral() const { return 1e8; } 6 | 7 | int merge(int a, int b) const { return min(a, b); } 8 | 9 | SegTree2d(int n, int m) : n(n), m(m) { 10 | seg = vector>(2 * n, vector(2 * m, neutral())); 11 | } 12 | 13 | int qry(int x1, int y1, int x2, int y2) const { 14 | int ret = neutral(); 15 | int y3 = y1 + m, y4 = y2 + m; 16 | for (x1 += n, x2 += n; x1 <= x2; ++x1 /= 2, --x2 /= 2) { 17 | for (y1 = y3, y2 = y4; y1 <= y2; ++y1 /= 2, --y2 /= 2) { 18 | if ((x1 & 1) and (y1 & 1)) 19 | ret = merge(ret, seg[x1][y1]); 20 | if ((x1 & 1) and !(y2 & 1)) 21 | ret = merge(ret, seg[x1][y2]); 22 | if (!(x2 & 1) and (y1 & 1)) 23 | ret = merge(ret, seg[x2][y1]); 24 | if (!(x2 & 1) and !(y2 & 1)) 25 | ret = merge(ret, seg[x2][y2]); 26 | } 27 | } 28 | return ret; 29 | } 30 | 31 | void upd(int x, int y, int val) { 32 | int y2 = y += m; 33 | for (x += n; x; x /= 2, y = y2) { 34 | if (x >= n) 35 | seg[x][y] = val; 36 | else 37 | seg[x][y] = merge(seg[2 * x][y], seg[2 * x + 1][y]); 38 | while (y /= 2) 39 | seg[x][y] = merge(seg[x][2 * y], seg[x][2 * y + 1]); 40 | } 41 | } 42 | }; -------------------------------------------------------------------------------- /templates/DS/SegTreeSD.cpp: -------------------------------------------------------------------------------- 1 | /// update point / query range 2 | struct SegTree { 3 | vector seg; 4 | int N; 5 | int ign; 6 | 7 | SegTree(int n) { 8 | N = 1; 9 | while (N <= n)N <<= 1; 10 | ign = 0; /// update this 11 | seg.assign(N << 1, ign); 12 | } 13 | 14 | int mrg(int a, int b) { return a + b; } 15 | 16 | void update(int i, int val) { 17 | seg[i += N - 1] = val; /// update this 18 | while ((i >>= 1) >= 1) { 19 | seg[i] = mrg(seg[i << 1], seg[i << 1 | 1]); 20 | } 21 | } 22 | 23 | int query(int l, int r) { 24 | int ret = ign, k = 0; 25 | l += N - 2, r += N - 1; 26 | while ((l += (1 << k)) <= r) { 27 | ret = mrg(ret, seg[l >> (k = min(__lg(l & -l), __lg(r - l + 1)))]); 28 | } 29 | return ret; 30 | } 31 | }; -------------------------------------------------------------------------------- /templates/DS/SparsTable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | SparseTable sp(a, [](int a, int b) { 3 | return max(a, b); 4 | }); 5 | */ 6 | 7 | template> 8 | class SparseTable { 9 | public: 10 | int n; 11 | vector> sp; 12 | CMP func; 13 | 14 | SparseTable(const vector &a, const CMP &f) : func(f) { 15 | n = a.size(); 16 | int max_log = 32 - __builtin_clz(n); 17 | sp.resize(max_log); 18 | sp[0] = a; 19 | for (int j = 1; j < max_log; ++j) { 20 | sp[j].resize(n - (1 << j) + 1); 21 | for (int i = 0; i + (1 << j) <= n; ++i) { 22 | sp[j][i] = func( 23 | sp[j - 1][i], 24 | sp[j - 1][i + (1 << (j - 1))] 25 | ); 26 | } 27 | } 28 | } 29 | 30 | T get(int l, int r) const { 31 | int lg = __lg(r - l + 1); 32 | return func( 33 | sp[lg][l], 34 | sp[lg][r - (1 << lg) + 1] 35 | ); 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /templates/DS/order_set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace __gnu_pbds; 5 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 6 | -------------------------------------------------------------------------------- /templates/Graph/BFS01.cpp: -------------------------------------------------------------------------------- 1 | void bfs01(int src) { 2 | const int n = adj.size(); 3 | const int INF = 1e9; 4 | vector d(n, INF); 5 | d[src] = 0; 6 | deque q; 7 | q.push_front(src); 8 | while (!q.empty()) { 9 | int v = q.front(); 10 | q.pop_front(); 11 | for (auto edge: adj[v]) { 12 | int u = edge.first; 13 | int w = edge.second; 14 | if (d[v] + w < d[u]) { 15 | d[u] = d[v] + w; 16 | if (w == 1) 17 | q.push_back(u); 18 | else 19 | q.push_front(u); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /templates/Graph/Bridges.cpp: -------------------------------------------------------------------------------- 1 | /// O(N + M) 2 | int n; 3 | vector> adj; 4 | 5 | vector visited; 6 | vector tin, low; 7 | int timer; 8 | 9 | void dfs(int v, int p = -1) { 10 | visited[v] = true; 11 | tin[v] = low[v] = timer++; 12 | for (int to: adj[v]) { 13 | if (to == p) continue; 14 | if (visited[to]) { 15 | low[v] = min(low[v], tin[to]); 16 | } else { 17 | dfs(to, v); 18 | low[v] = min(low[v], low[to]); 19 | if (low[to] > tin[v]) 20 | IS_BRIDGE(v, to); 21 | } 22 | } 23 | } 24 | 25 | void find_bridges() { 26 | timer = 0; 27 | visited.assign(n, false); 28 | tin.assign(n, -1); 29 | low.assign(n, -1); 30 | for (int i = 0; i < n; ++i) { 31 | if (!visited[i]) 32 | dfs(i); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /templates/Graph/DSU With Rollback.cpp: -------------------------------------------------------------------------------- 1 | struct DSU { 2 | vector par, sz, checkPoints; 3 | vector> updates; 4 | 5 | void init(int n) { 6 | par.assign(n + 1, {}); 7 | iota(par.begin(), par.end(), 0); 8 | sz.assign(n + 1, 1); 9 | checkPoints.clear(); 10 | updates.clear(); 11 | } 12 | 13 | int find(int u) { 14 | while (u ^ par[u]) { 15 | u = par[u]; 16 | } 17 | return u; 18 | } 19 | 20 | bool merge(int u, int v) { 21 | u = find(u), v = find(v); 22 | if (u == v) { 23 | return false; 24 | } 25 | if (sz[u] < sz[v]) { 26 | swap(u, v); 27 | } 28 | updates.emplace_back(v, u); 29 | sz[u] += sz[v]; 30 | par[v] = u; 31 | return true; 32 | } 33 | 34 | void snapshot() { 35 | checkPoints.emplace_back(updates.size()); 36 | } 37 | 38 | void rollback() { 39 | if (checkPoints.empty()) { 40 | return; 41 | } 42 | while (checkPoints.back() != updates.size()) { 43 | auto [v, u] = updates.back(); 44 | updates.pop_back(); 45 | sz[u] -= sz[v]; 46 | par[v] = v; 47 | } 48 | checkPoints.pop_back(); 49 | } 50 | 51 | int size(int u) { 52 | return sz[find(u)]; 53 | } 54 | 55 | int sameSet(int u, int v) { 56 | return find(u) == find(v); 57 | } 58 | 59 | }; 60 | -------------------------------------------------------------------------------- /templates/Graph/Dijkstra.cpp: -------------------------------------------------------------------------------- 1 | const int INF = 1e17; 2 | vector>> adj; // [v,w] 3 | 4 | vector dijkstra(int s, vector &par) { 5 | int n = adj.size(); 6 | vector vis(n, INF); 7 | par.resize(n, -1); 8 | vis[s] = 0; 9 | set> st = {{0, s}}; 10 | while (st.size()) { 11 | auto [d, u] = *st.begin(); 12 | st.erase(st.begin()); 13 | for (auto [v, w]: adj[u]) { 14 | int dd = w + d; 15 | if (vis[v] > dd) { 16 | par[v] = u; 17 | st.erase({vis[v], v}); 18 | st.insert({vis[v] = dd, v}); 19 | } 20 | } 21 | } 22 | return vis; 23 | } 24 | -------------------------------------------------------------------------------- /templates/Graph/Euler Path Directed.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | all the edges should be in the same connected component 3 | #directed graph: euler path: for all -> indeg = outdeg or nodes having indeg > outdeg = outdeg > indeg = 1 and for others in = out 4 | #directed graph: euler circuit: for all -> indeg = outdeg 5 | */ 6 | 7 | vector g[N], ans; 8 | int done[N]; 9 | 10 | void dfs(int u) { 11 | while (done[u] < g[u].size()) dfs(g[u][done[u]++]); 12 | ans.push_back(u); 13 | } 14 | 15 | int solve(int n) { 16 | int edges = 0; 17 | vector in(n + 1, 0), out(n + 1, 0); 18 | for (int u = 1; u <= n; u++) { 19 | for (auto v: g[u]) 20 | in[v]++, out[u]++, edges++; 21 | } 22 | int ok = 1, cnt1 = 0, cnt2 = 0, root = 0; 23 | for (int i = 1; i <= n; i++) { 24 | if (in[i] - out[i] == 1) cnt1++; 25 | if (out[i] - in[i] == 1) cnt2++, root = i; 26 | if (abs(in[i] - out[i]) > 1) ok = 0; 27 | } 28 | if (cnt1 > 1 || cnt2 > 1) ok = 0; 29 | if (!ok) return 0; 30 | if (root == 0) { 31 | for (int i = 1; i <= n; i++) if (out[i]) root = i; 32 | } 33 | if (root == 0) return 1; //empty graph 34 | dfs(root); 35 | if (ans.size() != edges + 1) return 0; //connectivity 36 | reverse(ans.begin(), ans.end()); 37 | return 1; 38 | } 39 | -------------------------------------------------------------------------------- /templates/Graph/Euler Path Undirected.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | all the edges should be in the same connected component 3 | #undirected graph: euler path: all degrees are even or exactly two of them are odd. 4 | #undirected graph: euler circuit: all degrees are even 5 | */ 6 | vector> g[N]; // 1-based 7 | vector ans; 8 | int done[N]; 9 | int vis[N * N]; //number of edges 10 | void dfs(int u) { 11 | while (done[u] < g[u].size()) { 12 | auto e = g[u][done[u]++]; 13 | if (vis[e.second]) continue; 14 | vis[e.second] = 1; 15 | dfs(e.first); 16 | } 17 | ans.push_back(u); 18 | } 19 | 20 | int solve(int n) { 21 | int edges = 0; 22 | ans.clear(); 23 | memset(done, 0, sizeof done); 24 | memset(vis, 0, sizeof vis); 25 | vector deg(n + 1, 0); 26 | for (int u = 1; u <= n; u++) { 27 | for (auto e: g[u]) { 28 | deg[e.first]++, deg[u]++, edges++; 29 | } 30 | } 31 | int odd = 0, root = 0; 32 | for (int i = 1; i <= n; i++) { 33 | if (deg[i] & 1) odd++, root = i; 34 | } 35 | if (odd > 2) return 0; 36 | if (root == 0) { 37 | for (int i = 1; i <= n; i++) if (deg[i]) root = i; 38 | } 39 | if (root == 0) return 1; //empty graph 40 | dfs(root); 41 | if (ans.size() != edges / 2 + 1) return 0; 42 | reverse(ans.begin(), ans.end()); 43 | return 1; 44 | } 45 | -------------------------------------------------------------------------------- /templates/Graph/Floyd-Warshall.cpp: -------------------------------------------------------------------------------- 1 | /// Don't forget to set the adj initially to INF (INF + INF shouldn't overflow) 2 | void floyd() { 3 | int i, j, k; 4 | for (i = 0; i < N; ++i) 5 | adj[i][i] = 0; 6 | for (k = 0; k < N; ++k) for (i = 0; i < N; ++i) 7 | for (j = 0; j < N; ++j) 8 | adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); 9 | } 10 | -------------------------------------------------------------------------------- /templates/Graph/Hungarian.cpp: -------------------------------------------------------------------------------- 1 | #define ll long long 2 | const ll oo = 1e17; 3 | typedef vector vll; 4 | #define sz(x) ((int)(x).size()) 5 | 6 | pair solve(const vector &a) { 7 | if (a.empty()) return {0, {}}; 8 | int n = sz(a) + 1, m = sz(a[0]) + 1; 9 | vll p(m), ans(n - 1); 10 | vll u(n), v(m); 11 | for (int i = 1; i < n; ++i) { 12 | p[0] = i; 13 | int j0 = 0; 14 | vll dist(m, LONG_LONG_MAX); 15 | vll pre(m, -1); 16 | vector done(m + 1); 17 | do { 18 | done[j0] = true; 19 | int i0 = p[j0], j1; 20 | ll delta = LONG_LONG_MAX; 21 | for (int j = 1; j < m; ++j) { 22 | if (!done[j]) { 23 | auto cur = a[i0 - 1][j - 1] - u[i0] - v[j]; 24 | if (cur < dist[j]) dist[j] = cur, pre[j] = j0; 25 | if (dist[j] < delta) delta = dist[j], j1 = j; 26 | } 27 | } 28 | for (int j = 0; j < m; ++j) { 29 | if (done[j]) u[p[j]] += delta, v[j] -= delta; 30 | else dist[j] -= delta; 31 | } 32 | j0 = j1; 33 | } while (p[j0]); 34 | while (j0) { 35 | int j1 = pre[j0]; 36 | p[j0] = p[j1], j0 = j1; 37 | } 38 | } 39 | for (int j = 1; j < m; ++j) { 40 | if (p[j]) { 41 | ans[p[j] - 1] = j - 1; 42 | } 43 | } 44 | return {-v[0], ans}; // min cost 45 | } 46 | -------------------------------------------------------------------------------- /templates/Graph/Kth Shortest Path.cpp: -------------------------------------------------------------------------------- 1 | struct edge { 2 | int s, e, c; //start, end, cost 3 | bool operator<(const edge &e) const { return c < e.c; } 4 | }; 5 | 6 | const int SIZE = 100; //max nodes number 7 | int N, start, sink, K; //find the k-th shortest path from start to end 8 | int adj[SIZE][SIZE]; //this can be adjList instead of adjMatrix 9 | 10 | //Returns -1 of no k-th shortest path exist between start and end 11 | int getKthShortestPath() { 12 | multiset pq; //first is cost and second is node 13 | edge e = {-1, start, 0}; 14 | pq.insert(e); 15 | vector reached[N]; 16 | //reached[i][j] is the cost of the j-th shortest path from start to i 17 | while (!pq.empty()) { 18 | edge e = *pq.begin(); 19 | pq.erase(pq.begin()); 20 | if (reached[e.e].size() >= K)continue; 21 | reached[e.e].push_back(e.c); 22 | for (int i = 0; i < N; i++) { 23 | if (adj[e.e][i] == -1)continue; 24 | edge ne = {e.e, i, e.c + adj[e.e][i]}; 25 | pq.insert(ne); 26 | } 27 | } 28 | // no k-th path exist between start and end 29 | return reached[sink].size() >= K ? reached[sink].back() : -1; 30 | } 31 | 32 | void solve() { 33 | /// set N, start, end, K, dist 34 | /// Initialize adjacency matrix with -1 (not adjacent) 35 | memset(adj, -1, sizeof adj); 36 | 37 | int d = getKthShortestPath(); 38 | } 39 | -------------------------------------------------------------------------------- /templates/Graph/Kuhn.cpp: -------------------------------------------------------------------------------- 1 | vector> adj; 2 | vector mt; 3 | vector used; 4 | 5 | bool try_kuhn(int v) { 6 | if (used[v]) return false; 7 | used[v] = true; 8 | for (int to: adj[v]) { 9 | if (mt[to] == -1 || try_kuhn(mt[to])) { 10 | mt[to] = v; 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | 17 | int run() { 18 | int n = adj.size(); 19 | mt.assign(n, -1); 20 | for (int v = 0; v < n; ++v) { 21 | used.assign(n, false); 22 | try_kuhn(v); 23 | } 24 | int res{}; 25 | for (int x: mt) res += (x != -1); 26 | return res; 27 | } 28 | -------------------------------------------------------------------------------- /templates/Graph/PersistentDSU.cpp: -------------------------------------------------------------------------------- 1 | struct DSU { 2 | vector>> par; 3 | int time = 0; // initial time 4 | DSU(int n) : par(n + 1, {{-1, 0}}) {} 5 | 6 | bool merge(int u, int v) { 7 | ++time; 8 | if ((u = root(u, time)) == (v = root(v, time))) return 0; 9 | if (par[u].back().first > par[v].back().first) 10 | swap(u, v); 11 | par[u].emplace_back(par[u].back().first + par[v].back().first, time); 12 | par[v].emplace_back(u, time); //par[v] = u 13 | return 1; 14 | } 15 | 16 | bool same(int u, int v, int t) { 17 | return root(u, t) == root(v, t); 18 | } 19 | 20 | int root(int u, int t) { 21 | if (par[u].back().first >= 0 && par[u].back().second <= t) 22 | return root(par[u].back().first, t); 23 | return u; 24 | } 25 | 26 | int size(int u, int t) { //size of the component of u at time t 27 | u = root(u, t); 28 | int l = 0, r = (int) par[u].size() - 1, ans = 0; 29 | while (l <= r) { 30 | int mid = l + r >> 1; 31 | if (par[u][mid].second <= t) ans = mid, l = mid + 1; 32 | else r = mid - 1; 33 | } 34 | return -par[u][ans].first; 35 | } 36 | 37 | int first_time_to_be_same(int u, int v) { 38 | int ans = -1, l = 0, r = time; 39 | while (l <= r) { 40 | int mid = l + r >> 1; 41 | if (same(u, v, mid)) ans = mid, r = mid - 1; 42 | else l = mid + 1; 43 | } 44 | return ans; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /templates/Graph/SPFA.cpp: -------------------------------------------------------------------------------- 1 | const ll INF = 1e18; 2 | vector>> adj; 3 | 4 | bool SPFA(int s, vector &d) { 5 | int n = adj.size(); 6 | d.assign(n, INF); 7 | vector cnt(n, 0); 8 | vector inqueue(n, false); 9 | queue q; 10 | d[s] = 0; 11 | q.push(s); 12 | inqueue[s] = true; 13 | while (!q.empty()) { 14 | int u = q.front(); 15 | q.pop(); 16 | inqueue[u] = false; 17 | for (auto [v, w]: adj[u]) { 18 | if (d[u] + w >= d[v]) continue; 19 | d[v] = d[u] + w; 20 | if (!inqueue[v]) { 21 | q.push(v); 22 | inqueue[v] = true; 23 | cnt[v]++; 24 | if (cnt[v] > n) 25 | return false; 26 | } 27 | } 28 | } 29 | return true; 30 | } 31 | -------------------------------------------------------------------------------- /templates/Graph/Topological.cpp: -------------------------------------------------------------------------------- 1 | vector > adj; 2 | vector in; /// in-degree of each node 3 | int n; // number of nodes 4 | vector res; 5 | 6 | bool Topological() { 7 | queue q; 8 | res.clear(); 9 | for (int i = 0; i < n; ++i) 10 | if (in[i] == 0) q.push(i); 11 | while (!q.empty()) { 12 | int u = q.front(); 13 | q.pop(); 14 | res.emplace_back(u); 15 | for (int v: adj[u]) 16 | if (!--in[v]) 17 | q.push(v); 18 | } 19 | return res.size() == n; 20 | } 21 | -------------------------------------------------------------------------------- /templates/Linear Algebra/MatPower.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * steps to solve matrix power problems 4 | * write iterative dp with memory reduction 5 | * lets say i want to write 6 | * new_dp[x] = 3*dp[i-1] 7 | * int the transition matrix this is equal to 8 | * trans[i-1][x] =3; 9 | * 10 | * to keep old value of a number make a cycle for example let 11 | * we want to get sum of all number at most k 12 | * we add extra row let it's index be n and answer of f(n) at row(n-1) 13 | * so we want each time increase dp[n] by dp[n-1] 14 | * new_dp[n] = dp[n-1] +dp[n] 15 | * so we can write it like this 16 | * trans[n][n] =1 ,this mean add the old of dp[n], 17 | * trans[n-1][n]= 1 --> new_dp[n]+=dp[n-1] 18 | * 19 | * 20 | * to add constant number p to dp[n-1] 21 | * answer is at dp[n-1] and p is at n 22 | * trans[n][n] = 1;//dp[n]=1 23 | * trans[n][n - 1] = p;//dp[n-1]+=p 24 | * 25 | * to add i*q for loop in matrix 26 | * trans[n + 1][n + 1] = 1;//new_dp[n+1]=dp[n+1] 27 | * trans[n][n] = 1;//dp[n]=1 28 | * trans[n][n + 1] = 1;//new_dp[n+1]+=1 29 | * trans[n + 1][n - 1] = q;//new_dp[n-1]+=i*q 30 | * 31 | * //i*i*r 32 | * trans[n + 2][n + 2] = 1; 33 | * trans[n + 1][n + 2] = 2; 34 | * trans[n][n + 2] = 1; 35 | * trans[n + 2][n - 1] = r; 36 | */ 37 | typedef vector row; 38 | typedef vector mat; 39 | 40 | mat operator*(const mat &a, const mat &b) { 41 | int r1 = a.size(), r2 = b.size(), c2 = b[0].size(); 42 | mat ret(r1, row(c2)); 43 | for (int i = 0; i < r1; i++) 44 | for (int j = 0; j < c2; j++) 45 | for (int k = 0; k < r2; k++) 46 | ret[i][j] += a[i][k] * b[k][j], ret[i][j] %= mod; 47 | return ret; 48 | } 49 | 50 | mat operator^(const mat &a, ll k) { 51 | mat ret(a.size(), row(a.size())); 52 | for (int i = 0; i < a.size(); i++)ret[i][i] = 1; 53 | for (mat tmp = a; k; tmp = tmp * tmp, k /= 2)if (k & 1)ret = ret * tmp; 54 | return ret; 55 | } 56 | -------------------------------------------------------------------------------- /templates/Linear Algebra/XOR Basis.cpp: -------------------------------------------------------------------------------- 1 | template 2 | struct Basis { 3 | T a[B]; 4 | Basis() { 5 | memset(a, 0, sizeof a); 6 | } 7 | void insert(T x){ 8 | for (int i = B - 1; i >= 0; i--) { 9 | if (x >> i & 1) { 10 | if (a[i]) x ^= a[i]; 11 | else { 12 | a[i] = x; 13 | break; 14 | } 15 | } 16 | } 17 | } 18 | bool can(T x) { 19 | for(int i = B - 1; i >= 0; i--) { 20 | x = min(x, x ^ a[i]); 21 | } 22 | return x == 0; 23 | } 24 | T max_xor(T ans = 0) { 25 | for(int i = B - 1; i >= 0; i--) { 26 | ans = max(ans, ans ^ a[i]); 27 | } 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /templates/Number Theory/A List of Useful Equations in Competitive Programming _ Inception.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/templates/Number Theory/A List of Useful Equations in Competitive Programming _ Inception.pdf -------------------------------------------------------------------------------- /templates/Number Theory/Bazout's Identity.cpp: -------------------------------------------------------------------------------- 1 | For nonzero integers 𝑎 a and 𝑏 b, let 𝑑 d be the greatest common divisor 𝑑 = gcd ⁡ ( 𝑎 , 𝑏 ) d=gcd(a,b). Then, there exist integers 𝑥 x and 𝑦 y such that 𝑎 𝑥 + 𝑏 𝑦 = 𝑑 . ax+by=d. 2 | -------------------------------------------------------------------------------- /templates/Number Theory/CRT.cpp: -------------------------------------------------------------------------------- 1 | #define ll long long 2 | #define M first 3 | #define R second 4 | typedef pair mod_eq; 5 | 6 | void nxt_r(ll &r0, ll &r1, ll &r) { 7 | int r2 = r0 - r * r1; 8 | r0 = r1, r1 = r2; 9 | } 10 | 11 | // r0 = a , r1 = b 12 | // return gcd = a * x0 + b * y0 13 | ll egcd(ll r0, ll r1, ll &x0, ll &y0) { 14 | ll x1 = y0 = 0, y1 = x0 = 1; 15 | while (r1) { 16 | ll r = r0 / r1; 17 | nxt_r(r0, r1, r); 18 | nxt_r(x0, x1, r); 19 | nxt_r(y0, y1, r); 20 | } 21 | return r0; 22 | } 23 | 24 | // c = x * a + y * b 25 | // x` = x - (b / g) * k , y` = y + (a / g) * k - for any k 26 | bool solveLDE(ll a, ll b, ll c, ll &x, ll &y, ll &g) { 27 | g = egcd(a, b, x, y); 28 | ll m = c / g; 29 | x *= m, y *= m; 30 | return m * g == c; 31 | } 32 | 33 | bool mod_inv(ll a, ll mod, ll &x) { 34 | ll y, g = egcd(a, mod, x, y); 35 | if (g != 1) { 36 | return false; // a and mod are not co-prime 37 | } 38 | x %= mod; 39 | if (x < 0)x += mod; 40 | return true; 41 | } 42 | 43 | bool CRT(const mod_eq &e1, const mod_eq &e2, mod_eq &res) { 44 | ll q1, q2, g; 45 | if (!solveLDE(e1.M, -e2.M, e2.R - e1.R, q1, q2, g)) { 46 | return false; 47 | } 48 | q2 %= e1.M / g; 49 | ll lcm = abs(e1.M / g * e2.M); 50 | //// ll x = ((__int128) e2.M * (__int128) q2 + (__int128) e2.R) % lcm; ?? 51 | ll x = e2.M * q2 + e2.R; 52 | x %= lcm; 53 | if (x < 0)x += lcm; 54 | res = {lcm, x}; 55 | return true; 56 | } 57 | 58 | bool CRT(const vector &eq, mod_eq &ret) { 59 | ret = eq[0]; 60 | for (int i = 1; i < eq.size(); ++i) { 61 | if (!CRT(eq[i], ret, ret)) { 62 | return false; 63 | } 64 | } 65 | return true; 66 | } 67 | -------------------------------------------------------------------------------- /templates/Number Theory/Chicken McNugget Theorem.txt: -------------------------------------------------------------------------------- 1 | The Chicken McNugget Theorem states that for any two relatively prime positive integers $m,n$, the greatest integer that cannot be written in the form $am + bn$ for non negative integers $a, b$ is $mn-m-n$. 2 | A consequence of the theorem is that there are exactly (m - 1) * (n - 1) / 2 positive integers which cannot be expressed in the form $am + bn$. 3 | -------------------------------------------------------------------------------- /templates/Number Theory/Discrete Root.cpp: -------------------------------------------------------------------------------- 1 | // returns any or all numbers x such that x ^ k = a (mod m) 2 | // existence: a = 0 is trivial, and if a > 0: a ^ (phi(m) / gcd(k, phi(m))) == 1 mod m 3 | // if solution exists, then number of solutions = gcd(k, phi(m)). 4 | // here m is prime, but it will work for any integer which has a primitive root 5 | int discrete_root(int k, int a, int m) { 6 | if (a == 0) return 1; 7 | int g = primitive_root(m); 8 | int phi = m - 1; // m is prime 9 | // run baby step-giant step 10 | int sq = (int) sqrt(m + .0) + 1; 11 | vector> dec(sq); 12 | for (int i = 1; i <= sq; ++i) dec[i - 1] = make_pair(power(g, 1LL * i * sq % phi * k % phi, m), i); 13 | sort(dec.begin(), dec.end()); 14 | int any_ans = -1; 15 | for (int i = 0; i < sq; ++i) { 16 | int my = power(g, 1LL * i * k % phi, m) * 1LL * a % m; 17 | auto it = lower_bound(dec.begin(), dec.end(), make_pair(my, 0)); 18 | if (it != dec.end() && it->first == my) { 19 | any_ans = it->second * sq - i; 20 | break; 21 | } 22 | } 23 | if (any_ans == -1) return -1; //no solution 24 | // for any answer 25 | int delta = (m - 1) / __gcd(k, m - 1); 26 | return power(g, any_ans % delta, m); 27 | // // for all possible answers 28 | // int delta = (m - 1) / __gcd(k, m - 1); 29 | // vector ans; 30 | // for (int cur = any_ans % delta; cur < m-1; cur += delta) ans.push_back(power(g, cur, m)); 31 | // sort(ans.begin(), ans.end()); 32 | // // assert(ans.size() == __gcd(k, m - 1)) 33 | // return ans; 34 | } 35 | -------------------------------------------------------------------------------- /templates/Number Theory/DiscreteLog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | using namespace __gnu_cxx; 5 | 6 | int phi(int n) { 7 | int res = n; 8 | for (int i = 2; i <= n / i; ++i) { 9 | if (n % i)continue; 10 | res -= res / i; 11 | while (!(n % i))n /= i; 12 | } 13 | if (n > 1) { 14 | res -= res / n; 15 | } 16 | return res; 17 | } 18 | 19 | struct mul { 20 | int mod; 21 | mul(int mod) : mod(mod) {} 22 | int operator()(int a, int b) const { return a * 1ll * b % mod; } 23 | }; 24 | 25 | int identity_element(const mul &) { return 1; } 26 | 27 | //// a ^ x = b (% mod) --- O( sqrt(mod) ) 28 | bool DiscreteLog(int a, int b, int mod, int &x) { 29 | a %= mod; //// b %= mod ??? 30 | mul M(mod); 31 | int alpha = 1, add = 0, g; 32 | while ((g = __gcd(a, mod)) > 1) { 33 | if (b == alpha) 34 | return x = add, true; 35 | if (b % g)return false; 36 | b /= g; 37 | mod /= g; 38 | M = mul(mod); 39 | ++add; 40 | alpha = M(alpha, a / g); 41 | } 42 | int ph = phi(mod); 43 | int inv = power(a, ph - 1, M); 44 | unordered_map mp; 45 | int m = ceil(sqrt(mod)); 46 | int k = 1; 47 | for (int i = 0, j = alpha; i < m; ++i) { 48 | mp.emplace(j, i); 49 | j = M(j, a); 50 | k = M(k, inv); 51 | } 52 | x = -1; 53 | for (int i = 0, j = b; i < m; ++i) { 54 | auto it = mp.find(j); 55 | if (it != mp.end()) { 56 | int cur = it->second + i * m + add; 57 | if (x == -1 || cur < x) 58 | x = cur; 59 | } 60 | j = M(j, k); 61 | } 62 | return x != -1; 63 | } 64 | -------------------------------------------------------------------------------- /templates/Number Theory/Fibonacci Numbers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/templates/Number Theory/Fibonacci Numbers.pdf -------------------------------------------------------------------------------- /templates/Number Theory/Floor Sum.cpp: -------------------------------------------------------------------------------- 1 | // \sum{k=1}^{n}{floor(n/k)} 2 | // count of numbers such that n/i = k -> n/k - n/(k+1) 3 | ll floor_sum(ll n) { 4 | ll sum = 0; 5 | for (ll i = 1, last; i <= n; i = last + 1) { 6 | last = n / (n / i); 7 | sum += (n / i) * (last - i + 1); 8 | // n / x yields the same value for i <= x <= last. 9 | } 10 | return sum; 11 | } 12 | -------------------------------------------------------------------------------- /templates/Number Theory/Gauss - doubles.cpp: -------------------------------------------------------------------------------- 1 | const double eps = 1e-9; 2 | 3 | int Gauss(vector> a, vector &ans) { 4 | int n = (int) a.size(), m = (int) a[0].size() - 1; 5 | vector pos(m, -1); 6 | double det = 1; 7 | int rank = 0; 8 | for (int col = 0, row = 0; col < m && row < n; ++col) { 9 | int mx = row; 10 | for (int i = row; i < n; i++) if (fabs(a[i][col]) > fabs(a[mx][col])) mx = i; 11 | if (fabs(a[mx][col]) < eps) { 12 | det = 0; 13 | continue; 14 | } 15 | for (int i = col; i <= m; i++) swap(a[row][i], a[mx][i]); 16 | if (row != mx) det = -det; 17 | det *= a[row][col]; 18 | pos[col] = row; 19 | for (int i = 0; i < n; i++) { 20 | if (i != row && fabs(a[i][col]) > eps) { 21 | double c = a[i][col] / a[row][col]; 22 | for (int j = col; j <= m; j++) a[i][j] -= a[row][j] * c; 23 | } 24 | } 25 | ++row, ++rank; 26 | } 27 | ans.assign(m, 0); 28 | for (int i = 0; i < m; i++) { 29 | if (pos[i] != -1) ans[i] = a[pos[i]][m] / a[pos[i]][i]; 30 | } 31 | for (int i = 0; i < n; i++) { 32 | double sum = 0; 33 | for (int j = 0; j < m; j++) sum += ans[j] * a[i][j]; 34 | if (fabs(sum - a[i][m]) > eps) return -1; //no solution 35 | } 36 | for (int i = 0; i < m; i++) if (pos[i] == -1) return 2; //infinte solutions 37 | return 1; //unique solution 38 | } 39 | -------------------------------------------------------------------------------- /templates/Number Theory/Gauss.cpp: -------------------------------------------------------------------------------- 1 | int Gauss(vector> a, vector &ans) { 2 | int n = a.size(), m = (int) a[0].size() - 1; 3 | vector pos(m, -1); 4 | int free_var = 0; 5 | const ll MODSQ = (ll) mod * mod; 6 | int det = 1, rank = 0; 7 | for (int col = 0, row = 0; col < m && row < n; col++) { 8 | int mx = row; 9 | for (int k = row; k < n; k++) if (a[k][col] > a[mx][col]) mx = k; 10 | if (a[mx][col] == 0) { 11 | det = 0; 12 | continue; 13 | } 14 | for (int j = col; j <= m; j++) swap(a[mx][j], a[row][j]); 15 | if (row != mx) det = det == 0 ? 0 : mod - det; 16 | det = 1LL * det * a[row][col] % mod; 17 | pos[col] = row; 18 | int inv = power(a[row][col], mod - 2); 19 | for (int i = 0; i < n && inv; i++) { 20 | if (i != row && a[i][col]) { 21 | int x = ((ll) a[i][col] * inv) % mod; 22 | for (int j = col; j <= m && x; j++) { 23 | if (a[row][j]) a[i][j] = (MODSQ + a[i][j] - ((ll) a[row][j] * x)) % mod; 24 | } 25 | } 26 | } 27 | row++; 28 | ++rank; 29 | } 30 | ans.assign(m, 0); 31 | for (int i = 0; i < m; i++) { 32 | if (pos[i] == -1) free_var++; 33 | else ans[i] = ((ll) a[pos[i]][m] * power(a[pos[i]][i], mod - 2)) % mod; 34 | } 35 | for (int i = 0; i < n; i++) { 36 | ll val = 0; 37 | for (int j = 0; j < m; j++) val = (val + ((ll) ans[j] * a[i][j])) % mod; 38 | if (val != a[i][m]) return -1; //no solution 39 | } 40 | return free_var; //has solution 41 | } 42 | -------------------------------------------------------------------------------- /templates/Number Theory/Linear Congruence Equation.cpp: -------------------------------------------------------------------------------- 1 | 2 | //from kactl, if a and b are co-prime, x is a inv % b 3 | ll euclid(ll a, ll b, ll &x, ll &y) { 4 | if (!b) return x = 1, y = 0, a; 5 | ll d = euclid(b, a % b, y, x); 6 | return y -= a/b * x, d; 7 | } 8 | 9 | //solves (a * x = b) % m 10 | //returns x in range [0, m-1] if sol exists or -1 if it doesn't 11 | //returns what to increment to get other answers 12 | void congruence_equation(ll a, ll b, ll m, ll& x, ll& inc){ 13 | ll g = __gcd(a, m); 14 | // no solution 15 | if(b % g){ 16 | x = -1; inc = m; 17 | return; 18 | } 19 | 20 | //not co-prime 21 | if(g == 1){ 22 | ll aInv, y; 23 | euclid(a, m, aInv, y); 24 | aInv = (aInv + m) % m; 25 | x = aInv * b % m; 26 | inc = m; 27 | return; 28 | } 29 | 30 | //not co-prime but sol exists 31 | congruence_equation(a/g, b/g, m/g, x, inc); 32 | } 33 | -------------------------------------------------------------------------------- /templates/Number Theory/Linear Diophantine Equation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/templates/Number Theory/Linear Diophantine Equation.pdf -------------------------------------------------------------------------------- /templates/Number Theory/Linear Sieve and Mobius.cpp: -------------------------------------------------------------------------------- 1 | const int N = 1e7 + 7; 2 | vector prime; 3 | bitset isComp; 4 | char mob[N]; 5 | int lpf[N], gpf[N]; 6 | 7 | void sieve(int n = N) { 8 | isComp = 0; 9 | mob[1] = 1; 10 | for (int i = 2; i < n; ++i) { 11 | if (!isComp[i]) { 12 | prime.push_back(i); 13 | mob[i] = -1; 14 | lpf[i] = gpf[i] = i; 15 | } 16 | for (int j = 0; j < prime.size() && i * prime[j] < n; ++j) { 17 | isComp[i * prime[j]] = true; 18 | lpf[prime[j] * i] = prime[j]; 19 | gpf[prime[j] * i] = gpf[i]; 20 | if (i % prime[j] == 0) { 21 | mob[i * prime[j]] = 0; 22 | break; 23 | } else 24 | mob[i * prime[j]] = mob[i] * mob[prime[j]]; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /templates/Number Theory/MillerRabin.cpp: -------------------------------------------------------------------------------- 1 | using ll = long long; 2 | namespace MillerRabin { 3 | mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); 4 | const int P = 1e6 + 9; 5 | int primes[P], spf[P]; 6 | inline ll mul_mod(ll x, ll y, ll m) { 7 | ll res = __int128(x) * y % m; 8 | return res; 9 | // ll res = x * y - (ll)((long double)x * y / m + 0.5) * m; 10 | // return res < 0 ? res + m : res; 11 | } 12 | inline ll pow_mod(ll x, ll n, ll m) { 13 | ll res = 1 % m; 14 | for (; n; n >>= 1) { 15 | if (n & 1) res = mul_mod(res, x, m); 16 | x = mul_mod(x, x, m); 17 | } 18 | return res; 19 | } 20 | // O(it * (logn)^3), it = number of rounds performed (but faster in practice) 21 | inline bool miller_rabin(ll n) { 22 | if (n <= 2 || (n & 1 ^ 1)) return (n == 2); 23 | if (n < P) return spf[n] == n; 24 | ll c, d, s = 0, r = n - 1; 25 | for (; !(r & 1); r >>= 1, s++) {} 26 | // each iteration is a round 27 | for (int i = 0; primes[i] < n && primes[i] < 32; i++) { 28 | c = pow_mod(primes[i], r, n); 29 | for (int j = 0; j < s; j++) { 30 | d = mul_mod(c, c, n); 31 | if (d == 1 && c != 1 && c != (n - 1)) return false; 32 | c = d; 33 | } 34 | if (c != 1) return false; 35 | } 36 | return true; 37 | } 38 | void init() { 39 | int cnt = 0; 40 | for (int i = 2; i < P; i++) { 41 | if (!spf[i]) primes[cnt++] = spf[i] = i; 42 | for (int j = 0, k; (k = i * primes[j]) < P; j++) { 43 | spf[k] = primes[j]; 44 | if (spf[i] == spf[k]) break; 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /templates/Number Theory/Phi.cpp: -------------------------------------------------------------------------------- 1 | int Phi(int n) { 2 | int ret = n; 3 | for (int i = 2; i * i <= n; i++) { 4 | if (n % i == 0) { 5 | while (n % i == 0) 6 | n /= i; 7 | ret -= ret / i; 8 | } 9 | } 10 | if (n > 1) 11 | ret -= ret / n; 12 | return ret; 13 | } 14 | void Phi_1_n() { 15 | iota(phi, phi + N, 0); 16 | for (int i = 2; i < N; ++i) { 17 | if (phi[i] == i) { 18 | for (int j = i; j < N; j += i) 19 | phi[j] -= phi[j] / i; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /templates/Number Theory/Power Tower.cpp: -------------------------------------------------------------------------------- 1 | map mp; 2 | 3 | ll phi(ll n) { 4 | if (mp.count(n)) return mp[n]; 5 | ll ans = n, m = n; 6 | for (ll i = 2; i * i <= m; i++) { 7 | if (m % i == 0) { 8 | while (m % i == 0) m /= i; 9 | ans = ans / i * (i - 1); 10 | } 11 | } 12 | if (m > 1) ans = ans / m * (m - 1); 13 | return mp[n] = ans; 14 | } 15 | 16 | inline ll MOD(ll x, ll m) { 17 | if (x < m) return x; 18 | return x % m + m; 19 | } 20 | 21 | ll power(ll n, ll k, ll mod) { 22 | ll ans = MOD(1, mod); 23 | while (k) { 24 | if (k & 1) ans = MOD(ans * n, mod); 25 | n = MOD(n * n, mod); 26 | k >>= 1; 27 | } 28 | return ans; 29 | } 30 | 31 | // if x >= log2(m), then a^x = a^(MOD(x, phi(m))) % m 32 | ll powerTower(const vector &a, ll l, ll r, ll m) { 33 | if (l == r) return MOD(a[l], m); 34 | if (m == 1) return 1; 35 | return power(a[l], powerTower(a, l + 1, r, phi(m)), m); 36 | } 37 | -------------------------------------------------------------------------------- /templates/Number Theory/Prime power in N!.cpp: -------------------------------------------------------------------------------- 1 | ll count_p_in_nfact(ll p, ll n) { 2 | ll res = 0, q = p; 3 | while (q <= n) { 4 | res += n / q, q *= p; 5 | } 6 | return res; 7 | } 8 | -------------------------------------------------------------------------------- /templates/Number Theory/Primitive root.cpp: -------------------------------------------------------------------------------- 1 | int totient(int n) { 2 | int ans = n; 3 | for (int i = 2; i * i <= n; i++) { 4 | if (n % i == 0) { 5 | while (n % i == 0) n /= i; 6 | ans = ans / i * (i - 1); 7 | } 8 | } 9 | if (n > 1) ans = ans / n * (n - 1); 10 | return ans; 11 | } 12 | int power(int a, int b, int m) { 13 | int res = 1; 14 | while (b > 0) { 15 | if (b & 1) res = 1LL * res * a % m; 16 | a = 1LL * a * a % m; 17 | b >>= 1; 18 | } 19 | return res; 20 | } 21 | // g is a primitive root modulo p if and only if for any integer a such that 22 | // gcd(a, p) = 1, there exists an integer k such that: g^k = a(mod p). 23 | // primitive root modulo n exists iff n = 1, 2, 4 or n = p^k or 2 * p^k for some odd prime p 24 | int primitive_root(int p) { 25 | // first check if primitive root exists or not. I have omitted this part here 26 | vector fact; 27 | int phi = totient(p), n = phi; 28 | for (int i = 2; i * i <= n; ++i) { 29 | if (n % i == 0) { 30 | fact.push_back(i); 31 | while (n % i == 0) n /= i; 32 | } 33 | } 34 | if (n > 1) fact.push_back(n); 35 | for (int res = 2; res <= p; ++res) { // this loop will run at most (logp ^ 6) times i.e. until a root is found 36 | bool ok = true; 37 | // check if this is a primitive root modulo p 38 | for (size_t i = 0; i < fact.size() && ok; ++i) 39 | ok &= power(res, phi / fact[i], p) != 1; 40 | if (ok) return res; 41 | } 42 | return -1; 43 | } 44 | -------------------------------------------------------------------------------- /templates/Number Theory/Segmented Sieve.cpp: -------------------------------------------------------------------------------- 1 | #define ll long long 2 | 3 | vector segmentedSieve(ll L, ll R) { 4 | // generate all primes up to sqrt(R) 5 | ll lim = sqrt(R); 6 | vector mark(lim + 1, false); 7 | vector primes; 8 | for (ll i = 2; i <= lim; ++i) { 9 | if (!mark[i]) { 10 | primes.emplace_back(i); 11 | for (ll j = i * i; j <= lim; j += i) 12 | mark[j] = true; 13 | } 14 | } 15 | 16 | vector isPrime(R - L + 1, true); 17 | for (ll i: primes) 18 | for (ll j = max(i * i, (L + i - 1) / i * i); j <= R; j += i) 19 | isPrime[j - L] = false; 20 | if (L == 1) 21 | isPrime[0] = false; 22 | return isPrime; 23 | } 24 | -------------------------------------------------------------------------------- /templates/Number Theory/cheat.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdelaleem024/competitive-programming/0235f53c886e91aa1c0504c2f9fd0a2e21c14272/templates/Number Theory/cheat.pdf -------------------------------------------------------------------------------- /templates/Strings/Aho-Corasick.cpp: -------------------------------------------------------------------------------- 1 | struct Node { 2 | unordered_map ch; 3 | int fail{}, nxt{}, id = -1; 4 | }; 5 | 6 | struct Aho { 7 | 8 | static const int ALPHA = 128; 9 | vector Trie; 10 | 11 | int addNode() { 12 | Trie.emplace_back(); 13 | return Trie.size() - 1; 14 | } 15 | 16 | void init() { 17 | Trie.clear(); 18 | addNode(); 19 | } 20 | 21 | Aho() { init(); } 22 | 23 | /// If the same string appears before, return its index 24 | int insert(const string &pat, int ID) { 25 | int u = 0; 26 | for (char c: pat) { 27 | int &nd = Trie[u].ch[c]; 28 | if (!nd) nd = addNode(); 29 | u = nd; 30 | } 31 | int &id = Trie[u].id; 32 | return ~id ? id : (id = ID); 33 | } 34 | 35 | int nxtF(int u, char c) { 36 | while (!Trie[u].ch.count(c)) 37 | u = Trie[u].fail; 38 | u = Trie[u].ch[c]; 39 | return u; 40 | } 41 | 42 | int Nxt(int u) { 43 | if (!u) return u; 44 | int &v = Trie[u].nxt; 45 | return ~Trie[v].id ? v : v = Nxt(v); 46 | } 47 | 48 | void computeFail() { 49 | queue q; 50 | for (int i = 0; i < ALPHA; i++) { 51 | int &nd = Trie[0].ch[i]; 52 | if (nd) 53 | q.push(nd); 54 | } 55 | while (q.size()) { 56 | int u = q.front(); 57 | q.pop(); 58 | int f = Trie[u].fail; 59 | for (auto [ch, v]: Trie[u].ch) { 60 | Trie[v].fail = Trie[v].nxt = nxtF(f, ch); 61 | q.push(v); 62 | } 63 | } 64 | } 65 | 66 | vector> match(string &s, int numPat) { 67 | vector> ret(numPat); 68 | int cur = 0, i = 0; 69 | for (char c: s) { 70 | cur = nxtF(cur, c); 71 | for (int p = cur; p; p = Nxt(p)) { 72 | int &x = Trie[p].id; 73 | if (~x) ret[x].push_back(i); 74 | } 75 | ++i; 76 | } 77 | return ret; 78 | } 79 | 80 | }; -------------------------------------------------------------------------------- /templates/Strings/KMP.cpp: -------------------------------------------------------------------------------- 1 | vector kmp(const string &s) { 2 | const int n = (int) s.length(); 3 | vector fail(n); 4 | int j = 0; 5 | for (int i = 1; i < n; i++) { 6 | while (j > 0 && s[i] != s[j]) j = fail[j - 1]; 7 | j += (s[j] == s[i]); 8 | fail[i] = j; 9 | } 10 | return fail; 11 | } 12 | 13 | void compute_automaton(string s, vector> &aut) { 14 | s += '#'; 15 | int n = s.size(); 16 | vector pi = kmp(s); 17 | aut.assign(n, vector(26)); 18 | for (int i = 0; i < n; i++) { 19 | for (int c = 0; c < 26; c++) { 20 | if (i > 0 && 'a' + c != s[i]) { 21 | aut[i][c] = aut[pi[i - 1]][c]; 22 | } else { 23 | aut[i][c] = i + ('a' + c == s[i]); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /templates/Strings/Palindromic Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | -> cnt contains the number of palindromic suffixes of the node 3 | */ 4 | struct PalindromicTree { 5 | struct node { 6 | int nxt[26], len, st, en, link, cnt, oc; 7 | }; 8 | string s; 9 | vector t; 10 | int sz, last; 11 | PalindromicTree() {} 12 | PalindromicTree(string _s) { 13 | s = _s; 14 | int n = s.size(); 15 | t.clear(); 16 | t.resize(n + 9); 17 | sz = 2, last = 2; 18 | t[1].len = -1, t[1].link = 1; 19 | t[2].len = 0, t[2].link = 1; 20 | } 21 | int extend(int pos) { // returns 1 if it creates a new palindrome 22 | int cur = last, curlen = 0; 23 | int ch = s[pos] - 'a'; 24 | while (1) { 25 | curlen = t[cur].len; 26 | if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) break; 27 | cur = t[cur].link; 28 | } 29 | if (t[cur].nxt[ch]) { 30 | last = t[cur].nxt[ch]; 31 | t[last].oc++; 32 | return 0; 33 | } 34 | sz++; 35 | last = sz; 36 | t[sz].oc = 1; 37 | t[sz].len = t[cur].len + 2; 38 | t[cur].nxt[ch] = sz; 39 | t[sz].en = pos; 40 | t[sz].st = pos - t[sz].len + 1; 41 | if (t[sz].len == 1) { 42 | t[sz].link = 2; 43 | t[sz].cnt = 1; 44 | return 1; 45 | } 46 | while (1) { 47 | cur = t[cur].link; 48 | curlen = t[cur].len; 49 | if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) { 50 | t[sz].link = t[cur].nxt[ch]; 51 | break; 52 | } 53 | } 54 | t[sz].cnt = 1 + t[t[sz].link].cnt; 55 | return 1; 56 | } 57 | void calc_occurrences() { 58 | for (int i = sz; i >= 3; i--) t[t[i].link].oc += t[i].oc; 59 | } 60 | } t; 61 | -------------------------------------------------------------------------------- /templates/Strings/Trie.cpp: -------------------------------------------------------------------------------- 1 | struct Node { 2 | unordered_map nxt; 3 | int isEnd = 0, sz = 0;//subTree size 4 | int &operator[](char x) { 5 | return nxt[x]; 6 | } 7 | }; 8 | 9 | struct Trie { 10 | vector tr; 11 | 12 | int newNode() { 13 | tr.emplace_back(); 14 | return tr.size() - 1; 15 | } 16 | 17 | Trie() { tr.clear(), newNode(); } 18 | 19 | void insert(const string &s) { 20 | int u = 0; 21 | for (char c: s) { 22 | if (!tr[u][c]) 23 | tr[u][c] = newNode(); 24 | tr[u].sz++; 25 | u = tr[u][c]; 26 | } 27 | tr[u].sz++; 28 | tr[u].isEnd++; 29 | } 30 | 31 | }; 32 | -------------------------------------------------------------------------------- /templates/Strings/Z.cpp: -------------------------------------------------------------------------------- 1 | /* the max number of characters starting from the position   2 | i that coincide with the first characters of s  */ 3 | vector z_function(string s) { 4 | int n = s.size(); 5 | vector z(n); 6 | int l = 0, r = 0; 7 | for(int i = 1; i < n; i++) { 8 | if(i < r) { 9 | z[i] = min(r - i, z[i - l]); 10 | } 11 | while(i + z[i] < n && s[z[i]] == s[i + z[i]]) { 12 | z[i]++; 13 | } 14 | if(i + z[i] > r) { 15 | l = i; 16 | r = i + z[i]; 17 | } 18 | } 19 | return z; 20 | } 21 | 22 | vector search(string& S, string &pat) { 23 | int N = S.size(), M = pat.size(); 24 | string combined = pat + S; 25 | vector Z = z_function(combined); 26 | vector matches; 27 | for (int i = 0; i < N; i++) { 28 | if (Z[M + i] >= M) { 29 | matches.push_back(i); 30 | } 31 | } 32 | return matches; 33 | } -------------------------------------------------------------------------------- /templates/Trees/HLD.cpp: -------------------------------------------------------------------------------- 1 | struct HLD { 2 | private: 3 | void dfs0(int u, vector> &adj) { 4 | sz[u] = 1; 5 | for (int &v: adj[u]) { 6 | if (v == p[u])continue; 7 | dep[v] = dep[u] + 1; 8 | p[v] = u; 9 | dfs0(v, adj); 10 | sz[u] += sz[v]; 11 | if (adj[u][0] == p[u] or sz[adj[u][0]] < sz[v]) 12 | swap(v, adj[u][0]); 13 | } 14 | } 15 | 16 | void dfs1(int u, int top, int &timer, const vector> &adj) { 17 | in[u] = timer++, tp[u] = top; 18 | for (int v: adj[u]) { 19 | if (v == p[u])continue; 20 | if (v == adj[u][0]) dfs1(v, top, timer, adj); 21 | else dfs1(v, v, timer, adj); 22 | } 23 | } 24 | 25 | public: 26 | vector sz, p, dep, in, tp; 27 | 28 | HLD(vector> adj, int root = 0) { 29 | int n = adj.size(); 30 | sz.assign(n, {}); 31 | p.assign(n, {}); 32 | dep.assign(n, {}); 33 | in.assign(n, {}); 34 | tp.assign(n, {}); 35 | int t = 0; 36 | dfs0(root, adj); 37 | dfs1(root, root, t, adj); 38 | } 39 | 40 | int LCA(int u, int v) { 41 | while (tp[u] != tp[v]) { 42 | if (dep[tp[u]] < dep[tp[v]])swap(u, v); 43 | u = p[tp[u]]; 44 | } 45 | if (dep[u] < dep[v])return u; 46 | return v; 47 | } 48 | 49 | int dis(int u, int v) { 50 | return dep[u] + dep[v] - 2 * dep[LCA(u, v)]; 51 | } 52 | 53 | int pos(int node) { return in[node]; } 54 | 55 | pair sub_tree(int u) { return {in[u], in[u] + sz[u] - 1}; } 56 | 57 | vector> query_path(int x, int y) { 58 | vector> res; 59 | while (tp[x] != tp[y]) { 60 | if (dep[tp[x]] < dep[tp[y]]) swap(x, y); 61 | res.emplace_back(in[tp[x]], in[x]); 62 | x = p[tp[x]]; 63 | } 64 | if (dep[x] > dep[y]) swap(x, y); 65 | res.emplace_back(in[x], in[y]); 66 | return res; 67 | } 68 | 69 | }; 70 | -------------------------------------------------------------------------------- /templates/Trees/LCA Euler tour.cpp: -------------------------------------------------------------------------------- 1 | vector> adj; 2 | vector in, lvl; 3 | vector> euler; 4 | 5 | void dfs0(int u, int par) { 6 | in[u] = euler[0].size(); 7 | euler[0].emplace_back(u); 8 | for (auto &v: adj[u]) { 9 | if (par == v)continue; 10 | lvl[v] = lvl[u] + 1; 11 | dfs0(v, u); 12 | euler[0].emplace_back(u); 13 | } 14 | } 15 | 16 | int mrg(int u, int v) { 17 | return lvl[u] < lvl[v] ? u : v; 18 | } 19 | 20 | void precompute_lca(int root = 0) { 21 | euler = {{}}; 22 | in.assign(adj.size(), {}); 23 | lvl.assign(adj.size(), {}); 24 | dfs0(root, 0); 25 | const int sz = euler[0].size(); 26 | for (int msk = 1; (1 << msk) <= sz; ++msk) { 27 | euler.emplace_back(sz - (1 << msk) + 1); 28 | for (int i = 0; i + (1 << msk) <= sz; ++i) { 29 | euler[msk][i] = mrg( 30 | euler[msk - 1][i], 31 | euler[msk - 1][i + (1 << (msk - 1))] 32 | ); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /templates/Trees/LCA.cpp: -------------------------------------------------------------------------------- 1 | vector>> adj; /// [v , w] 2 | vector lvl; 3 | vector> dp, up; 4 | int LOG, initial; 5 | 6 | int mrg(int u, int v) { return max(u, v); }/* TODO */ 7 | 8 | void dfs0(int u, int p = 0) { 9 | for (int i = 1; i < LOG; i++) { 10 | up[i][u] = up[i - 1][up[i - 1][u]]; 11 | dp[i][u] = mrg(dp[i - 1][u], dp[i - 1][up[i - 1][u]]); 12 | } 13 | for (auto [v, w]: adj[u]) { 14 | if (v == p)continue; 15 | up[0][v] = u; 16 | dp[0][v] = w; 17 | lvl[v] = lvl[u] + 1; 18 | dfs0(v, u); 19 | } 20 | } 21 | 22 | int LCA(int u, int v) { 23 | if (lvl[u] < lvl[v]) { 24 | swap(u, v); 25 | } 26 | int diff = lvl[u] - lvl[v]; 27 | for (int i = 0; diff; diff >>= 1, ++i) { 28 | if (diff & 1) { 29 | u = up[i][u]; 30 | } 31 | } 32 | if (u == v)return u; 33 | for (int i = LOG - 1; i >= 0; i--) { 34 | if (up[i][u] != up[i][v]) { 35 | u = up[i][u], v = up[i][v]; 36 | } 37 | } 38 | return up[0][u]; 39 | } 40 | 41 | int query(int u, int v) { 42 | if (lvl[u] < lvl[v]) { 43 | swap(u, v); 44 | } 45 | int res = initial; 46 | int diff = lvl[u] - lvl[v]; 47 | for (int i = 0; diff; ++i, diff >>= 1) { 48 | if (diff & 1) { 49 | res = mrg(res, dp[i][u]); 50 | u = up[i][u]; 51 | } 52 | } 53 | if (u == v)return res; 54 | for (int i = LOG - 1; i >= 0; i--) { 55 | if (up[i][u] != up[i][v]) { 56 | res = mrg(res, mrg(dp[i][u], dp[i][v])); 57 | u = up[i][u], v = up[i][v]; 58 | } 59 | } 60 | res = mrg(res, mrg(dp[0][u], dp[0][v])); 61 | return res; 62 | } 63 | 64 | void compute(int root = 1) { 65 | LOG = __lg(adj.size()) + 1; 66 | lvl.assign(adj.size(), {}); 67 | dp.assign(LOG, vector(adj.size())); 68 | up.assign(LOG, vector(adj.size())); 69 | dfs0(root); 70 | } 71 | -------------------------------------------------------------------------------- /templates/Trees/Small to large.cpp: -------------------------------------------------------------------------------- 1 | const int N = 3e5 + 5; 2 | vector> adj; 3 | int in[N], sz[N], tree[N]; 4 | int t; /// reset this to 0 each testcase 5 | 6 | void dfs0(int u, int p = -1) { 7 | tree[t] = u, in[u] = t++; 8 | sz[u] = 1; 9 | for (int v: adj[u]) { 10 | if (v == p) continue; 11 | dfs0(v, u); 12 | sz[u] += sz[v]; 13 | } 14 | } 15 | 16 | void add(int node) {} 17 | void rem(int node) {} 18 | 19 | void dfs1(int u, int p, bool keep) { 20 | int bg = -1, mx = -1; 21 | for (int &v: adj[u]) { 22 | if (v != p && sz[v] > mx) 23 | mx = v, bg = v; 24 | } 25 | for (int &v: adj[u]) { 26 | if (v == p or v == bg)continue; 27 | dfs1(v, u, false); 28 | } 29 | if (~bg) { 30 | dfs1(bg, u, true); 31 | } 32 | add(u); 33 | for (int v: adj[u]) { 34 | if (v == p or v == bg)continue; 35 | for (int i = 0; i < sz[v]; ++i) { 36 | int x = tree[in[v] + i]; 37 | add(x); 38 | } 39 | } 40 | 41 | //// get the answers here 42 | 43 | if (!keep) { 44 | for (int i = 0; i < sz[u]; ++i) { 45 | int x = tree[in[u] + i]; 46 | rem(x); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /templates/Trees/Tree Hashing.cpp: -------------------------------------------------------------------------------- 1 | #define ull unsigned long long 2 | #define all(X) X.begin(),X.end() 3 | 4 | ull pw(ull b, ull p) { 5 | if (!p) return 1; 6 | ull ret = pw(b, p >> 1); 7 | ret *= ret; 8 | if (p & 1) 9 | ret = ret * b; 10 | return ret; 11 | } 12 | 13 | ull dfs(int u, int par) { 14 | vector child; 15 | for (auto v: adj[u]) { 16 | if (v == par)continue; 17 | child.push_back(dfs(v, u)); 18 | } 19 | sort(all(child)); 20 | ull ret = 0; 21 | for (int i = 0; i < child.size(); ++i) { 22 | ret += child[i] * child[i] + child[i] * pw(31, i + 1) + (ull) 42; 23 | } 24 | return ret; 25 | } 26 | -------------------------------------------------------------------------------- /templates/Utils/Balanced_parentheses_count.cpp: -------------------------------------------------------------------------------- 1 | /// n size of the string , k is the number of different type 2 | int Balanced_parentheses_count(int n, int k, int mod) { 3 | if (n & 1)return 0; 4 | n /= 2; 5 | long long a = 1, b = 1, c = 1; 6 | for (int i = 1; i <= 2 * n; ++i) { 7 | a = a * i % mod; 8 | if (i == n)b = a; 9 | if (i == n + 1)c = a; 10 | } 11 | int res = a * power(b, mod - 2, mod) % mod * power(c, mod - 2, mod) % mod; 12 | return res * power(k, n, mod) % mod; 13 | } -------------------------------------------------------------------------------- /templates/Utils/Tringle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Heron's formula is used to find the area of a triangle that has three different sides. 3 | Heron's formula is written as, Area = √[s(s-a)(s-b)(s-c)], 4 | where a, b and c are the sides of the triangle, and 's' is the semi-perimeter of the triangle. 5 | */ 6 | 7 | /* 8 | Area of triangle with using coordinates is: A = (1/2) |x1(y2 − y3) + x2(y3 − y1) + x3(y1 − y2)|, 9 | where (x1,y1),(x2,y2), and (x3,y3) are the coordinates of vertices of triangle. 10 | */ -------------------------------------------------------------------------------- /templates/Utils/fraction Class.cpp: -------------------------------------------------------------------------------- 1 | #define ABS(x) ((x)>=0?(x):-(x)) 2 | 3 | struct frac { 4 | long long n, d; 5 | 6 | frac(const long long &N, const long long &D = 1) : 7 | n(N), d(D) { 8 | long long g = gcd(ABS(n), ABS(d)); 9 | if (!g) { 10 | this->n = this->d = 0; 11 | return; 12 | } 13 | n /= g, d /= g; 14 | if (n == 0) d = 1; 15 | if (d < 0) n *= -1, d *= -1; 16 | if (d == 0) n = 1; 17 | } 18 | 19 | bool operator<(const frac &f) const { 20 | return n * f.d < d * f.n; 21 | } 22 | 23 | frac operator*(const frac &f) const { 24 | return frac(n * f.n, d * f.d); 25 | } 26 | 27 | frac operator/(const frac &f) const { 28 | return frac(n * f.d, d * f.n); 29 | } 30 | 31 | frac operator-(const frac &f) const { 32 | return frac(n * f.d - d * f.n, d * f.d); 33 | } 34 | 35 | frac operator+(const frac &f) const { 36 | return frac(n * f.d + d * f.n, d * f.d); 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /templates/Utils/kthRoot.cpp: -------------------------------------------------------------------------------- 1 | ll kthRoot(ll n, ll k) // return integer kth root for n 2 | { // Also can be done by binary search for accurate results 3 | double root = powl((double) n, 1.0 / (double) k); 4 | // will have percision errors 5 | ll realRoot = (ll) (root - 1); 6 | while (1) { 7 | ll a = realRoot + 1, p = 1; 8 | // compute a^k 9 | for (int j = 0; j < k; j++) { 10 | if (p > n / a) // we exceed n, this also detect overflow 11 | return realRoot; 12 | p *= a; 13 | } 14 | ++realRoot; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /templates/Utils/stress.cpp: -------------------------------------------------------------------------------- 1 | #include "bits/stdc++.h" 2 | 3 | using namespace std; 4 | #define ll long long 5 | #define int long long 6 | 7 | random_device rd; 8 | mt19937_64 mt(rd()); 9 | 10 | ll rnd(ll l, ll r) { return uniform_int_distribution(l, r)(mt); } 11 | 12 | const int LIMIT = 1e6; 13 | 14 | void generate() { 15 | ofstream cout("test.txt"); 16 | 17 | int n = rnd(1, 10); 18 | cout << n << '\n'; 19 | for (int i = 0; i < n; ++i) { 20 | cout << rnd(1, 1e5) << ' '; 21 | } 22 | 23 | cout.close(); 24 | } 25 | 26 | int32_t main() { 27 | system("g++ -lm -O3 -std=c++17 -DLOCAL -pipe -o main.exe ../main.cpp"); 28 | system("g++ -lm -O3 -std=c++17 -pipe -o brute.exe ../brute.cpp"); 29 | // system("g++ -lm -O3 -std=c++17 -pipe -o gen.exe ../gen.cpp"); // run gen file ? 30 | 31 | for (int tc = 1; tc <= LIMIT; ++tc) { 32 | cerr << "Case " << tc << '\n'; 33 | 34 | // system("gen.exe >test.txt"); /// file 35 | // generate(); /// function 36 | 37 | system("main.exe wa.txt"); 38 | if (system("brute.exe ac.txt"))break; 39 | ifstream acs("ac.txt"); 40 | ifstream was("wa.txt"); 41 | string ac, wa; 42 | getline(was, wa, (char) EOF); 43 | getline(acs, ac, (char) EOF); 44 | was.close(); 45 | acs.close(); 46 | if (ac != wa) break; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /templates/Utils/unorderd_map O(1).cpp: -------------------------------------------------------------------------------- 1 | struct custom_hash { 2 | static uint64_t splitmix64(uint64_t x) { 3 | // http://xorshift.di.unimi.it/splitmix64.c 4 | x += 0x9e3779b97f4a7c15; 5 | x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; 6 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 7 | return x ^ (x >> 31); 8 | } 9 | 10 | size_t operator()(uint64_t x) const { 11 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 12 | return splitmix64(x + FIXED_RANDOM); 13 | } 14 | }; 15 | 16 | struct pair_hash { 17 | size_t operator()(const pair &x) const { 18 | return hash()(((long long) x.first) ^ (((long long) x.second) << 32)); 19 | } 20 | }; 21 | --------------------------------------------------------------------------------