├── Backtracking ├── Concepts │ ├── Generate all permutations.cpp │ └── TopSort to generate all orderings & detect cycle.cpp ├── Problems │ └── CSES creating strings.cpp ├── README.md └── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg ├── Dynamic programming ├── CSES Coin Combinations I.cpp ├── CSES Coin Combinations II.cpp ├── CSES Dice Combinations.cpp ├── CSES Grid Paths.cpp ├── CSES Removing Digits.cpp ├── Frog 1 AtCoder - dp_a .cpp ├── Frog 2 AtCoder - dp_b.cpp ├── README.md ├── Vacation AtCoder - dp_c.cpp └── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg ├── Graph theory ├── Concepts │ ├── BFS traversal[Level of a tree].cpp │ ├── BFS_.cpp │ ├── Bipartite Graph checking.cpp │ ├── Cycle detection of undirected graph and printing the cycle.cpp │ ├── Cycle detection on DAG[using BFS].cpp │ ├── DFS.cpp │ ├── Finding all paths(starting from 1) in an undirected graph.cpp │ ├── TopSort to generate all orderings & detect cycle.cpp │ ├── TopSort.cpp │ ├── TopSort[BFS (kahn's algo)].cpp │ ├── dijkstra [ using set].cpp │ └── dijkstra.cpp ├── Problems │ ├── BFS & DFS traversal │ │ ├── Aizu - ALDS1_11_C.cpp │ │ ├── Aizu ALDS1_11_A.cpp │ │ └── Aizu ALDS1_11_B.cpp │ ├── Bipartite graph checking │ │ ├── CodeForces 862B_Mahmoud and Ehab and the bipartiteness.cpp │ │ ├── SPOJ A Bug's life.cpp │ │ └── UVA_10004_Bicoloring.cpp │ ├── CSES Building Roads_1666[connected components].cpp │ ├── CSES Building Teams_1668.cpp │ ├── CSES Shortest Routes I.cpp │ ├── CSES message route_1667.cpp │ ├── CSES round_trip_1669.cpp │ ├── CodeForces_24A_Ring road.cpp │ ├── Connected components │ │ └── UVa 459 - Graph Connectivity.cpp │ ├── Grid problems │ │ ├── CSES Labyrinth.cpp │ │ ├── CSES counting rooms.cpp │ │ ├── CSES counting rooms[BFS].cpp │ │ ├── LightOJ 1012_Guilty Prince.cpp │ │ ├── UVA 572 Oil Deposits.cpp │ │ ├── UVa 11094.cpp │ │ ├── UVa 11953.cpp │ │ └── UVa 871.cpp │ ├── LightOJ1094_Farthest Nodes in a Tree.cpp │ ├── Topological sort │ │ ├── CSES Course Schedule.cpp │ │ ├── UVA10305 OrderingTasks[BFS used].cpp │ │ ├── UVa 10305 order Tasks[DFS used].cpp │ │ ├── UVa 11504 dominos.cpp │ │ └── UVa 872 ordering.cpp │ └── atcoder 54C One-stroke Path.cpp ├── README.md └── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg ├── Mathematics ├── Basic maths concepts │ ├── Multiplication of two large numbers.cpp │ └── Quotient and remainder of a very large number.cpp ├── Problems │ ├── CodeForces 1370A Maximum GCD.cpp │ ├── CodeForces 987C_Three displays.cpp │ ├── Di-visible Confusion.cpp │ ├── Divisors 2 SPOJ - DIV2.cpp │ ├── Fadi and LCM CodeForces - 1285C.cpp │ ├── HackerRank constructing-a-number_Constructing a Number.cpp │ ├── IP Checking LightOJ - 1354.cpp │ ├── Positive Negative Sign LightOJ - 1294.cpp │ ├── Power Sequence CodeForces - 1397B.cpp │ └── Swords CodeForces - 1216D.cpp ├── README.md └── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg ├── Number Theory ├── Basic Number theory concepts │ ├── BigMod.cpp │ ├── Number of Divisors of N using prime factorization.cpp │ └── SPF.cpp ├── Problems │ ├── A Missing Factor Gym - 310911K.cpp │ ├── Again Prime No Time. UVA - 10780.cpp │ ├── CF 1051B Relatively Prime Pairs.cpp │ ├── CF Gym - 310911G.cpp │ ├── CSES common divisors.cpp │ ├── Chef and Prime Divisors CodeChef - CHAPD.cpp │ ├── Codeforces Gym - 310911H .cpp │ ├── Common Divisors CodeForces - 1203C.cpp │ ├── Count the factors UVA - 10699 .cpp │ ├── Divisor Summation SPOJ - DIVSUM.cpp │ ├── Goldbach's Conjecture LightOJ - 1259 .cpp │ ├── Iftar Party LightOJ - 1014.cpp │ ├── Intelligent Factorial Factorization LightOJ - 1035.cpp │ ├── SPOJ - TDPRIMES.cpp │ ├── Trailing Zeroes (I) LightOJ - 1028.cpp │ ├── Twin Primes UVA - 10394.cpp │ ├── UVa 374.cpp │ ├── UVa12856 - Counting substhreengs.cpp │ ├── Uva 136 Ugly numbers.cpp │ └── spf_lpf_divisors.cpp ├── README.md └── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg ├── Prefix sum ├── CSES Static Range Sum Queries.cpp ├── README.md └── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg ├── Probabilities ├── CF 312B.cpp ├── README.md └── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg ├── README.md ├── Segment tree ├── 1.Seg Tree (build update query) │ ├── segmentTree Lazy propagation.cpp │ └── segmentTree.cpp ├── 2.CSES Problems │ ├── CSES Range Xor Queries.cpp │ ├── CSES Static Range Minimum Queries.cpp │ ├── CSES_Dynamic Range Minimum Queries.cpp │ └── CSES_Dynamic Range Sum Queries.cpp ├── README.md └── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg ├── Sorting and Searching ├── CSES Collecting Numbers.cpp ├── CSES Maximum Subarray Sum.cpp ├── CSES Movie Festival.cpp ├── CSES Restaurant Customers.cpp ├── CSES Stick Lengths.cpp ├── CSES Sum of Two Values.cpp ├── CSES Towers.cpp ├── CSES apartments.cpp ├── CSES concert tickets.cpp ├── CSES subarray sums I [using set].cpp ├── CSES subarray sums I.cpp ├── Distinct Numbers.cpp ├── README.md ├── assets │ ├── banner gif.gif │ ├── codejam.png │ ├── cp banner.jpeg │ ├── cses banner.jpeg │ ├── cses.PNG │ ├── cses2.jpg │ ├── icpc.jpeg │ ├── icpc.jpg │ ├── icpc.png │ ├── images.png │ ├── mix.png │ ├── mix2.jpg │ └── my logo.jpeg └── concept │ └── sum of two numbers within a range.cpp ├── assets ├── banner gif.gif ├── codejam.png ├── cp banner.jpeg ├── cses banner.jpeg ├── cses.PNG ├── cses2.jpg ├── icpc.jpeg ├── icpc.jpg ├── icpc.png ├── images.png ├── mix.png ├── mix2.jpg ├── my logo.jpeg └── purple work.gif ├── contribution.md └── string algorithms ├── Basic string implementation ├── CF 1104B Game with string.cpp ├── CF 665C simple strings.cpp ├── Chef and Pick digit.cpp ├── Kattis Palindrome substring.cpp ├── remove common char & concate.cpp └── remove duplicates.cpp └── README.md /Backtracking/Concepts/Generate all permutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define int long long int 8 | #define all(x) x.begin(),x.end() 9 | 10 | const int N = 10; 11 | int n; 12 | set ans,final; 13 | string res; 14 | bool taken[N] = {}; 15 | 16 | void recur(){ 17 | if(res.size() == n){ 18 | ans.insert(res); return; 19 | } 20 | 21 | rep(i,0,n){ 22 | if(!taken[i]){ 23 | res.push_back(i); 24 | taken[i] = 1; 25 | 26 | recur(); 27 | 28 | taken[i] = 0; 29 | res.pop_back(); 30 | } 31 | } 32 | } 33 | 34 | void solve() { 35 | string s; cin >> s; 36 | n = s.size(); 37 | 38 | recur(); 39 | 40 | for(auto x:ans){ 41 | string curr; 42 | for(auto y:x) curr += s[y]; 43 | 44 | final.insert(curr); 45 | } 46 | //ans has all the permutations and final has all the distincts 47 | 48 | cout << final.size() << '\n'; 49 | 50 | for(auto x:final){ 51 | cout << x <<'\n'; 52 | } 53 | 54 | } 55 | 56 | /*void solve() { // This solution also gives AC 57 | 58 | string s; cin >> s; sort(s.begin(),s.end()); 59 | std::vector ans; 60 | 61 | do{ 62 | ans.push_back(s); 63 | }while(next_permutation(s.begin(),s.end())); 64 | 65 | cout << ans.size() << '\n'; 66 | 67 | for(auto x:ans) cout << x << '\n'; 68 | 69 | }*/ 70 | 71 | int32_t main() 72 | { 73 | std::ios::sync_with_stdio(false);cin.tie(NULL); 74 | solve(); 75 | 76 | return 0; 77 | } 78 | 79 | -------------------------------------------------------------------------------- /Backtracking/Concepts/TopSort to generate all orderings & detect cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 100; 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | 7 | 8 | vector adj[MAXN]; 9 | int inDegree[MAXN]; 10 | bool visited[MAXN]; 11 | vector order; 12 | 13 | vector> answer; 14 | set inputs; 15 | int n; 16 | string s1,s2; 17 | 18 | 19 | void CLEAR(){ 20 | for(auto x:adj) x.clear(); 21 | for(auto x:inDegree) x = 0; 22 | for(auto x:visited) x = 0; 23 | order.clear(); 24 | answer.clear(); 25 | inputs.clear(); 26 | s1.clear(); s2.clear(); 27 | } 28 | 29 | void dfs(int depth) { 30 | if(depth == n) { 31 | answer.push_back(order); 32 | return; 33 | } 34 | for(int u : inputs) { 35 | if(!visited[u] && inDegree[u] == 0) { 36 | visited[u] = true; 37 | order.push_back(u); 38 | 39 | 40 | for(auto v:adj[u]) inDegree[v]--; 41 | 42 | dfs(depth+1); 43 | 44 | for(auto v:adj[u]) inDegree[v]++; 45 | 46 | 47 | order.pop_back(); 48 | visited[u] = false; 49 | } 50 | } 51 | } 52 | 53 | void topologicalSort() { 54 | dfs(0); 55 | 56 | if(!answer.size()){ 57 | cout << "NO\n"; return; 58 | } 59 | for(auto x:answer){ 60 | for(int i = 0 ; i < x.size() ; i++){ 61 | char ch = x[i] + 'A'; 62 | cout << ch << (i == x.size() - 1 ? "\n":" "); 63 | } 64 | } 65 | } 66 | void solve(){ 67 | CLEAR(); 68 | getline(cin >> ws,s1); 69 | getline(cin >> ws,s2); 70 | 71 | for(auto x:s1) if(!isspace(x)) inputs.insert(x - 'A'); 72 | n = inputs.size(); 73 | 74 | 75 | for(int i = 0 ; i < s2.size(); i++) { 76 | if(s2[i] == '<'){ 77 | adj[s2[i-1] - 'A'].push_back(s2[i+1] - 'A'); 78 | inDegree[s2[i+1] - 'A']++; 79 | } 80 | } 81 | 82 | // for(auto x:adj) 83 | // for(auto y:inputs) cout << y << ' '; 84 | topologicalSort(); 85 | } 86 | 87 | int main() { 88 | std::ios::sync_with_stdio(false);cin.tie(NULL); 89 | #ifndef ONLINE_JUDGE 90 | freopen( "in.txt" , "r" ,stdin); 91 | freopen("out.txt", "w", stdout); 92 | // freopen("Error.txt","w",stderr); 93 | #endif // ONLINE_JUDGE 94 | 95 | int t ; cin >> t; 96 | 97 | rep(i,0,t){ 98 | solve(); 99 | if(i != t-1) cout << "\n"; 100 | } 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /Backtracking/Problems/CSES creating strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | const int N = 10; 20 | int n; 21 | set ans,final; 22 | string res; 23 | bool taken[N] = {}; 24 | 25 | void recur(){ 26 | if(res.size() == n){ 27 | ans.insert(res); return; 28 | } 29 | 30 | rep(i,0,n){ 31 | if(!taken[i]){ 32 | res.push_back(i); 33 | taken[i] = 1; 34 | 35 | recur(); 36 | 37 | taken[i] = 0; 38 | res.pop_back(); 39 | } 40 | } 41 | } 42 | 43 | void solve() { 44 | string s; cin >> s; 45 | n = s.size(); 46 | 47 | 48 | recur(); 49 | 50 | 51 | for(auto x:ans){ 52 | string curr; 53 | for(auto y:x) curr += s[y]; 54 | 55 | final.insert(curr); 56 | } 57 | cout << final.size() << '\n'; 58 | 59 | for(auto x:final){ 60 | cout << x <<'\n'; 61 | } 62 | 63 | } 64 | 65 | /*void solve() { // This solution also gives AC 66 | 67 | string s; cin >> s; sort(s.begin(),s.end()); 68 | std::vector ans; 69 | 70 | do{ 71 | ans.push_back(s); 72 | }while(next_permutation(s.begin(),s.end())); 73 | 74 | cout << ans.size() << '\n'; 75 | 76 | for(auto x:ans) cout << x << '\n'; 77 | 78 | }*/ 79 | 80 | int32_t main() 81 | { 82 | std::ios::sync_with_stdio(false);cin.tie(NULL); 83 | IO(); 84 | solve(); 85 | 86 | return 0; 87 | } 88 | 89 | -------------------------------------------------------------------------------- /Backtracking/README.md: -------------------------------------------------------------------------------- 1 | ![](assets/cp%20banner.jpeg) 2 | 3 | > **Concepts** 4 | 5 | |No|________|Concept|Details| 6 | |-|-|-|-| 7 | |**1**.| |**Generate all permutations**|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Backtracking/Concepts/Generate%20all%20permutations.cpp)| 8 | 9 |
10 | 11 | > **Problems** 12 | 13 | |No|________|Problem|Solution| 14 | |-|-|-|-| 15 | |**1**.| |[**CSES creating strings**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Backtracking/Problems/CSES%20creating%20strings.cpp)| -------------------------------------------------------------------------------- /Backtracking/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/banner gif.gif -------------------------------------------------------------------------------- /Backtracking/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/codejam.png -------------------------------------------------------------------------------- /Backtracking/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Backtracking/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Backtracking/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/cses.PNG -------------------------------------------------------------------------------- /Backtracking/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/cses2.jpg -------------------------------------------------------------------------------- /Backtracking/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/icpc.jpeg -------------------------------------------------------------------------------- /Backtracking/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/icpc.jpg -------------------------------------------------------------------------------- /Backtracking/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/icpc.png -------------------------------------------------------------------------------- /Backtracking/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/images.png -------------------------------------------------------------------------------- /Backtracking/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/mix.png -------------------------------------------------------------------------------- /Backtracking/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/mix2.jpg -------------------------------------------------------------------------------- /Backtracking/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Backtracking/assets/my logo.jpeg -------------------------------------------------------------------------------- /Dynamic programming/CSES Coin Combinations I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | // #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | const int MOD = 1e9+7; 19 | 20 | void solve(int t){ 21 | int n , x; cin >> n >> x; 22 | 23 | int a[n]; 24 | rep(i,0,n) cin >> a[i]; 25 | 26 | int dp[x+1] = {}; dp[0] = 1; 27 | 28 | rep(i,1,x+1){ 29 | for(auto x:a) 30 | { 31 | if(i - x >= 0) {dp[i] += dp[i-x]; dp[i] %= MOD;} 32 | } 33 | } 34 | 35 | cout << dp[x] << '\n'; 36 | } 37 | int32_t main() 38 | { 39 | std::ios::sync_with_stdio(false);cin.tie(NULL); 40 | IO(); 41 | int t = 1; 42 | // cin >> t; 43 | 44 | rep(i,1,t+1){ 45 | solve(i); 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Dynamic programming/CSES Coin Combinations II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | // #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | const int MOD = 1e9+7; 19 | 20 | void solve(int t){ 21 | int n , x; cin >> n >> x; 22 | 23 | int a[n]; 24 | rep(i,0,n) cin >> a[i]; 25 | 26 | int dp[x+1] = {}; dp[0] = 1; 27 | 28 | for(auto ele:a){ 29 | rep(i,1,x+1) 30 | { 31 | if(i - ele >= 0) {dp[i] += dp[i-ele]; dp[i] %= MOD;} 32 | } 33 | } 34 | 35 | cout << dp[x] << '\n'; 36 | } 37 | int32_t main() 38 | { 39 | std::ios::sync_with_stdio(false);cin.tie(NULL); 40 | IO(); 41 | int t = 1; 42 | // cin >> t; 43 | 44 | rep(i,1,t+1){ 45 | solve(i); 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Dynamic programming/CSES Dice Combinations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | // #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | const int MOD = 1e9+7; 19 | 20 | void solve(int t){ 21 | int n; cin >> n; 22 | 23 | int dp[n+1] = {}; dp[0] = 1; 24 | rep(i,1,n+1){ 25 | for (int j = 1; j <= min(i,6); ++j) 26 | { 27 | dp[i] += dp[i-j]; dp[i] %= MOD; 28 | } 29 | } 30 | 31 | cout << dp[n] << '\n'; 32 | } 33 | int32_t main() 34 | { 35 | std::ios::sync_with_stdio(false);cin.tie(NULL); 36 | IO(); 37 | int t = 1; 38 | cin >> t; 39 | 40 | rep(i,1,t+1){ 41 | solve(i); 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Dynamic programming/CSES Grid Paths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | #define endl '\n' 6 | 7 | void IO() 8 | { 9 | #ifndef ONLINE_JUDGE 10 | freopen( "in.txt" , "r" ,stdin); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt","w",stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | #define int long long int 16 | #define all(x) x.begin(),x.end() 17 | 18 | const int MOD = 1e9+7; 19 | const int N = 1001; 20 | int score [N][N]; 21 | char grid[N][N]; 22 | int n ; 23 | 24 | bool isValid(int p,int q){ 25 | return (p < 0 or p >= n or q < 0 or q >= n? 0:1) ; 26 | } 27 | 28 | int dx[] = {0,1}; 29 | int dy[] = {1,0}; 30 | int vis[N][N]; 31 | 32 | void bfs(){ 33 | queue> q; 34 | q.push({0,0}); 35 | 36 | if(grid[0][0] == '.') score[0][0] = 1; 37 | 38 | while(!q.empty()){ 39 | int x = q.front().first , y = q.front().second; 40 | q.pop(); 41 | 42 | rep(i,0,2){ 43 | int X = x + dx[i]; int Y = y + dy[i]; 44 | 45 | if(isValid(X,Y)){ 46 | if(grid[X][Y] == '.'){ 47 | score[X][Y] += score[x][y] ; 48 | score[X][Y] %= MOD; 49 | if(!vis[X][Y] ){q.push({X,Y}); vis[X][Y] = 1;} 50 | } 51 | } 52 | } 53 | } 54 | } 55 | 56 | void solve(){ 57 | cin >> n; 58 | rep(i,0,n) rep(j,0,n) cin >> grid[i][j]; 59 | 60 | bfs(); 61 | 62 | cout << score[n-1][n-1] << '\n'; 63 | } 64 | int32_t main() 65 | { 66 | std::ios::sync_with_stdio(false);cin.tie(NULL); 67 | IO(); 68 | solve(); 69 | 70 | return 0; 71 | } 72 | 73 | 74 | -------------------------------------------------------------------------------- /Dynamic programming/CSES Removing Digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | const int MOD = 1e9+7; 20 | const int N = 1e6 + 3; 21 | vector dp(N,INT_MAX); 22 | int n; 23 | 24 | void solve(int t){ 25 | rep(i,1,10) dp[i] = 1; 26 | cin >> n; 27 | 28 | rep(i,10,n+1){ 29 | stringstream p; 30 | string s; 31 | 32 | p << i; p >> s; 33 | for(auto ch:s){ 34 | if(ch == '0') continue; 35 | int ans = dp[i - (ch-'0')]+1; 36 | dp[i] = min(dp[i],ans); 37 | } 38 | } 39 | 40 | cout << dp[n] << '\n'; 41 | 42 | } 43 | int32_t main() 44 | { 45 | std::ios::sync_with_stdio(false);cin.tie(NULL); 46 | IO(); 47 | 48 | int t = 1; 49 | // cin >> t; 50 | 51 | rep(i,1,t+1){ 52 | solve(i); 53 | } 54 | 55 | return 0; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Dynamic programming/Frog 2 AtCoder - dp_b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 1e5 + 7; 9 | const int MX = 1e6; 10 | std::vector graph[N]; 11 | vector lvl(N, -1); 12 | 13 | void FastIO(){ 14 | #ifndef ONLINE_JUDGE 15 | freopen("in.txt", "r", stdin ); 16 | // freopen("out.txt", "w", stdout); 17 | // freopen("Error.txt", "w", stderr); 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | #define int long long 22 | vector cost(N,INT_MAX); 23 | 24 | void solve(int t){ 25 | int n,k; 26 | cin >> n >> k; 27 | 28 | int arr[n]; 29 | rep(i,0,n) cin >> arr[i]; 30 | 31 | cost[0] = 0; 32 | rep(i,1,n){ 33 | for(int j = i-1 ; j >= max(0ll,i-k) ; j--){ 34 | cost[i] = min(cost[i] , cost[j] + abs(arr[i] - arr[j])); 35 | } 36 | } 37 | 38 | cout << cost[n-1] << '\n'; 39 | } 40 | 41 | int32_t main() 42 | { 43 | FastIO(); 44 | 45 | int t = 1; 46 | // cin >> t; 47 | 48 | rep(i,1,t+1){ 49 | solve(i); 50 | } 51 | return 0; 52 | } 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Dynamic programming/README.md: -------------------------------------------------------------------------------- 1 | ![](assets/cp%20banner.jpeg) 2 | 3 | > **Problems & Solutions** 4 | 5 | |No|________|Problem|Solution| 6 | |-|-|-|-| 7 | |**1**.| |[**CSES Coin Combinations I**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Dynamic%20programming/CSES%20Coin%20Combinations%20I.cpp)| 8 | |**2**.| |[**CSES Coin Combinations II**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Dynamic%20programming/CSES%20Coin%20Combinations%20II.cpp)| 9 | |**3**.| |[**CSES Dice Combinations**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Dynamic%20programming/CSES%20Dice%20Combinations.cpp)| 10 | |**4**.| |[**CSES Grid Paths**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Dynamic%20programming/CSES%20Grid%20Paths.cpp)| 11 | |**5**.| |[**CSES Removing Digits**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Dynamic%20programming/CSES%20Removing%20Digits.cpp)| 12 | |**6**.| |[**Frog 1 AtCoder - dp_a**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Dynamic%20programming/Frog%201%20AtCoder%20-%20dp_a%20.cpp)| 13 | |**7**.| |[**Frog 2 AtCoder - dp_b**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Dynamic%20programming/Frog%202%20AtCoder%20-%20dp_b.cpp)| 14 | |**8**.| |[**Vacation AtCoder - dp_c**]()|[**Code**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Dynamic%20programming/Vacation%20AtCoder%20-%20dp_c.cpp)| 15 | -------------------------------------------------------------------------------- /Dynamic programming/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/banner gif.gif -------------------------------------------------------------------------------- /Dynamic programming/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/codejam.png -------------------------------------------------------------------------------- /Dynamic programming/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Dynamic programming/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Dynamic programming/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/cses.PNG -------------------------------------------------------------------------------- /Dynamic programming/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/cses2.jpg -------------------------------------------------------------------------------- /Dynamic programming/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/icpc.jpeg -------------------------------------------------------------------------------- /Dynamic programming/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/icpc.jpg -------------------------------------------------------------------------------- /Dynamic programming/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/icpc.png -------------------------------------------------------------------------------- /Dynamic programming/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/images.png -------------------------------------------------------------------------------- /Dynamic programming/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/mix.png -------------------------------------------------------------------------------- /Dynamic programming/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/mix2.jpg -------------------------------------------------------------------------------- /Dynamic programming/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Dynamic programming/assets/my logo.jpeg -------------------------------------------------------------------------------- /Graph theory/Concepts/BFS traversal[Level of a tree].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | const int N=1e4+1; 6 | int vis[N]; 7 | std::vector graph[N]; 8 | int level[N]; 9 | 10 | vector bfs(int starting_node) 11 | { 12 | vector v; 13 | queue q; q.push(starting_node); 14 | vis[starting_node]=1; 15 | level[starting_node]=0; 16 | 17 | while(!q.empty()) 18 | { 19 | int curr=q.front(); 20 | q.pop(); 21 | 22 | v.push_back(curr); 23 | for(auto x:graph[curr]) 24 | if(vis[x]==0) 25 | { 26 | q.push(x); vis[x]=1; 27 | level[x]=level[curr]+1; 28 | } 29 | } 30 | 31 | return v; 32 | } 33 | 34 | 35 | int main() 36 | { 37 | freopen("in.txt","r", stdin); 38 | int node,edges; cin>>node>>edges; 39 | 40 | for(int i=0;i>x>>y; 43 | graph[x].push_back(y); graph[y].push_back(x); 44 | } 45 | 46 | vector ans = bfs(1); 47 | 48 | for(auto x:ans) cout< 2 | using namespace std; 3 | 4 | 5 | const int N=107; 6 | int vis[N]; 7 | std::vector graph[N]; 8 | int level[N]; 9 | 10 | vector bfs(int starting_node) 11 | { 12 | vector v; 13 | queue q; 14 | q.push(starting_node); 15 | vis[starting_node] = 1; 16 | 17 | while(!q.empty()) 18 | { 19 | int curr=q.front(); 20 | q.pop(); vis[curr]=1; 21 | 22 | v.push_back(curr); 23 | for(auto x:graph[curr]) 24 | if(vis[x]==0) q.push(x); 25 | 26 | } 27 | return v; 28 | } 29 | 30 | 31 | int main() 32 | { 33 | freopen("in.txt","r", stdin); 34 | 35 | int node,edges; cin>>node>>edges; 36 | 37 | for(int i=0;i>x>>y; 40 | graph[x].push_back(y); graph[y].push_back(x); 41 | } 42 | 43 | vector ans = bfs(1); 44 | 45 | for(auto x:ans) cout< 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | // #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | const int N = 1e5 + 7; 20 | vector g[N]; 21 | bool ok = 1; 22 | int color[N]; 23 | bool vis[N]; 24 | 25 | void dfs(int par,int clr){ 26 | if(!ok) return; 27 | vis[par] = 1; 28 | color[par] = clr; 29 | 30 | for(auto child:g[par]){ 31 | if(!vis[child]) dfs(child,clr ^ 1); 32 | else if(color[child] == color[par]) {ok = 0; break;} 33 | } 34 | } 35 | 36 | void solve(int t){ 37 | int n,m; 38 | cin >> n >> m; 39 | 40 | rep(i,0,m){ int u,v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u);} 41 | 42 | rep(i,1,n+1) if(!vis[i]) dfs(i,0); 43 | 44 | if(!ok) { cout << "IMPOSSIBLE\n"; return;} 45 | 46 | rep(i,1,n+1) cout << color[i] + 1 << ' '; cout << '\n'; 47 | } 48 | int32_t main() 49 | { 50 | std::ios::sync_with_stdio(false);cin.tie(NULL); 51 | IO(); 52 | int t = 1; 53 | // cin >> t; 54 | 55 | rep(i,1,t+1){ 56 | solve(i); 57 | } 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Graph theory/Concepts/Cycle detection of undirected graph and printing the cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | // #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | const int N = 2e5 + 7; 19 | vector g[N]; 20 | vector ans; 21 | bool vis[N]; 22 | bool found = 0; 23 | int strt,ed; 24 | int par[N] = {}; 25 | 26 | void dfs(int curr,int prev){ 27 | 28 | // cout << curr << ' ' << prev << '\n'; 29 | if(found) return; 30 | vis[curr] = 1; 31 | par[curr] = prev; 32 | 33 | for(auto child:g[curr]){ 34 | if(!vis[child]) dfs(child,curr); 35 | else if(vis[child] and child != prev and !found) { 36 | strt = curr; 37 | ed = child; 38 | found = 1; 39 | return; 40 | } 41 | } 42 | } 43 | 44 | void solve(int t){ 45 | int n,m; cin >> n >> m; 46 | 47 | rep(i,0,m){ 48 | int u,v; cin >> u >> v; 49 | g[u].push_back(v); g[v].push_back(u); 50 | } 51 | 52 | rep(i,1,n+1){ 53 | if(!vis[i]){ 54 | ans.clear(); 55 | dfs(i,0); 56 | } 57 | 58 | if(found){ 59 | while(strt != ed){ 60 | ans.push_back(strt); strt = par[strt]; 61 | } 62 | 63 | ans.push_back(ed); 64 | cout << "Path size = " << ans.size()+1 << '\n'; 65 | 66 | cout << "Cycle Path is --> " ; 67 | for(auto x:ans) 68 | cout << x << ' '; 69 | cout << ans[0] << '\n'; 70 | 71 | break; 72 | } 73 | } 74 | 75 | if(!found) cout << "No cycle\n"; 76 | } 77 | int32_t main() 78 | { 79 | std::ios::sync_with_stdio(false);cin.tie(NULL); 80 | IO(); 81 | int t = 1; 82 | // cin >> t; 83 | 84 | rep(i,1,t+1){ 85 | solve(i); 86 | } 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Graph theory/Concepts/Cycle detection on DAG[using BFS].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | const int N = 105; 18 | vector g[N]; 19 | int indeg[N]; 20 | bool vis[N]; 21 | vector ans; 22 | 23 | void bfs(queue q){ 24 | while(!q.empty()){ 25 | int curr = q.front(); 26 | q.pop(); 27 | 28 | ans.push_back(curr); 29 | for(auto child:g[curr]){ 30 | indeg[child]--; 31 | if(indeg[child] == 0 and !vis[child]){ 32 | vis[child] = 1; 33 | q.push(child); 34 | } 35 | } 36 | } 37 | 38 | } 39 | void CLEAR(){ 40 | ans.clear(); 41 | rep(i,0,N) indeg[i] = 0 , g[i].clear() , vis[i] = 0; 42 | } 43 | 44 | void solve(int t){ 45 | int u,v,n,m; 46 | while(cin >> n >> m){ 47 | if(!n and !m) break; 48 | rep(i,0,m){ 49 | cin >> u >> v; 50 | g[u].push_back(v); 51 | indeg[v]++; 52 | } 53 | 54 | queue q; 55 | rep(i,1,n+1) if(!indeg[i]){ q.push(i); vis[i] = 1;} 56 | bfs(q); 57 | 58 | 59 | if(ans.size() == n) for(auto x:ans) cout << x << ' '; 60 | else cout << "Cycle detected"; cout << '\n'; 61 | CLEAR(); 62 | } 63 | } 64 | 65 | int32_t main() 66 | { 67 | IO(); 68 | std::ios::sync_with_stdio(false);cin.tie(NULL); 69 | 70 | int t = 1; 71 | // cin >> t; 72 | 73 | rep(i,1,t+1){ 74 | solve(i); 75 | } 76 | return 0; 77 | } 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /Graph theory/Concepts/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N=1e4+1; 5 | int vis[N]; 6 | std::vector graph[N]; 7 | 8 | void dfs(int starting_node) 9 | { 10 | cout << starting_node << ' '; 11 | vis[starting_node] = 1; 12 | 13 | for(auto child:graph[starting_node]) 14 | if(!vis[child]) dfs(child); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | freopen("in.txt","r", stdin); 21 | int node,edges; cin>>node>>edges; 22 | 23 | for(int i=0;i>x>>y; 26 | graph[x].push_back(y); graph[y].push_back(x); 27 | } 28 | 29 | dfs(1); 30 | 31 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int MAXN = 105; 7 | 8 | vector adj[MAXN]; 9 | int inDegree[MAXN]; 10 | bool visited[MAXN]; 11 | vector order; 12 | int n, m; 13 | vector> answer; 14 | 15 | 16 | void dfs(int par,int depth) { 17 | visited[par] = true; 18 | order.push_back(par); 19 | 20 | if(depth == n) { 21 | answer.push_back(order); 22 | return; 23 | } 24 | 25 | for(int child:adj[par]) 26 | if(!visited[child]) { 27 | dfs(child,depth+1); 28 | 29 | order.pop_back(); 30 | visited[child] = false; 31 | } 32 | 33 | 34 | } 35 | 36 | void solve(){ 37 | 38 | cin >> n >> m; 39 | 40 | for (int i = 0; i < m; ++i) 41 | { 42 | int u,v; 43 | cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); 44 | } 45 | 46 | dfs(1,1); 47 | cout << "Number of paths = " << answer.size() << '\n'; cout << '\n'; 48 | 49 | for(auto x:answer){ for(auto y :x) cout << y << ' '; cout << '\n';} 50 | } 51 | 52 | int main() { 53 | #ifndef ONLINE_JUDGE 54 | freopen( "in.txt" , "r" ,stdin); 55 | // freopen("out.txt", "w", stdout); 56 | // freopen("Error.txt","w",stderr); 57 | #endif // ONLINE_JUDGE 58 | 59 | 60 | solve(); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Graph theory/Concepts/TopSort to generate all orderings & detect cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 100; 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | 7 | 8 | vector adj[MAXN]; 9 | int inDegree[MAXN]; 10 | bool visited[MAXN]; 11 | vector order; 12 | 13 | vector> answer; 14 | set inputs; 15 | int n; 16 | string s1,s2; 17 | 18 | 19 | void CLEAR(){ 20 | for(auto x:adj) x.clear(); 21 | for(auto x:inDegree) x = 0; 22 | for(auto x:visited) x = 0; 23 | order.clear(); 24 | answer.clear(); 25 | inputs.clear(); 26 | s1.clear(); s2.clear(); 27 | } 28 | 29 | void dfs(int depth) { 30 | if(depth == n) { 31 | answer.push_back(order); 32 | return; 33 | } 34 | for(int u : inputs) { 35 | if(!visited[u] && inDegree[u] == 0) { 36 | visited[u] = true; 37 | order.push_back(u); 38 | 39 | 40 | for(auto v:adj[u]) inDegree[v]--; 41 | 42 | dfs(depth+1); 43 | 44 | for(auto v:adj[u]) inDegree[v]++; 45 | 46 | 47 | order.pop_back(); 48 | visited[u] = false; 49 | } 50 | } 51 | } 52 | 53 | void topologicalSort() { 54 | dfs(0); 55 | 56 | if(!answer.size()){ 57 | cout << "NO\n"; return; 58 | } 59 | for(auto x:answer){ 60 | for(int i = 0 ; i < x.size() ; i++){ 61 | char ch = x[i] + 'A'; 62 | cout << ch << (i == x.size() - 1 ? "\n":" "); 63 | } 64 | } 65 | } 66 | void solve(){ 67 | CLEAR(); 68 | getline(cin >> ws,s1); 69 | getline(cin >> ws,s2); 70 | 71 | for(auto x:s1) if(!isspace(x)) inputs.insert(x - 'A'); 72 | n = inputs.size(); 73 | 74 | 75 | for(int i = 0 ; i < s2.size(); i++) { 76 | if(s2[i] == '<'){ 77 | adj[s2[i-1] - 'A'].push_back(s2[i+1] - 'A'); 78 | inDegree[s2[i+1] - 'A']++; 79 | } 80 | } 81 | 82 | // for(auto x:adj) 83 | // for(auto y:inputs) cout << y << ' '; 84 | topologicalSort(); 85 | } 86 | 87 | int main() { 88 | std::ios::sync_with_stdio(false);cin.tie(NULL); 89 | #ifndef ONLINE_JUDGE 90 | freopen( "in.txt" , "r" ,stdin); 91 | freopen("out.txt", "w", stdout); 92 | // freopen("Error.txt","w",stderr); 93 | #endif // ONLINE_JUDGE 94 | 95 | int t ; cin >> t; 96 | 97 | rep(i,0,t){ 98 | solve(); 99 | if(i != t-1) cout << "\n"; 100 | } 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /Graph theory/Concepts/TopSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | 6 | const int N = 100007; 7 | std::vector graph[N]; 8 | bool vis[N]; 9 | 10 | stack ans; 11 | 12 | void dfs(int source){ 13 | vis[source] = 1; 14 | 15 | for(auto child:graph[source]){ 16 | if(!vis[child]) dfs(child); 17 | } 18 | ans.push(source); // when all the childs of a node have been visited then we will push it into the stack 19 | } 20 | 21 | void solve(int t){ 22 | int node,edge; cin >> node >> edge; 23 | 24 | rep(i,0,m){ 25 | int u , v; 26 | cin >> u >> v; 27 | graph[u].push_back(v); // directed graph 28 | } 29 | 30 | rep(i,1,node+1) 31 | if(!vis[i]) 32 | dfs(i); 33 | 34 | while(!ans.empty()){ 35 | cout << ans.top() << ' '; ans.pop(); 36 | } 37 | cout << '\n'; 38 | } 39 | 40 | int32_t main() 41 | { 42 | int t = 1; 43 | // cin >> t; 44 | 45 | rep(i,1,t+1){ 46 | solve(i); 47 | } 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Graph theory/Concepts/TopSort[BFS (kahn's algo)].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | 9 | void IO() 10 | { 11 | #ifndef ONLINE_JUDGE 12 | freopen( "in.txt" , "r" ,stdin); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt","w",stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | // #define int long long int 18 | #define all(x) x.begin(),x.end() 19 | 20 | const int N = 1e5 + 7; 21 | vector ans; 22 | int n,m; 23 | int indeg[N] = {}; 24 | vector g[N]; 25 | bool vis[N]; 26 | 27 | void bfs(){ 28 | queue q; 29 | rep(i,1,n+1) if(indeg[i] == 0){ q.push(i); vis[i] = 1;} 30 | 31 | while(!q.empty()){ 32 | int parent = q.front(); 33 | ans.push_back(parent); q.pop(); 34 | 35 | for(auto child:g[parent]){ 36 | indeg[child]--; 37 | 38 | if(!indeg[child] and !vis[child]){ 39 | vis[child] = 1; q.push(child); 40 | } 41 | } 42 | } 43 | } 44 | 45 | void solve(int t){ 46 | cin >> n >> m; 47 | 48 | rep(i,0,m){ 49 | int u,v; cin >> u >> v; 50 | g[u].push_back(v); 51 | indeg[v]++; 52 | } 53 | 54 | bfs(); 55 | 56 | if(ans.size() == n){ for(auto x:ans) cout << x << ' ';} 57 | else cout << "IMPOSSIBLE\n"; 58 | } 59 | int32_t main() 60 | { 61 | std::ios::sync_with_stdio(false);cin.tie(NULL); 62 | IO(); 63 | int t = 1; 64 | // cin >> t; 65 | 66 | rep(i,1,t+1){ 67 | solve(i); 68 | } 69 | 70 | return 0; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Graph theory/Concepts/dijkstra [ using set].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | #define int long long 6 | const int N = 1e5 + 5; 7 | const int INF = LLONG_MAX; 8 | 9 | // solution of CF 20C dijkstra? 10 | vector< pair > graph[N]; 11 | vector dist(N,INF); 12 | vector path(N,0); 13 | bool vis[N]; 14 | 15 | void dijkstra() 16 | { 17 | set> q; 18 | q.insert({0,1}); 19 | dist[1] = 0; 20 | 21 | while(!q.empty()){ 22 | int curr_dist=(*q.begin()).first , curr_edge = (*q.begin()).second; 23 | q.erase(*q.begin()); 24 | 25 | if(vis[curr_edge]) continue; 26 | vis[curr_edge] = 1; 27 | 28 | for(auto child:graph[curr_edge]){ 29 | int child_edge = child.second , child_dist = child.first; 30 | 31 | if( curr_dist + child_dist < dist[child_edge]){ 32 | dist[child_edge] = curr_dist + child_dist; 33 | q.insert({dist[child_edge],child_edge}); 34 | path[child_edge] = curr_edge; 35 | } 36 | } 37 | } 38 | } 39 | 40 | int32_t main() 41 | { 42 | int node,edges; cin >> node >> edges; 43 | 44 | for(int i=0;i> x >> y >> z; 47 | if( x == y) continue; 48 | graph[x].push_back({z,y}); // graph[y].push_back({z,x}); 49 | } 50 | 51 | vector route; 52 | dijkstra(); 53 | 54 | for (int i = 1; i <= node; ++i) 55 | { 56 | cout << dist[i] << ' '; 57 | } 58 | 59 | return 0; 60 | 61 | } -------------------------------------------------------------------------------- /Graph theory/Concepts/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | #define int long long 6 | const int N = 1e5 + 5; 7 | const int INF = 1e12+10; 8 | 9 | // solution of CF 20C dijkstra? 10 | vector< pair > graph[N]; 11 | vector dist(N,INF); 12 | vector path(N,0); 13 | bool vis[N]; 14 | 15 | void dijkstra() 16 | { 17 | set> q; 18 | q.insert({0,1}); 19 | dist[1] = 0; 20 | 21 | while(!q.empty()){ 22 | int curr_dist=(*q.begin()).first , curr_edge = (*q.begin()).second; 23 | q.erase(*q.begin()); 24 | 25 | if(vis[curr_edge]) continue; 26 | vis[curr_edge] = 1; 27 | 28 | for(auto child:graph[curr_edge]){ 29 | int child_edge = child.second , child_dist = child.first; 30 | 31 | if( curr_dist + child_dist < dist[child_edge]){ 32 | dist[child_edge] = curr_dist + child_dist; 33 | q.insert({dist[child_edge],child_edge}); 34 | path[child_edge] = curr_edge; 35 | } 36 | } 37 | } 38 | } 39 | 40 | void print(int node){ 41 | if(node == 1){ 42 | cout << node << ' '; return; 43 | } 44 | print(path[node]); 45 | 46 | cout << node << ' '; 47 | } 48 | 49 | int32_t main() 50 | { 51 | int node,edges; cin >> node >> edges; 52 | 53 | for(int i=0;i> x >> y >> z; 56 | if( x == y) continue; 57 | graph[x].push_back({z,y}); graph[y].push_back({z,x}); 58 | } 59 | 60 | vector route; 61 | dijkstra(); 62 | 63 | for (int i = 1; i <= node; ++i) 64 | { 65 | cout << dist[i] << ' '; 66 | } 67 | // if(path[node] == 0) { cout << -1 << '\n'; return 0;} 68 | // print(node); // can print it by storing values in vector and then reversing it 69 | 70 | return 0; 71 | 72 | } 73 | -------------------------------------------------------------------------------- /Graph theory/Problems/BFS & DFS traversal/Aizu - ALDS1_11_C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 107; 9 | const int MX = 1e6; 10 | std::vector graph[N]; 11 | vector lvl(N, -1); 12 | 13 | void FastIO(){ 14 | #ifndef ONLINE_JUDGE 15 | freopen("in.txt", "r", stdin ); 16 | // freopen("out.txt", "w", stdout); 17 | // freopen("Error.txt", "w", stderr); 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | void bfs(){ 22 | queue q; 23 | q.push(1); 24 | lvl[1] = 0; 25 | 26 | 27 | while(!q.empty()){ 28 | int u = q.front(); 29 | int currLvl = lvl[u]; 30 | q.pop(); 31 | 32 | for(auto v:graph[u]){ 33 | if(lvl[v] == -1){ 34 | q.push(v); 35 | lvl[v] = currLvl + 1; 36 | } 37 | 38 | } 39 | } 40 | } 41 | void solve(int t){ 42 | int n; 43 | cin >> n; 44 | 45 | rep(i,0,n){ 46 | int u; 47 | cin >> u; 48 | int m; 49 | cin >> m; 50 | 51 | rep(j,0,m){ 52 | int v; 53 | cin >> v; 54 | graph[u].push_back(v); 55 | } 56 | } 57 | bfs(); 58 | 59 | rep(i,1,n+1){ 60 | cout << i << ' ' << lvl[i] << endl; 61 | } 62 | } 63 | 64 | int32_t main() 65 | { 66 | FastIO(); 67 | 68 | int t = 1; 69 | // cin >> t; 70 | 71 | rep(i,1,t+1){ 72 | solve(i); 73 | } 74 | return 0; 75 | } 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Graph theory/Problems/Bipartite graph checking/CodeForces 862B_Mahmoud and Ehab and the bipartiteness.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef deque dq ; 6 | typedef list li; 7 | typedef vector vst ; 8 | typedef vector vll ; 9 | typedef deque::iterator dita; 10 | typedef double dd; 11 | typedef greater gt; 12 | typedef list::iterator liita; 13 | typedef vector::iterator vecita; 14 | #define immortal_slave int main() 15 | #define in(x) cin>>x; 16 | #define khalid std::ios::sync_with_stdio(false);cin.tie(NULL); 17 | #define mp make_pair 18 | #define out(x) cout<= b;--i) 32 | #define rep3j(j,a,b) for(ll j = a; j >= b;--j) 33 | #define ss stringstream 34 | #define st string 35 | #define min3(a,b,c) min( a,min( b, c)) 36 | #define min4(a,b,c,d) min( d,min3( a, b, c)) 37 | #define max3(a,b,c) max( a,max( b, c)) 38 | #define max4(a,b,c,d) max( d,max3(a,b,c)) 39 | #define sum(v) accumulate(v.begin(),v.end(),0ll) 40 | #define lcm(a,b) (a*b)/__gcd(a,b) 41 | #define all(x) x.begin(),x.end() 42 | //Starting in the name of Allah(THE ALMIGHTY) 43 | void FastIO() 44 | { 45 | khalid 46 | #ifndef ONLINE_JUDGE 47 | freopen("in.txt","r",stdin); 48 | freopen("out.txt","w",stdout); 49 | #endif // ONLINE_JUDGE 50 | } 51 | int dx[]={1,-1,0,0}; 52 | int dy[]={0,0,1,-1}; 53 | 54 | const int N = 2e5+5; 55 | const ll mod = 1e9+7; 56 | 57 | vector graph[N]; 58 | int vis[N]; 59 | ll one=0,zero=0; 60 | void dfs(int p,int color) 61 | { 62 | if(vis[p]!=-1) return; 63 | 64 | vis[p]=color; 65 | 66 | if(color) one++; else zero++; 67 | 68 | for(auto x:graph[p]) dfs(x,1-color); 69 | } 70 | 71 | void solve() 72 | { 73 | int n; in(n) 74 | rep(0,n-1) 75 | { 76 | ll a,b; in(a>>b) 77 | graph[a].pb(b); graph[b].pb(a); 78 | } 79 | 80 | rep2(1,n) 81 | if(vis[i]==-1) dfs(i,0); 82 | 83 | ll ans = one * zero - (n-1); 84 | out(ans) 85 | 86 | } 87 | 88 | immortal_slave 89 | { 90 | FastIO(); 91 | 92 | ll t=1; 93 | //in(t) 94 | 95 | memset(vis,-1,sizeof(vis)); 96 | 97 | 98 | while(t--){ 99 | solve(); 100 | } 101 | 102 | return 0; 103 | 104 | } -------------------------------------------------------------------------------- /Graph theory/Problems/CSES Building Roads_1666[connected components].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | const int N = 1e5 + 7; 20 | int n,m; 21 | vector g[N]; 22 | vector> answer; 23 | bool vis[N]; 24 | vector curr; 25 | 26 | 27 | void dfs(int par){ 28 | vis[par] = 1; 29 | curr.push_back(par); 30 | 31 | for(auto child:g[par]) 32 | if(!vis[child]) dfs(child); 33 | } 34 | 35 | void solve(int t){ 36 | cin >> n >> m; 37 | 38 | rep(i,0,m){ 39 | int u,v; 40 | cin >> u >> v; 41 | g[u].push_back(v); g[v].push_back(u); 42 | } 43 | 44 | rep(i,1,n+1){ 45 | if(!vis[i]) { 46 | curr.clear(); dfs(i);// for(auto x:curr) cout << x << ' '; cout << '\n'; 47 | answer.push_back(curr); 48 | } 49 | 50 | } 51 | 52 | n = answer.size(); 53 | cout << n-1 << '\n'; 54 | 55 | if(answer.size()){ 56 | rep(i,1,n) 57 | cout << answer[i][0] << ' ' << answer[i-1][0] << '\n'; 58 | } 59 | 60 | } 61 | int32_t main() 62 | { 63 | std::ios::sync_with_stdio(false);cin.tie(NULL); 64 | IO(); 65 | int t = 1; 66 | // cin >> t; 67 | 68 | rep(i,1,t+1){ 69 | solve(i); 70 | } 71 | 72 | return 0; 73 | } 74 | 75 | -------------------------------------------------------------------------------- /Graph theory/Problems/CSES Building Teams_1668.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | // #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | const int N = 1e5 + 7; 20 | vector g[N]; 21 | bool ok = 1; 22 | int color[N]; 23 | bool vis[N]; 24 | 25 | void dfs(int par,int clr){ 26 | if(!ok) return; 27 | vis[par] = 1; 28 | color[par] = clr; 29 | 30 | for(auto child:g[par]){ 31 | if(!vis[child]) dfs(child,clr ^ 1); 32 | else if(color[child] == color[par]) {ok = 0; break;} 33 | } 34 | } 35 | 36 | void solve(int t){ 37 | int n,m; 38 | cin >> n >> m; 39 | 40 | rep(i,0,m){ int u,v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u);} 41 | 42 | rep(i,1,n+1) if(!vis[i]) dfs(i,0); 43 | 44 | if(!ok) { cout << "IMPOSSIBLE\n"; return;} 45 | 46 | rep(i,1,n+1) cout << color[i] + 1 << ' '; cout << '\n'; 47 | } 48 | int32_t main() 49 | { 50 | std::ios::sync_with_stdio(false);cin.tie(NULL); 51 | IO(); 52 | int t = 1; 53 | // cin >> t; 54 | 55 | rep(i,1,t+1){ 56 | solve(i); 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Graph theory/Problems/CSES Shortest Routes I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | #define int long long 6 | const int N = 1e5 + 5; 7 | const int INF = 1e18+10; 8 | 9 | // solution of CF 20C dijkstra? 10 | vector< pair > graph[N]; 11 | vector dist(N,INF); 12 | bool vis[N]; 13 | 14 | void dijkstra() 15 | { 16 | priority_queue, vector< pair >, greater< pair> > q; 17 | 18 | q.push({0,1}); 19 | dist[1] = 0; 20 | 21 | while(!q.empty()){ 22 | int curr_dist=q.top().first , curr_edge = q.top().second; 23 | q.pop(); 24 | 25 | if(vis[curr_edge]) continue; 26 | vis[curr_edge] = 1; 27 | 28 | for(auto child:graph[curr_edge]){ 29 | int child_edge = child.second , child_dist = child.first; 30 | 31 | if( curr_dist + child_dist < dist[child_edge]){ 32 | dist[child_edge] = curr_dist + child_dist; 33 | q.push({dist[child_edge],child_edge}); 34 | } 35 | } 36 | } 37 | } 38 | 39 | int32_t main() 40 | { 41 | #ifndef ONLINE_JUDGE 42 | freopen("in.txt","r", stdin); 43 | // freopen("out.txt", "w", stdout); 44 | freopen("Error.txt", "w", stderr); 45 | #endif // ONLINE_JUDGE 46 | 47 | int node,edges; cin >> node >> edges; 48 | 49 | for(int i=0;i> x >> y >> z; 52 | if( x == y) continue; 53 | graph[x].push_back({z,y}); 54 | } 55 | 56 | dijkstra(); 57 | 58 | for (int i = 1; i <= node; ++i) 59 | { 60 | cout << dist[i] << ' '; 61 | } 62 | // if(path[node] == 0) { cout << -1 << '\n'; return 0;} 63 | // print(node); // can print it by storing values in vector and then reversing it 64 | 65 | return 0; 66 | 67 | } -------------------------------------------------------------------------------- /Graph theory/Problems/CSES message route_1667.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | 17 | const int N = 2e5 + 7; 18 | #define int long long int 19 | #define all(x) x.begin(),x.end() 20 | #define INF 1e8 21 | 22 | int lvl[N]; 23 | vector g[N]; 24 | int source[N]; 25 | int n,m; 26 | 27 | void bfs(int par,int prev){ 28 | queue q; 29 | source[par] = prev; 30 | q.push(par); 31 | 32 | lvl[par] = 0; 33 | while(!q.empty()){ 34 | int curr = q.front(); q.pop(); 35 | 36 | for(auto child:g[curr]){ 37 | if(lvl[child] == INF) { 38 | q.push(child); 39 | source[child] = curr; 40 | lvl[child] = lvl[curr] + 1; 41 | } 42 | } 43 | } 44 | } 45 | 46 | void solve(int t){ 47 | 48 | cin >> n >> m; 49 | 50 | rep(i,0,m){ 51 | int u,v; cin >> u >> v; 52 | g[u].push_back(v); g[v].push_back(u); 53 | } 54 | 55 | bfs(1,0); 56 | vector ans; 57 | 58 | if(lvl[n] == INF) {cout << "IMPOSSIBLE\n"; return; } 59 | 60 | int curr = n; 61 | 62 | while(curr > 0){ 63 | ans.push_back(curr); 64 | curr = source[curr]; 65 | } 66 | 67 | reverse(ans.begin(),ans.end()); 68 | 69 | cout << ans.size() << '\n'; 70 | for(auto x:ans) cout << x << ' '; 71 | 72 | cout << '\n'; 73 | } 74 | int32_t main() 75 | { 76 | std::ios::sync_with_stdio(false);cin.tie(NULL); 77 | IO(); 78 | int t = 1; 79 | // cin >> t; 80 | 81 | rep(i,1,N) lvl[i] = INF; 82 | 83 | rep(i,1,t+1){ 84 | solve(i); 85 | } 86 | 87 | return 0; 88 | } 89 | 90 | -------------------------------------------------------------------------------- /Graph theory/Problems/CSES round_trip_1669.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | // #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | const int N = 2e5 + 7; 19 | vector g[N]; 20 | vector ans; 21 | bool vis[N]; 22 | bool found = 0; 23 | int strt,ed; 24 | int par[N] = {}; 25 | 26 | void dfs(int curr,int prev){ 27 | 28 | // cout << curr << ' ' << prev << '\n'; 29 | if(found) return; 30 | vis[curr] = 1; 31 | par[curr] = prev; 32 | 33 | for(auto child:g[curr]){ 34 | if(!vis[child]) dfs(child,curr); 35 | else if(vis[child] and child != prev and !found) { 36 | strt = curr; 37 | ed = child; 38 | found = 1; 39 | return; 40 | } 41 | } 42 | } 43 | 44 | void solve(int t){ 45 | int n,m; cin >> n >> m; 46 | 47 | rep(i,0,m){ 48 | int u,v; cin >> u >> v; 49 | g[u].push_back(v); g[v].push_back(u); 50 | } 51 | 52 | rep(i,1,n+1){ 53 | if(!vis[i]){ 54 | ans.clear(); 55 | dfs(i,0); 56 | } 57 | 58 | if(found){ 59 | while(strt != ed){ 60 | ans.push_back(strt); strt = par[strt]; 61 | } 62 | 63 | ans.push_back(ed); 64 | cout << ans.size()+1 << '\n'; 65 | 66 | for(auto x:ans) 67 | cout << x << ' '; 68 | cout << ans[0] << '\n'; 69 | 70 | break; 71 | } 72 | } 73 | 74 | if(!found) cout << "IMPOSSIBLE\n"; 75 | } 76 | int32_t main() 77 | { 78 | std::ios::sync_with_stdio(false);cin.tie(NULL); 79 | IO(); 80 | int t = 1; 81 | // cin >> t; 82 | 83 | rep(i,1,t+1){ 84 | solve(i); 85 | } 86 | 87 | return 0; 88 | } 89 | 90 | -------------------------------------------------------------------------------- /Graph theory/Problems/Connected components/UVa 459 - Graph Connectivity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | 20 | const int N = 28; 21 | vector g[N]; 22 | bool vis[N]; 23 | 24 | void dfs(int par){ 25 | vis[par] = 1; 26 | for(auto child:g[par]) 27 | if(!vis[child]) dfs(child); 28 | } 29 | 30 | void solve(int t){ 31 | char a; 32 | cin >> a; 33 | cin.ignore(); 34 | 35 | int n = a - 'A' + 1 , ans = 0; 36 | string s; 37 | 38 | while(getline(cin , s) and s != ""){ 39 | g[(s[0] - 'A') + 1].push_back((s[1] - 'A') + 1); 40 | g[(s[1] - 'A') + 1].push_back((s[0] - 'A') + 1); 41 | } 42 | 43 | rep(i,1,n+1){ 44 | if(!vis[i]){ ++ans;dfs(i);} 45 | } 46 | 47 | cout << ans << '\n'; 48 | 49 | rep(i,0,N) g[i].clear() , vis[i] = 0; 50 | } 51 | int32_t main() 52 | { 53 | std::ios::sync_with_stdio(false);cin.tie(NULL); 54 | IO(); 55 | 56 | int t = 1; 57 | cin >> t; 58 | 59 | rep(i,1,t+1){ 60 | solve(i); 61 | 62 | if(i != t) cout << "\n"; 63 | } 64 | 65 | return 0; 66 | } 67 | 68 | -------------------------------------------------------------------------------- /Graph theory/Problems/Grid problems/CSES Labyrinth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(a,b) for(int i = a; i < b;++i) 5 | #define repj(j,a,b) for(int j = a; j < b;++j) 6 | 7 | #define immortal_slave int main() 8 | #define khalid std::ios::sync_with_stdio(false);cin.tie(NULL); 9 | 10 | 11 | 12 | //Starting in the name of Allah(THE ALMIGHTY 13 | 14 | int dx[]={0,0,-1,1}; // R L U D 15 | int dy[]={1,-1,0,0}; 16 | 17 | const int N = 2000; 18 | const int mod = 1e9+7; 19 | 20 | int strtX,strtY,endX,endY,n,m; 21 | string ans; 22 | 23 | char graph[N][N]; 24 | void FastIO() 25 | { 26 | khalid 27 | #ifndef ONLINE_JUDGE 28 | freopen("in.txt","r", stdin); 29 | freopen("out.txt", "w", stdout); 30 | freopen("Error.txt", "w", stderr); 31 | #endif // ONLINE_JUDGE 32 | } 33 | 34 | void track(){ 35 | int x = endX, y = endY; 36 | while(1){ 37 | if(graph[x][y] == 'A') break; 38 | 39 | ans += graph[x][y]; 40 | 41 | switch(graph[x][y]){ 42 | case 'R': --y; break; 43 | case 'L': ++y; break; 44 | case 'U': ++x; break; 45 | case 'D': --x; break; 46 | } 47 | 48 | } 49 | } 50 | bool isValid(int X, int Y){ 51 | return ((X>=0 and X < n and Y >= 0 and Y < m)?( (graph[X][Y] == '.' or graph[X][Y] == 'B')? 1:0):0); 52 | } 53 | 54 | void bfs(int startX,int startY){ 55 | queue< pair >q; 56 | bool startTracking = 0; 57 | q.push({startX,startY}); 58 | while(!q.empty()) 59 | { 60 | int x= q.front().first; 61 | int y= q.front().second; 62 | q.pop(); 63 | 64 | rep(0,4) 65 | { 66 | if(isValid ( x+dx[i], y+dy[i] )) 67 | { 68 | if( graph[x+dx[i]][y+dy[i]] == 'B') startTracking = 1; 69 | switch(i) 70 | { 71 | case 0 : graph[x+dx[i]][y+dy[i]]= 'R'; break; 72 | case 1 : graph[x+dx[i]][y+dy[i]]= 'L'; break; 73 | case 2 : graph[x+dx[i]][y+dy[i]]= 'U'; break; 74 | case 3 : graph[x+dx[i]][y+dy[i]]= 'D'; break; 75 | 76 | } 77 | if(startTracking == 1) break; 78 | q.push({x+dx[i],y+dy[i]}); 79 | } 80 | 81 | } 82 | if(startTracking == 1){ 83 | track(); 84 | break; 85 | } 86 | } 87 | } 88 | 89 | void solve() 90 | { 91 | cin >> n >> m; 92 | rep(0,n) repj(j,0,m) cin >> graph[i][j]; 93 | rep(0,n) repj(j,0,m) { 94 | if(graph[i][j] == 'A') strtX = i, strtY = j; 95 | else if(graph[i][j] == 'B') endX = i, endY = j; 96 | } 97 | 98 | bfs(strtX,strtY); 99 | 100 | if(ans.size()){ 101 | reverse(ans.begin(),ans.end()); 102 | cout << "YES\n" << ans.size() << endl; 103 | cout << ans << endl; 104 | } 105 | else{ 106 | cout << "NO\n"; 107 | } 108 | } 109 | 110 | 111 | immortal_slave 112 | { 113 | khalid 114 | FastIO(); 115 | int t=1; 116 | 117 | while(t--){ 118 | solve(); 119 | } 120 | 121 | 122 | return 0; 123 | 124 | } 125 | 126 | -------------------------------------------------------------------------------- /Graph theory/Problems/Grid problems/CSES counting rooms.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef deque dq ; 6 | typedef list li; 7 | typedef vector vst ; 8 | typedef vector vll ; 9 | typedef deque::iterator dita; 10 | typedef double dd; 11 | typedef greater gt; 12 | typedef list::iterator liita; 13 | typedef vector::iterator vecita; 14 | #define immortal_slave int main() 15 | #define in(x) cin>>x; 16 | #define khalid std::ios::sync_with_stdio(false);cin.tie(NULL); 17 | #define mp make_pair 18 | #define out(x) cout<= b;--i) 32 | #define rep3j(j,a,b) for(ll j = a; j >= b;--j) 33 | #define ss stringstream 34 | #define st string 35 | #define min3(a,b,c) min( a,min( b, c)) 36 | #define min4(a,b,c,d) min( d,min3( a, b, c)) 37 | #define max3(a,b,c) max( a,max( b, c)) 38 | #define max4(a,b,c,d) max( d,max3(a,b,c)) 39 | #define sum(v) accumulate(v.begin(),v.end(),0ll) 40 | 41 | 42 | //Starting in the name of Allah(THE ALMIGHTY) 43 | void FastIO() 44 | { 45 | khalid 46 | #ifndef ONLINE_JUDGE 47 | freopen("in.txt","r",stdin); 48 | freopen("out.txt","w",stdout); 49 | #endif // ONLINE_JUDGE 50 | } 51 | int dx[]={1,-1,0,0}; 52 | int dy[]={0,0,1,-1}; 53 | 54 | st graph[1001]; int n,m; 55 | 56 | bool valid(int x, int y ) 57 | { 58 | if(x<0|| x>=n || y<0 || y>=m )return false; 59 | 60 | return true; 61 | } 62 | 63 | void dfs(int x,int y) 64 | { 65 | if(graph[x][y]=='#') return; 66 | 67 | graph[x][y]='#'; 68 | 69 | rep(0,4) 70 | if(valid(x+dx[i],y+dy[i])) 71 | { 72 | dfs((x+dx[i]),(y+dy[i])); 73 | } 74 | } 75 | 76 | void solve() 77 | { 78 | int cnt=0; 79 | in(n>>m) 80 | 81 | rep(0,n) in(graph[i]) 82 | 83 | 84 | rep(0,n) 85 | repj(j,0,m) 86 | if(graph[i][j]=='.') 87 | { 88 | ++cnt; dfs(i,j); 89 | } 90 | 91 | out(cnt) 92 | } 93 | 94 | immortal_slave 95 | { 96 | FastIO(); 97 | 98 | ll t=1; 99 | //in(t) 100 | 101 | while(t--){ 102 | solve(); 103 | } 104 | 105 | 106 | return 0; 107 | 108 | } -------------------------------------------------------------------------------- /Graph theory/Problems/Grid problems/CSES counting rooms[BFS].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef deque dq ; 6 | typedef list li; 7 | typedef vector vst ; 8 | typedef vector vll ; 9 | typedef deque::iterator dita; 10 | typedef double dd; 11 | typedef greater gt; 12 | typedef list::iterator liita; 13 | typedef vector::iterator vecita; 14 | #define immortal_slave int main() 15 | #define in(x) cin>>x; 16 | #define khalid std::ios::sync_with_stdio(false);cin.tie(NULL); 17 | #define mp make_pair 18 | #define out(x) cout<= b;--i) 32 | #define rep3j(j,a,b) for(ll j = a; j >= b;--j) 33 | #define ss stringstream 34 | #define st string 35 | #define min3(a,b,c) min( a,min( b, c)) 36 | #define min4(a,b,c,d) min( d,min3( a, b, c)) 37 | #define max3(a,b,c) max( a,max( b, c)) 38 | #define max4(a,b,c,d) max( d,max3(a,b,c)) 39 | #define sum(v) accumulate(v.begin(),v.end(),0ll) 40 | 41 | 42 | //Starting in the name of Allah(THE ALMIGHTY) 43 | void FastIO() 44 | { 45 | khalid 46 | #ifndef ONLINE_JUDGE 47 | freopen("in.txt","r",stdin); 48 | freopen("out.txt","w",stdout); 49 | #endif // ONLINE_JUDGE 50 | } 51 | int dx[]={1,-1,0,0}; 52 | int dy[]={0,0,1,-1}; 53 | 54 | st graph[1007]; int n,m; 55 | 56 | bool valid(int x, int y ) 57 | { 58 | return ((x < 0 or x >= n or y < 0 or y >= m )? 0:1); 59 | } 60 | 61 | void bfs(int a,int b) 62 | { 63 | queue> q; 64 | q.push({a,b}); 65 | 66 | 67 | while(!q.empty()){ 68 | int x = q.front().first , y = q.front().second; 69 | q.pop(); 70 | 71 | rep(0,4){ 72 | int X = x+dx[i], Y = y+dy[i]; 73 | if(valid(X,Y)) { 74 | if(graph[X][Y] == '.'){ 75 | q.push({X,Y}); 76 | graph[X][Y] = '#'; 77 | } 78 | } 79 | } 80 | } 81 | 82 | 83 | } 84 | 85 | void solve() 86 | { 87 | int cnt=0; 88 | in(n>>m) 89 | 90 | rep(0,n) in(graph[i]) 91 | 92 | 93 | rep(0,n) 94 | repj(j,0,m) 95 | if(graph[i][j]=='.') { 96 | ++cnt; bfs(i,j); 97 | } 98 | 99 | out(cnt) 100 | } 101 | 102 | immortal_slave 103 | { 104 | FastIO(); 105 | 106 | ll t=1; 107 | //in(t) 108 | 109 | while(t--){ 110 | solve(); 111 | } 112 | 113 | 114 | return 0; 115 | 116 | } -------------------------------------------------------------------------------- /Graph theory/Problems/Grid problems/UVA 572 Oil Deposits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef deque dq ; 6 | typedef list li; 7 | typedef vector vst ; 8 | typedef vector vll ; 9 | typedef deque::iterator dita; 10 | typedef double dd; 11 | typedef greater gt; 12 | typedef list::iterator liita; 13 | typedef vector::iterator vecita; 14 | #define immortal_slave int main() 15 | #define in(x) cin>>x; 16 | #define khalid std::ios::sync_with_stdio(false);cin.tie(NULL); 17 | #define mp make_pair 18 | #define out(x) cout<= b;--i) 32 | #define rep3j(j,a,b) for(ll j = a; j >= b;--j) 33 | #define ss stringstream 34 | #define st string 35 | #define min3(a,b,c) min( a,min( b, c)) 36 | #define min4(a,b,c,d) min( d,min3( a, b, c)) 37 | #define max3(a,b,c) max( a,max( b, c)) 38 | #define max4(a,b,c,d) max( d,max3(a,b,c)) 39 | #define sum(v) accumulate(v.begin(),v.end(),0ll) 40 | 41 | 42 | //Starting in the name of Allah(THE ALMIGHTY) 43 | void FastIO() 44 | { 45 | khalid 46 | #ifndef ONLINE_JUDGE 47 | freopen("in.txt","r",stdin); 48 | // freopen("out.txt","w",stdout); 49 | #endif // ONLINE_JUDGE 50 | } 51 | int dx[]={1,-1,0,0,1,-1,-1,1}; 52 | int dy[]={0,0,1,-1,1,1,-1,-1}; 53 | 54 | char graph[105][105]={}; 55 | int n,m; 56 | 57 | bool valid(int x, int y ) 58 | { 59 | return (x<0|| x>=n || y<0 || y>=m )? false : true; 60 | } 61 | 62 | void dfs(int x,int y) 63 | { 64 | if(graph[x][y]=='*') return; 65 | 66 | graph[x][y]='*'; 67 | 68 | rep(0,8) 69 | if(valid(x+dx[i],y+dy[i])) dfs((x+dx[i]),(y+dy[i])); 70 | } 71 | 72 | void solve() 73 | { 74 | int cnt=0; 75 | rep(0,n) repj(j,0,m) in(graph[i][j]) 76 | rep(0,n) 77 | repj(j,0,m) 78 | if(graph[i][j]=='@'){ 79 | ++cnt; dfs(i,j); 80 | } 81 | 82 | out(cnt) 83 | } 84 | 85 | immortal_slave 86 | { 87 | FastIO(); 88 | 89 | ll t=1; 90 | // in(t) 91 | 92 | while(1){ 93 | cin >> n >> m; 94 | if(!n and !m) break; 95 | solve(); 96 | } 97 | 98 | 99 | return 0; 100 | 101 | } -------------------------------------------------------------------------------- /Graph theory/Problems/Grid problems/UVa 11094.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef deque dq ; 6 | typedef list li; 7 | typedef vector vst ; 8 | typedef vector vll ; 9 | typedef deque::iterator dita; 10 | typedef double dd; 11 | typedef greater gt; 12 | typedef list::iterator liita; 13 | typedef vector::iterator vecita; 14 | #define immortal_slave int main() 15 | #define in(x) cin>>x; 16 | #define khalid std::ios::sync_with_stdio(false);cin.tie(NULL); 17 | #define mp make_pair 18 | #define out(x) cout<= b;--i) 32 | #define rep3j(j,a,b) for(ll j = a; j >= b;--j) 33 | #define ss stringstream 34 | #define st string 35 | #define min3(a,b,c) min( a,min( b, c)) 36 | #define min4(a,b,c,d) min( d,min3( a, b, c)) 37 | #define max3(a,b,c) max( a,max( b, c)) 38 | #define max4(a,b,c,d) max( d,max3(a,b,c)) 39 | #define sum(v) accumulate(v.begin(),v.end(),0ll) 40 | 41 | 42 | //Starting in the name of Allah(THE ALMIGHTY) 43 | void FastIO() 44 | { 45 | khalid 46 | #ifndef ONLINE_JUDGE 47 | freopen("in.txt","r",stdin); 48 | // freopen("out.txt","w",stdout); 49 | #endif // ONLINE_JUDGE 50 | } 51 | int dx[]={1,-1,0,0}; 52 | int dy[]={0,0,1,-1}; 53 | 54 | st graph[25]; 55 | int n,m; 56 | 57 | bool valid(int x, int y ) 58 | { 59 | return(x<0|| x>=n)? false:true; 60 | } 61 | 62 | int cnt = 0; 63 | bool vis[25][25]; 64 | char land; 65 | 66 | void CLEAR(){ 67 | memset(vis,0,sizeof(vis)); 68 | } 69 | 70 | void dfs(int x,int y) 71 | { 72 | if(vis[x][y] or graph[x][y] != land) return; 73 | vis[x][y] = 1; ++cnt; 74 | 75 | rep(0,4){ 76 | int X = x + dx[i] , Y = y + dy[i]; 77 | if(valid(X,Y)){ 78 | (Y < 0)? Y = m-1:(Y >= m)? Y = 0:0; 79 | dfs(X,Y); 80 | } 81 | } 82 | } 83 | 84 | void solve() 85 | { 86 | int mx = 0; 87 | rep(0,n) in(graph[i]) 88 | 89 | int a,b; cin >> a >> b; 90 | land = graph[a][b]; 91 | dfs(a,b); 92 | 93 | rep(0,n) 94 | repj(j,0,m) 95 | if(graph[i][j] == land) { 96 | cnt = 0; dfs(i,j); mx = max(mx,cnt); 97 | } 98 | 99 | out(mx) 100 | CLEAR(); 101 | } 102 | 103 | immortal_slave 104 | { 105 | FastIO(); 106 | 107 | ll t=1; 108 | //in(t) 109 | 110 | while(cin >> n >> m){ 111 | solve(); 112 | } 113 | 114 | return 0; 115 | 116 | } -------------------------------------------------------------------------------- /Graph theory/Problems/Grid problems/UVa 11953.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | typedef deque dq ; 6 | typedef list li; 7 | typedef vector vst ; 8 | typedef vector vll ; 9 | typedef deque::iterator dita; 10 | typedef double dd; 11 | typedef greater gt; 12 | typedef list::iterator liita; 13 | typedef vector::iterator vecita; 14 | #define immortal_slave int main() 15 | #define in(x) cin>>x; 16 | #define khalid std::ios::sync_with_stdio(false);cin.tie(NULL); 17 | #define mp make_pair 18 | #define out(x) cout<= b;--i) 32 | #define rep3j(j,a,b) for(ll j = a; j >= b;--j) 33 | #define ss stringstream 34 | #define st string 35 | #define min3(a,b,c) min( a,min( b, c)) 36 | #define min4(a,b,c,d) min( d,min3( a, b, c)) 37 | #define max3(a,b,c) max( a,max( b, c)) 38 | #define max4(a,b,c,d) max( d,max3(a,b,c)) 39 | #define sum(v) accumulate(v.begin(),v.end(),0ll) 40 | 41 | 42 | //Starting in the name of Allah(THE ALMIGHTY) 43 | void FastIO() 44 | { 45 | khalid 46 | #ifndef ONLINE_JUDGE 47 | freopen("in.txt","r",stdin); 48 | // freopen("out.txt","w",stdout); 49 | #endif // ONLINE_JUDGE 50 | } 51 | int dx[]={1,-1,0,0}; 52 | int dy[]={0,0,1,-1}; 53 | 54 | st graph[1001]; int n; 55 | 56 | bool valid(int x, int y ) 57 | { 58 | return(x<0|| x>=n || y<0 || y>=n )? false:true; 59 | } 60 | 61 | void dfs(int x,int y) 62 | { 63 | if(graph[x][y] == '.') return; 64 | graph[x][y]='.'; 65 | 66 | rep(0,4) 67 | if(valid(x+dx[i],y+dy[i])) { 68 | dfs((x+dx[i]),(y+dy[i])); 69 | } 70 | } 71 | 72 | void solve(int t) 73 | { 74 | int cnt=0; 75 | in(n) 76 | 77 | rep(0,n) in(graph[i]) 78 | 79 | rep(0,n) 80 | repj(j,0,n) 81 | if(graph[i][j]=='x') 82 | ++cnt, dfs(i,j); 83 | 84 | out("Case " << t << ": " << cnt) 85 | } 86 | 87 | immortal_slave 88 | { 89 | FastIO(); 90 | 91 | ll t=1; 92 | in(t) 93 | 94 | rep(1,t+1){ 95 | solve(i); 96 | } 97 | 98 | return 0; 99 | 100 | } -------------------------------------------------------------------------------- /Graph theory/Problems/Grid problems/UVa 871.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | 6 | void IO() 7 | { 8 | #ifndef ONLINE_JUDGE 9 | freopen( "in.txt" , "r" ,stdin); 10 | // freopen("out.txt", "w", stdout); 11 | // freopen("Error.txt","w",stderr); 12 | #endif // ONLINE_JUDGE 13 | } 14 | 15 | int dx[] = {1,-1,0,0,1,-1,1,-1}; 16 | int dy[] = {0,0,1,-1,1,-1,-1,1}; 17 | vector g; 18 | bool vis[50][50] = {}; 19 | int n , m , cnt = 0; 20 | 21 | bool isValid(int X,int Y){ 22 | return (X < 0 || X >= n || Y < 0 || Y >= m )? 0:1; 23 | } 24 | 25 | void dfs(int x,int y){ 26 | if(vis[x][y] ) return; 27 | vis[x][y] = 1; 28 | ++cnt; 29 | 30 | // cout << cnt << '\n'; 31 | 32 | rep(i,0,8){ 33 | if(isValid(x+dx[i],y+dy[i])){ 34 | if(g[x+dx[i]][y+dy[i]] == '1') 35 | dfs(x+dx[i],y+dy[i]); 36 | } 37 | } 38 | } 39 | 40 | int32_t main() 41 | { 42 | std::ios::sync_with_stdio(false);cin.tie(NULL); 43 | IO(); 44 | 45 | int t = 1; char s[30]; 46 | scanf("%d\n",&t); 47 | 48 | 49 | rep(q,1,t+1){ 50 | g.clear(); 51 | int mx = 0; 52 | while(gets(s)){ 53 | if(!s[0]) {break;} 54 | g.push_back(s); 55 | } 56 | 57 | n = g.size(); 58 | m = g[0].size(); 59 | 60 | rep(i,0,n) 61 | rep(j,0,m) 62 | if(!vis[i][j] and g[i][j] == '1') { 63 | cnt = 0; 64 | dfs(i,j); 65 | mx = max(mx,cnt); 66 | } 67 | 68 | cout << mx << '\n'; 69 | 70 | rep(i,0,50) rep(j,0,50) vis[i][j] = 0; 71 | 72 | if(q != t) cout << '\n'; 73 | } 74 | 75 | return 0; 76 | } 77 | 78 | -------------------------------------------------------------------------------- /Graph theory/Problems/LightOJ1094_Farthest Nodes in a Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | int ans; 8 | const int N = 30006; 9 | std::vector> graph[N]; 10 | bool visited[N]; 11 | int dist[N]; 12 | 13 | void bfs(int start){ 14 | queue> q; 15 | q.push({start,0}); 16 | visited[start] = 1; 17 | 18 | while(!q.empty()){ 19 | int currNode = q.front().first; 20 | int currDist = q.front().second; 21 | visited[currNode] = 1; 22 | q.pop(); 23 | 24 | for(auto [childnode,childDist]:graph[currNode]){ 25 | if(!visited[childnode]) 26 | { 27 | q.push({childnode,childDist + currDist}); 28 | if(currDist + childDist> ans) 29 | { 30 | ans = currDist + childDist; 31 | } 32 | dist[childnode] = childDist + currDist; 33 | } 34 | } 35 | } 36 | } 37 | 38 | void solve(int t){ 39 | ans = 0; 40 | int n; 41 | cin >> n; 42 | 43 | rep(i,0,n-1){ 44 | int u,v,wt; 45 | cin >> u >> v >> wt; 46 | graph[u].push_back({v,wt}); 47 | graph[v].push_back({u,wt}); 48 | } 49 | 50 | 51 | bfs(0); 52 | int start; 53 | rep(i,0,n){ 54 | if(dist[i] == ans){ 55 | start = i; break; 56 | } 57 | } 58 | ans = 0; 59 | rep(i,0,n) visited[i] = 0; 60 | bfs(start); 61 | 62 | cout << "Case " << t << ": " << ans << "\n"; 63 | } 64 | 65 | int32_t main() 66 | { 67 | 68 | 69 | int t = 1; 70 | cin >> t; 71 | 72 | rep(i,1,t+1){ 73 | rep(j,0,N) visited[j] = 0 , graph[j].clear() ,dist[j] = 0; 74 | solve(i); 75 | } 76 | return 0; 77 | } 78 | 79 | -------------------------------------------------------------------------------- /Graph theory/Problems/Topological sort/CSES Course Schedule.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | 9 | void IO() 10 | { 11 | #ifndef ONLINE_JUDGE 12 | freopen( "in.txt" , "r" ,stdin); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt","w",stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | // #define int long long int 18 | #define all(x) x.begin(),x.end() 19 | 20 | const int N = 1e5 + 7; 21 | vector ans; 22 | int n,m; 23 | int indeg[N] = {}; 24 | vector g[N]; 25 | bool vis[N]; 26 | 27 | void bfs(){ 28 | queue q; 29 | rep(i,1,n+1) if(indeg[i] == 0){ q.push(i); vis[i] = 1;} 30 | 31 | while(!q.empty()){ 32 | int parent = q.front(); 33 | ans.push_back(parent); q.pop(); 34 | 35 | for(auto child:g[parent]){ 36 | indeg[child]--; 37 | 38 | if(!indeg[child] and !vis[child]){ 39 | vis[child] = 1; q.push(child); 40 | } 41 | } 42 | } 43 | } 44 | 45 | void solve(int t){ 46 | cin >> n >> m; 47 | 48 | rep(i,0,m){ 49 | int u,v; cin >> u >> v; 50 | g[u].push_back(v); 51 | indeg[v]++; 52 | } 53 | 54 | bfs(); 55 | 56 | if(ans.size() == n){ for(auto x:ans) cout << x << ' ';} 57 | else cout << "IMPOSSIBLE\n"; 58 | } 59 | int32_t main() 60 | { 61 | std::ios::sync_with_stdio(false);cin.tie(NULL); 62 | IO(); 63 | int t = 1; 64 | // cin >> t; 65 | 66 | rep(i,1,t+1){ 67 | solve(i); 68 | } 69 | 70 | return 0; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Graph theory/Problems/Topological sort/UVA10305 OrderingTasks[BFS used].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | const int N = 105; 18 | vector g[N]; 19 | int indeg[N]; 20 | bool vis[N]; 21 | vector ans; 22 | 23 | void bfs(queue q){ 24 | while(!q.empty()){ 25 | int curr = q.front(); 26 | q.pop(); 27 | 28 | ans.push_back(curr); 29 | for(auto child:g[curr]){ 30 | indeg[child]--; 31 | if(indeg[child] == 0 and !vis[child]){ 32 | vis[child] = 1; 33 | q.push(child); 34 | } 35 | } 36 | } 37 | 38 | } 39 | void CLEAR(){ 40 | ans.clear(); 41 | rep(i,0,N) indeg[i] = 0 , g[i].clear() , vis[i] = 0; 42 | } 43 | 44 | void solve(int t){ 45 | int u,v,n,m; 46 | while(cin >> n >> m){ 47 | if(!n and !m) break; 48 | rep(i,0,m){ 49 | cin >> u >> v; 50 | g[u].push_back(v); 51 | indeg[v]++; 52 | } 53 | 54 | queue q; 55 | rep(i,1,n+1) if(!indeg[i]){ q.push(i); vis[i] = 1;} 56 | bfs(q); 57 | 58 | for(auto x:ans) cout << x << ' '; cout << '\n'; 59 | 60 | CLEAR(); 61 | } 62 | } 63 | 64 | int32_t main() 65 | { 66 | IO(); 67 | std::ios::sync_with_stdio(false);cin.tie(NULL); 68 | 69 | int t = 1; 70 | // cin >> t; 71 | 72 | rep(i,1,t+1){ 73 | solve(i); 74 | } 75 | return 0; 76 | } 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Graph theory/Problems/Topological sort/UVa 10305 order Tasks[DFS used].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | 6 | void IO() 7 | { 8 | std::ios::sync_with_stdio(false);cin.tie(NULL); 9 | #ifndef ONLINE_JUDGE 10 | freopen( "in.txt" , "r" ,stdin); 11 | // freopen("out.txt", "w", stdout); 12 | freopen("Error.txt","w",stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | const int N = 109; 17 | int n , m; 18 | 19 | std::vector graph[N]; 20 | bool vis[N]; 21 | 22 | stack ans; 23 | 24 | void topsort(int source){ 25 | vis[source] = 1; 26 | 27 | for(auto child:graph[source]){ 28 | if(!vis[child]) topsort(child); 29 | } 30 | 31 | ans.push(source); // when all the childs of a node have been visited then we will push it into the stack 32 | } 33 | void CLEAR(){ rep(i,1,n+1) graph[i].clear() , vis[i] = 0;} 34 | 35 | 36 | void solve(int n , int m){ 37 | 38 | rep(i,0,m){ 39 | int u , v; 40 | cin >> u >> v; 41 | graph[u].push_back(v); // directed graph 42 | } 43 | 44 | rep(i,1,n+1) if(!vis[i]) topsort(i); 45 | 46 | while(!ans.empty()){ 47 | if(ans.size() > 1) cout << ans.top() << ' '; 48 | else cout << ans.top() << '\n'; 49 | ans.pop(); 50 | } 51 | 52 | CLEAR(); 53 | } 54 | 55 | int32_t main() 56 | { 57 | IO(); 58 | int t = 1; 59 | // cin >> t; 60 | 61 | while(cin >> n >> m){ 62 | if(!n and !m) break; 63 | solve(n , m); 64 | } 65 | return 0; 66 | } 67 | 68 | -------------------------------------------------------------------------------- /Graph theory/Problems/Topological sort/UVa 11504 dominos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | 6 | void IO() 7 | { 8 | std::ios::sync_with_stdio(false);cin.tie(NULL); 9 | #ifndef ONLINE_JUDGE 10 | freopen( "in.txt" , "r" ,stdin); 11 | // freopen("out.txt", "w", stdout); 12 | freopen("Error.txt","w",stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | const int N = 1e5 + 7; 17 | 18 | vector graph[N]; 19 | bool vis[N]; 20 | 21 | stack ans; 22 | int n,m; 23 | 24 | void topsort(int source){ 25 | vis[source] = 1; 26 | 27 | for(auto child:graph[source]){ 28 | if(!vis[child]) topsort(child); 29 | } 30 | 31 | ans.push(source); // when all the childs of a node have been visited then we will push it into the stack 32 | } 33 | 34 | void dfs(int source){ 35 | vis[source] = 1; 36 | 37 | for(auto child:graph[source]){ 38 | if(!vis[child]) dfs(child); 39 | } 40 | } 41 | void CLEAR(){ rep(i,1,n+1) graph[i].clear() , vis[i] = 0;} 42 | 43 | void solve(int t){ 44 | cin >> n >> m; 45 | 46 | rep(i,0,m){ 47 | int u , v; 48 | cin >> u >> v; 49 | graph[u].push_back(v); // directed graph 50 | } 51 | 52 | rep(i,1,n+1) if(!vis[i]) topsort(i); 53 | rep(i,1,n+1) vis[i] = 0; 54 | 55 | int knock = 0; 56 | 57 | while(!ans.empty()){ 58 | int source = ans.top(); ans.pop(); 59 | if(!vis[source]) {dfs(source); ++knock;} 60 | } 61 | 62 | cout << knock << '\n'; 63 | 64 | CLEAR(); 65 | } 66 | 67 | int32_t main() 68 | { 69 | IO(); 70 | int t = 1; 71 | cin >> t; 72 | 73 | rep(i,1,t+1){ 74 | solve(i); 75 | } 76 | return 0; 77 | } 78 | 79 | -------------------------------------------------------------------------------- /Graph theory/Problems/Topological sort/UVa 872 ordering.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAXN = 100; 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | 7 | 8 | vector adj[MAXN]; 9 | int inDegree[MAXN]; 10 | bool visited[MAXN]; 11 | vector order; 12 | 13 | vector> answer; 14 | set inputs; 15 | int n; 16 | string s1,s2; 17 | 18 | 19 | void CLEAR(){ 20 | for(auto x:adj) x.clear(); 21 | for(auto x:inDegree) x = 0; 22 | for(auto x:visited) x = 0; 23 | order.clear(); 24 | answer.clear(); 25 | inputs.clear(); 26 | s1.clear(); s2.clear(); 27 | } 28 | 29 | void dfs(int depth) { 30 | if(depth == n) { 31 | answer.push_back(order); 32 | return; 33 | } 34 | for(int u : inputs) { 35 | if(!visited[u] && inDegree[u] == 0) { 36 | visited[u] = true; 37 | order.push_back(u); 38 | 39 | 40 | for(auto v:adj[u]) inDegree[v]--; 41 | 42 | dfs(depth+1); 43 | 44 | for(auto v:adj[u]) inDegree[v]++; 45 | 46 | 47 | order.pop_back(); 48 | visited[u] = false; 49 | } 50 | } 51 | } 52 | 53 | void topologicalSort() { 54 | dfs(0); 55 | 56 | if(!answer.size()){ 57 | cout << "NO\n"; return; 58 | } 59 | for(auto x:answer){ 60 | for(int i = 0 ; i < x.size() ; i++){ char ch = x[i] + 'A'; cout << ch << (i == x.size() - 1 ? "\n":" "); } 61 | } 62 | } 63 | void solve(){ 64 | CLEAR(); 65 | getline(cin >> ws,s1); 66 | getline(cin >> ws,s2); 67 | 68 | for(auto x:s1) if(!isspace(x)) inputs.insert(x - 'A'); 69 | n = inputs.size(); 70 | 71 | 72 | for(int i = 0 ; i < s2.size(); i++) { 73 | if(s2[i] == '<'){ 74 | adj[s2[i-1] - 'A'].push_back(s2[i+1] - 'A'); 75 | inDegree[s2[i+1] - 'A']++; 76 | } 77 | } 78 | 79 | // for(auto x:adj) 80 | // for(auto y:inputs) cout << y << ' '; 81 | topologicalSort(); 82 | } 83 | 84 | int main() { 85 | std::ios::sync_with_stdio(false);cin.tie(NULL); 86 | #ifndef ONLINE_JUDGE 87 | freopen( "in.txt" , "r" ,stdin); 88 | freopen("out.txt", "w", stdout); 89 | // freopen("Error.txt","w",stderr); 90 | #endif // ONLINE_JUDGE 91 | 92 | int t ; cin >> t; 93 | 94 | rep(i,0,t){ 95 | solve(); 96 | if(i != t-1) cout << "\n"; 97 | } 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /Graph theory/Problems/atcoder 54C One-stroke Path.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int MAXN = 105; 7 | 8 | vector adj[MAXN]; 9 | int inDegree[MAXN]; 10 | bool visited[MAXN]; 11 | vector order; 12 | int n, m; 13 | vector> answer; 14 | 15 | 16 | void dfs(int par,int depth) { 17 | visited[par] = true; 18 | order.push_back(par); 19 | 20 | if(depth == n) { 21 | answer.push_back(order); 22 | return; 23 | } 24 | 25 | for(int child:adj[par]) 26 | if(!visited[child]) { 27 | 28 | dfs(child,depth+1); 29 | 30 | order.pop_back(); 31 | visited[child] = false; 32 | } 33 | 34 | 35 | } 36 | 37 | void solve(){ 38 | 39 | cin >> n >> m; 40 | 41 | for (int i = 0; i < m; ++i) 42 | { 43 | int u,v; 44 | cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); 45 | } 46 | 47 | dfs(1,1); 48 | cout << answer.size() << '\n'; 49 | } 50 | 51 | int main() { 52 | #ifndef ONLINE_JUDGE 53 | freopen( "in.txt" , "r" ,stdin); 54 | // freopen("out.txt", "w", stdout); 55 | // freopen("Error.txt","w",stderr); 56 | #endif // ONLINE_JUDGE 57 | 58 | 59 | solve(); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Graph theory/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/banner gif.gif -------------------------------------------------------------------------------- /Graph theory/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/codejam.png -------------------------------------------------------------------------------- /Graph theory/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Graph theory/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Graph theory/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/cses.PNG -------------------------------------------------------------------------------- /Graph theory/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/cses2.jpg -------------------------------------------------------------------------------- /Graph theory/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/icpc.jpeg -------------------------------------------------------------------------------- /Graph theory/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/icpc.jpg -------------------------------------------------------------------------------- /Graph theory/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/icpc.png -------------------------------------------------------------------------------- /Graph theory/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/images.png -------------------------------------------------------------------------------- /Graph theory/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/mix.png -------------------------------------------------------------------------------- /Graph theory/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/mix2.jpg -------------------------------------------------------------------------------- /Graph theory/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Graph theory/assets/my logo.jpeg -------------------------------------------------------------------------------- /Mathematics/Basic maths concepts/Multiplication of two large numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Multiply two large numbers represented as vectors of digits. 5 | // The digits are stored in reverse order (i.e., the least significant 6 | // digit is at the front of the vector). 7 | std::vector multiply(const std::vector& a, const std::vector& b) { 8 | // Result will have length a.size() + b.size() 9 | std::vector result(a.size() + b.size(), 0); 10 | 11 | for (size_t i = 0; i < a.size(); ++i) { 12 | for (size_t j = 0; j < b.size(); ++j) { 13 | result[i + j] += a[i] * b[j]; 14 | result[i + j + 1] += result[i + j] / 10; 15 | result[i + j] %= 10; 16 | } 17 | } 18 | 19 | // Remove leading zeros 20 | while (result.size() > 1 && result.back() == 0) { 21 | result.pop_back(); 22 | } 23 | 24 | return result; 25 | } 26 | 27 | int main() { 28 | // Multiply 12345 and 67890 29 | std::vector a = {5, 4, 3, 2, 1}; 30 | std::vector b = {0, 9, 8, 7, 6}; 31 | std::vector c = multiply(a, b); 32 | 33 | // Print 838102050 34 | for (int x : c) { 35 | std::cout << x; 36 | } 37 | std::cout << std::endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Mathematics/Basic maths concepts/Quotient and remainder of a very large number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | int32_t main() 9 | { 10 | 11 | string a; 12 | int b , res = 0; 13 | cin >> a >> b; 14 | 15 | std::vector div; 16 | 17 | rep(i,0,a.size()){ 18 | int d = (res * 10 + (a[i] - '0'))/b; 19 | if(d) div.push_back(d); 20 | res = (res * 10 + (a[i] - '0')) % b; 21 | } 22 | 23 | for(auto x:div) cout << x ; cout << endl; // Quoitient 24 | cout << res << '\n'; // Remainder 25 | 26 | return 0; 27 | } 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Mathematics/Problems/CodeForces 1370A Maximum GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | const int N = 5e5 + 500; 18 | const int MX = 1e6; 19 | 20 | void solve(int t){ 21 | int n; 22 | cin >> n; 23 | cout << n/2 << endl; 24 | } 25 | 26 | int32_t main() 27 | { 28 | IO(); 29 | std::ios::sync_with_stdio(false);cin.tie(NULL); 30 | 31 | int t = 1; 32 | cin >> t; 33 | 34 | rep(i,1,t+1){ 35 | solve(i); 36 | } 37 | return 0; 38 | } 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Mathematics/Problems/Di-visible Confusion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | const int N = 5e5 + 500; 18 | const int MX = 1e6; 19 | 20 | void solve(int t){ 21 | int n; 22 | cin >> n; 23 | int a[n + 1]; 24 | rep(i,1,n + 1) cin >> a[i]; 25 | 26 | bool OK = 1; 27 | rep(i,1,n + 1){ 28 | bool ok = 0; 29 | for (int j = i + 1; j >= 2; --j) 30 | { 31 | if(a[i] % j){ 32 | ok = 1; break; 33 | } 34 | } 35 | if(!ok){ 36 | OK = 0; 37 | break; 38 | } 39 | } 40 | 41 | cout << (OK?"YES\n":"NO\n"); 42 | } 43 | 44 | int32_t main() 45 | { 46 | IO(); 47 | std::ios::sync_with_stdio(false);cin.tie(NULL); 48 | 49 | int t = 1; 50 | cin >> t; 51 | 52 | rep(i,1,t+1){ 53 | solve(i); 54 | } 55 | return 0; 56 | } 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Mathematics/Problems/Divisors 2 SPOJ - DIV2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | const int N = 1e6 + 7; 18 | const int MX = 1e6; 19 | 20 | int divisors[N]; 21 | vector res(N,1); 22 | vector ans; 23 | void check(){ 24 | rep(i,1,N){ 25 | for (int j = i; j < N; j+=i) 26 | { 27 | divisors[j]++; 28 | } 29 | } 30 | 31 | rep(i,1,N){ 32 | for (int j = i; j < N; j+=i) 33 | { 34 | if(divisors[j] < 4 or (divisors[j] % divisors[i])) 35 | res[j] = 0; 36 | } 37 | } 38 | 39 | int cnt = 0; 40 | rep(i,1,N){ 41 | if(res[i]){ 42 | ++cnt; 43 | if(cnt % 108 == 0 and cnt > 0) 44 | ans.push_back(i); 45 | } 46 | } 47 | 48 | } 49 | 50 | void solve(int t){ 51 | int n = ans.size() ; 52 | rep(i,0,n) cout << ans[i] << "\n"; 53 | } 54 | 55 | int32_t main() 56 | { 57 | IO(); 58 | std::ios::sync_with_stdio(false);cin.tie(NULL); 59 | 60 | check(); 61 | 62 | int t = 1; 63 | // cin >> t; 64 | 65 | 66 | rep(i,1,t+1){ 67 | solve(i); 68 | } 69 | return 0; 70 | } 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Mathematics/Problems/Fadi and LCM CodeForces - 1285C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | const int N = 5e5 + 500; 18 | const int MX = 1e6; 19 | 20 | pair solve(int n){ 21 | pair ans;; 22 | for(int i = 1 ; i * i <= n; i++) 23 | if(n % i == 0 and ( (i*(n/i)/__gcd(i,n/i)) == n)) ans = {i , n/i}; 24 | 25 | return ans; 26 | } 27 | 28 | int32_t main() 29 | { 30 | IO(); 31 | std::ios::sync_with_stdio(false);cin.tie(NULL); 32 | 33 | int t = 1; 34 | // cin >> t; 35 | 36 | rep(i,1,t+1){ 37 | int n; 38 | cin >> n; 39 | pair result; 40 | result = solve(n); 41 | cout << result.first << " " << result.second << "\n"; 42 | } 43 | return 0; 44 | } 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Mathematics/Problems/IP Checking LightOJ - 1354.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | const int N = 1e6 + 1; 16 | 17 | void solve(int t){ 18 | string s , s1; 19 | cin >> s >> s1; 20 | // cout << s << '\n'; 21 | 22 | int a[4],pos = 0,b[4]; 23 | int n = s.size(); 24 | stringstream ss; 25 | string temp; 26 | 27 | rep(i,0,n){ 28 | if(s[i] == '.' or i == n-1){ 29 | if(i == n-1) temp += s[i]; 30 | 31 | ss << temp; 32 | ss >> a[pos]; 33 | 34 | ++pos; 35 | ss.clear(); temp.clear(); 36 | } 37 | else{ 38 | temp += s[i]; 39 | } 40 | 41 | } 42 | 43 | n = s1.size() , pos = 0; 44 | rep(i,0,n){ 45 | if(s1[i] == '.' or i == n-1){ 46 | if(i == n-1) temp += s1[i]; 47 | int x = 0; 48 | rep(j,0,8){ 49 | x += (temp[j]-'0') * (1 << (7 - j)); 50 | } 51 | 52 | b[pos] = x; 53 | ++pos; 54 | ss.clear(); temp.clear(); 55 | } 56 | else{ 57 | temp += s1[i]; 58 | } 59 | 60 | } 61 | 62 | bool ok = 1; 63 | rep(i,0,4) if(a[i] != b[i]) ok = 0; 64 | 65 | cout << "Case " << t << ": " ; 66 | 67 | cout << (ok? "Yes\n":"No\n"); 68 | 69 | } 70 | 71 | int32_t main() 72 | { 73 | IO(); 74 | std::ios::sync_with_stdio(false);cin.tie(NULL); 75 | 76 | int t = 1; 77 | cin >> t; 78 | 79 | rep(i,1,t+1){ 80 | solve(i); 81 | } 82 | return 0; 83 | } 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /Mathematics/Problems/Positive Negative Sign LightOJ - 1294.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | 7 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 8 | #define endl "\n" 9 | 10 | void IO(){ 11 | #ifndef ONLINE_JUDGE 12 | freopen("in.txt", "r", stdin ); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt", "w", stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | const int N = 1e6 + 1; 18 | 19 | void solve(int t){ 20 | int n,m; 21 | cin >> n >> m; 22 | 23 | /* 24 | brute force approach 25 | using for loop and assign + - sign 26 | according to the problem's ask then 27 | sum it up. 28 | */ 29 | 30 | // n = 12 m = 3 31 | // -1 - 2 - 3 + 4 + 5 + 6 - 7 - 8 - 9 + 10 + 11 + 12 32 | // (4-1) + (5-2) + (6-3) + (10-7) + (11 - 8) + (12 - 9) 33 | // 6 pairs and each of them have 3 as sum . ans = 3 * 6 34 | 35 | int pairs = n / 2; 36 | int sum_of_each_pair = m; 37 | int ans = sum_of_each_pair * pairs; 38 | cout << "Case " << t << ": " << ans << "\n"; 39 | } 40 | 41 | int32_t main() 42 | { 43 | IO(); 44 | std::ios::sync_with_stdio(false);cin.tie(NULL); 45 | 46 | int t = 1; 47 | cin >> t; 48 | 49 | rep(i,1,t+1){ 50 | solve(i); 51 | } 52 | return 0; 53 | } 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /Mathematics/Problems/Power Sequence CodeForces - 1397B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | const int N = 5e5 + 500; 18 | const int MX = 1e6; 19 | 20 | void solve(int t){ 21 | int n; 22 | cin >> n; 23 | 24 | int a[n] , c = 1; 25 | rep(i,0,n) cin >> a[i]; 26 | sort(a,a+n); 27 | int mn = 2e18; 28 | while(1){ 29 | 30 | int check = pow(c,n-1); 31 | if(check < 0) break; // checks overflow 32 | 33 | int cst = 0; 34 | rep(i,0,n){ 35 | cst += abs(a[i] - pow(c,i)); 36 | 37 | } 38 | 39 | if(cst < mn) mn = cst; 40 | else break; 41 | 42 | ++c; 43 | } 44 | 45 | cout << mn << '\n'; 46 | } 47 | 48 | int32_t main() 49 | { 50 | IO(); 51 | std::ios::sync_with_stdio(false);cin.tie(NULL); 52 | 53 | int t = 1; 54 | // cin >> t; 55 | 56 | rep(i,1,t+1){ 57 | solve(i); 58 | } 59 | return 0; 60 | } 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Mathematics/Problems/Swords CodeForces - 1216D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | const int N = 5e5 + 500; 18 | const int MX = 1e6; 19 | 20 | void solve(int t){ 21 | int n; 22 | cin >> n; 23 | 24 | int a[n] , gc = 0 , mx = 0; 25 | rep(i,0,n) { 26 | cin >> a[i]; 27 | mx = max(mx,a[i]); 28 | } 29 | 30 | int diff = 0; 31 | rep(i,0,n){ 32 | diff += mx - a[i]; 33 | gc = __gcd(gc,mx - a[i]); 34 | } 35 | int res = diff / gc; 36 | 37 | cout << res << ' ' << gc << '\n'; 38 | } 39 | 40 | int32_t main() 41 | { 42 | IO(); 43 | std::ios::sync_with_stdio(false);cin.tie(NULL); 44 | 45 | int t = 1; 46 | // cin >> t; 47 | 48 | rep(i,1,t+1){ 49 | solve(i); 50 | } 51 | return 0; 52 | } 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Mathematics/README.md: -------------------------------------------------------------------------------- 1 | 2 |

Concepts

3 | 4 | 1. [Multiplication of two large numbers](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Basic%20maths%20concepts/Multiplication%20of%20two%20large%20numbers.cpp) 5 | 1. [Quotient and remainder of a very large number](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Basic%20maths%20concepts/Quotient%20and%20remainder%20of%20a%20very%20large%20number.cpp) 6 | 7 | 8 |

Problems

9 | 10 | 1. [CodeForces 1370A Maximum GCD](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/CodeForces%201370A%20Maximum%20GCD.cpp) 11 | 1. [CodeForces 987C_Three displays](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/CodeForces%20987C_Three%20displays.cpp) 12 | 1. [Di-visible Confusion](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/Di-visible%20Confusion.cpp) 13 | 1. [Divisors 2 SPOJ - DIV2](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/Divisors%202%20SPOJ%20-%20DIV2.cpp) 14 | 1. [Fadi and LCM CodeForces - 1285C](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/Fadi%20and%20LCM%20CodeForces%20-%201285C.cpp) 15 | 1. [HackerRank constructing-a-number_Constructing a Number](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/HackerRank%20constructing-a-number_Constructing%20a%20Number.cpp) 16 | 1. [IP Checking LightOJ - 1354](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/IP%20Checking%20LightOJ%20-%201354.cpp) 17 | 1. [Positive Negative Sign LightOJ - 1294](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/Positive%20Negative%20Sign%20LightOJ%20-%201294.cpp) 18 | 1. [Power Sequence CodeForces - 1397B](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/Power%20Sequence%20CodeForces%20-%201397B.cpp) 19 | 1. [Swords CodeForces - 1216D](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Math%20concepts%20%26%20problems/Problems/Swords%20CodeForces%20-%201216D.cpp) 20 | -------------------------------------------------------------------------------- /Mathematics/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/banner gif.gif -------------------------------------------------------------------------------- /Mathematics/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/codejam.png -------------------------------------------------------------------------------- /Mathematics/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Mathematics/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Mathematics/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/cses.PNG -------------------------------------------------------------------------------- /Mathematics/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/cses2.jpg -------------------------------------------------------------------------------- /Mathematics/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/icpc.jpeg -------------------------------------------------------------------------------- /Mathematics/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/icpc.jpg -------------------------------------------------------------------------------- /Mathematics/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/icpc.png -------------------------------------------------------------------------------- /Mathematics/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/images.png -------------------------------------------------------------------------------- /Mathematics/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/mix.png -------------------------------------------------------------------------------- /Mathematics/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/mix2.jpg -------------------------------------------------------------------------------- /Mathematics/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Mathematics/assets/my logo.jpeg -------------------------------------------------------------------------------- /Number Theory/Basic Number theory concepts/BigMod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define rev(i,a,b) for(int i = a ; i >= b ; i--) 8 | 9 | #define endl '\n' 10 | #define lcm(a,b) (a*b)/(__gcd(a,b)) 11 | 12 | void IO() 13 | { 14 | std::ios::sync_with_stdio(false);cin.tie(NULL); 15 | #ifndef ONLINE_JUDGE 16 | freopen( "in.txt" , "r" ,stdin); 17 | // freopen("out.txt", "w", stdout); 18 | freopen("Error.txt","w",stderr); 19 | #endif // ONLINE_JUDGE 20 | } 21 | // #define int long long int 22 | 23 | #define all(x) x.begin(),x.end() 24 | const int N = 1e7 + 9; 25 | 26 | int BigMod(int B, int P , int M){ 27 | if(!P) return 1; 28 | if(P % 2){ 29 | int a = B % M; 30 | int b = BigMod(B,P-1,M); 31 | 32 | return (a * b) % M; 33 | } 34 | else{ 35 | int a = BigMod(B,P/2,M); 36 | return (a * a) % M; 37 | } 38 | } 39 | int32_t main() 40 | { 41 | IO(); 42 | int B,P,M; 43 | 44 | while(cin >> B >> P >> M){ 45 | cout << BigMod(B,P,M); 46 | cout << '\n'; 47 | } 48 | 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Number Theory/Basic Number theory concepts/Number of Divisors of N using prime factorization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const long long m = 1000010; 5 | 6 | void FastIO(){ 7 | #ifndef ONLINE_JUDGE 8 | freopen("in.txt", "r", stdin ); 9 | // freopen("out.txt", "w", stdout); 10 | // freopen("Error.txt", "w", stderr); 11 | #endif // ONLINE_JUDGE 12 | } 13 | 14 | std::vector primes; 15 | vector siv(m,1); 16 | void sieve() 17 | { 18 | 19 | for (long long i=2; i * i <= m; i++){ 20 | if(siv[i]) 21 | for (long long j=i*i; j <= m; j+=i) 22 | siv[j]=0; 23 | } 24 | 25 | for (int i=2; i 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 1e7 + 7; 9 | const int MX = 1e6; 10 | std::vector graph[N]; 11 | vector lvl(N, -1); 12 | 13 | void FastIO(){ 14 | #ifndef ONLINE_JUDGE 15 | freopen("in.txt", "r", stdin ); 16 | // freopen("out.txt", "w", stdout); 17 | // freopen("Error.txt", "w", stderr); 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | #define int long long 22 | 23 | // Modified sieve used to determine smallest prime factor 24 | 25 | int spf[N]; 26 | void SPF(){ 27 | rep(i,1,N) spf[i] = i; 28 | 29 | for(int i = 2; i * i <= N ; i++) 30 | for(int j = i * i ; j <= N ; j+=i) 31 | spf[j] = min(spf[j] , i); 32 | } 33 | int lpf[10000000 + 7]; 34 | void solve(int t){ 35 | int n; 36 | cin >> n; 37 | int num = n; 38 | 39 | vector ans; 40 | while(n > 1){ 41 | ans.push_back(spf[n]); 42 | n /= spf[n]; 43 | } 44 | 45 | lpf[num] = ans[ans.size() - 1]; 46 | 47 | cout << num << " : " ; 48 | for(auto x:ans) cout << x << ' '; 49 | cout << "\n"; 50 | 51 | cout << "lpf of n is : " << lpf[num]; 52 | 53 | } 54 | 55 | int32_t main() 56 | { 57 | FastIO(); 58 | SPF(); 59 | int t = 1; 60 | // cin >> t; 61 | 62 | rep(i,1,t+1){ 63 | solve(i); 64 | } 65 | return 0; 66 | } 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /Number Theory/Problems/A Missing Factor Gym - 310911K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void FastIO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | 17 | 18 | void solve(int t){ 19 | int n , ans = 1; 20 | cin >> n; 21 | 22 | int largest = (int) sqrt(n); 23 | for (int i = 2; i * i <= largest; ++i) 24 | { 25 | while(largest % i == 0){ 26 | ans = i; 27 | largest/=i; 28 | } 29 | } 30 | 31 | ans = max(ans,largest); 32 | cout << ans << "\n"; 33 | } 34 | 35 | int32_t main() 36 | { 37 | FastIO(); 38 | int t = 1; 39 | // cin >> t; 40 | 41 | rep(i,1,t+1){ 42 | solve(i); 43 | } 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Number Theory/Problems/Again Prime No Time. UVA - 10780.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | const int N = 1e5 + 7; 17 | int spf[N]; 18 | vector ans[N]; 19 | void SPF(){ 20 | 21 | rep(i,1,N) spf[i] = i; 22 | 23 | for(int i = 2; i * i <= N ; i++) 24 | for(int j = i * i ; j <= N ; j+=i) 25 | spf[j] = min(spf[j] , i); 26 | } 27 | int divOfN [N] , divOfM [N] ; 28 | void solve(int t){ 29 | int n,m,mn = 1e18; 30 | cin >> m >> n; 31 | 32 | rep(i,0,N) divOfN[i] = 0 , divOfM[i] = 0; 33 | 34 | rep(i,2,n+1){ 35 | for(auto x:ans[i]){ 36 | ++divOfN[x]; 37 | } 38 | } 39 | 40 | for(auto x:ans[m]){ 41 | ++divOfM[x]; 42 | } 43 | 44 | cout << "Case " << t << ":" << "\n"; 45 | rep(i,2,m + 1){ 46 | if(divOfM[i] > divOfN[i]){ 47 | cout << "Impossible to divide\n"; 48 | return; 49 | } 50 | if(divOfM[i]) mn = min(mn,divOfN[i] / divOfM[i]); 51 | } 52 | 53 | cout << mn << "\n"; 54 | } 55 | 56 | int32_t main() 57 | { 58 | IO(); 59 | std::ios::sync_with_stdio(false);cin.tie(NULL); 60 | SPF(); 61 | 62 | rep(i,2,N){ 63 | int n = i; 64 | while(n > 1){ 65 | ans[i].push_back(spf[n]); 66 | n /= spf[n]; 67 | } 68 | } 69 | 70 | int t = 1; 71 | 72 | cin >> t; 73 | 74 | rep(i,1,t+1){ 75 | solve(i); 76 | } 77 | return 0; 78 | } 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /Number Theory/Problems/CF 1051B Relatively Prime Pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 107; 9 | const int MX = 1e6; 10 | std::vector graph[N]; 11 | vector lvl(N, -1); 12 | 13 | void FastIO(){ 14 | #ifndef ONLINE_JUDGE 15 | freopen("in.txt", "r", stdin ); 16 | // freopen("out.txt", "w", stdout); 17 | // freopen("Error.txt", "w", stderr); 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | #define int long long 22 | 23 | void solve(int t){ 24 | int l,r; 25 | cin >> l >> r; 26 | 27 | cout << "YES\n"; 28 | 29 | for(int i = l ; l < r ; l +=2){ 30 | cout << l << ' ' << l + 1 << endl; 31 | } 32 | } 33 | 34 | int32_t main() 35 | { 36 | FastIO(); 37 | 38 | int t = 1; 39 | // cin >> t; 40 | 41 | rep(i,1,t+1){ 42 | solve(i); 43 | } 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Number Theory/Problems/CF Gym - 310911G.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | 7 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 8 | #define endl "\n" 9 | 10 | void IO(){ 11 | #ifndef ONLINE_JUDGE 12 | freopen("in.txt", "r", stdin ); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt", "w", stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | const int N = 1e6 + 1; 18 | 19 | 20 | void solve(int t){ 21 | int n; 22 | cin >> n; 23 | vector v(N,0),ans(N,0); 24 | 25 | rep(i,0,n){ 26 | int a; 27 | cin >> a; 28 | v[a]++; 29 | } 30 | 31 | rep(i,1,N){ 32 | for(int j = i ; j < N ; j += i) 33 | ans[i] += v[j]; 34 | } 35 | 36 | rep(i,1,N) cout << ans[i] << "\n"; 37 | } 38 | 39 | int32_t main() 40 | { 41 | IO(); 42 | std::ios::sync_with_stdio(false);cin.tie(NULL); 43 | 44 | int t = 1; 45 | // cin >> t; 46 | 47 | rep(i,1,t+1){ 48 | solve(i); 49 | } 50 | return 0; 51 | } 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Number Theory/Problems/CSES common divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | #define endl '\n' 6 | void IO() 7 | { 8 | #ifndef ONLINE_JUDGE 9 | freopen( "in.txt" , "r" ,stdin); 10 | // freopen("out.txt", "w", stdout); 11 | // freopen("Error.txt","w",stderr); 12 | #endif // ONLINE_JUDGE 13 | } 14 | // #define int long long int 15 | #define all(x) x.begin(),x.end() 16 | 17 | const int N = 1e6 + 7; 18 | int divs[N]; 19 | 20 | void solve(){ 21 | int n , ans = 1; cin >> n; 22 | int a[n]; 23 | rep(i,0,n)cin >> a[i]; 24 | 25 | rep(i,0,n){ 26 | for (int j = 1; j*j <= a[i]; ++j) 27 | { 28 | if(a[i] % j == 0){ 29 | divs[j]++; 30 | if(divs[j] > 1) 31 | ans = max(ans,j); 32 | 33 | if(j != a[i]/j){ 34 | divs[a[i]/j]++; 35 | 36 | if(divs[a[i]/j]>1) 37 | ans = max(ans,a[i]/j); 38 | } 39 | } 40 | } 41 | } 42 | 43 | cout << ans << '\n'; 44 | } 45 | 46 | int32_t main() 47 | { 48 | ios_base::sync_with_stdio(0); 49 | cin.tie(0); 50 | 51 | IO(); 52 | 53 | solve(); 54 | 55 | return 0; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Number Theory/Problems/Chef and Prime Divisors CodeChef - CHAPD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | void solve(int t){ 17 | int a,b; 18 | cin >> a >> b; 19 | 20 | int gc = __gcd(a,b); 21 | 22 | while(b > 1 and gc > 1){ 23 | b /= gc; 24 | gc = __gcd(a,b); 25 | } 26 | 27 | cout << (b > 1 ? "No\n":"Yes\n"); 28 | } 29 | 30 | int32_t main() 31 | { 32 | IO(); 33 | std::ios::sync_with_stdio(false);cin.tie(NULL); 34 | 35 | int t = 1; 36 | 37 | cin >> t; 38 | 39 | rep(i,1,t+1){ 40 | solve(i); 41 | } 42 | return 0; 43 | } 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Number Theory/Problems/Codeforces Gym - 310911H .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 107; 9 | const int MX = 1e6; 10 | std::vector graph[N]; 11 | vector lvl(N, -1); 12 | 13 | void FastIO(){ 14 | #ifndef ONLINE_JUDGE 15 | freopen("in.txt", "r", stdin ); 16 | // freopen("out.txt", "w", stdout); 17 | // freopen("Error.txt", "w", stderr); 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | #define int double 22 | 23 | int lcm(int a, int b){ 24 | int ans = (a * b)/ __gcd((long long)a,(long long)b); 25 | return ans; 26 | } 27 | 28 | void solve(int t){ 29 | int n,m,l,r; 30 | cin >> n >> m >> l >> r; 31 | 32 | int common = lcm(n,m); 33 | 34 | long long ans = floor(r/common) - ceil(l/common) + 1; 35 | // if(l % common == 0) ++ ans; 36 | 37 | cout << ans << "\n"; 38 | } 39 | 40 | int32_t main() 41 | { 42 | FastIO(); 43 | 44 | int t = 1; 45 | // cin >> t; 46 | 47 | rep(i,1,t+1){ 48 | solve(i); 49 | } 50 | return 0; 51 | } 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Number Theory/Problems/Common Divisors CodeForces - 1203C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 107; 9 | const int MX = 1e6; 10 | std::vector graph[N]; 11 | vector lvl(N, -1); 12 | 13 | void FastIO(){ 14 | #ifndef ONLINE_JUDGE 15 | freopen("in.txt", "r", stdin ); 16 | // freopen("out.txt", "w", stdout); 17 | // freopen("Error.txt", "w", stderr); 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | #define int long long 22 | 23 | void solve(int t){ 24 | int n , cnt = 0; 25 | cin >> n; 26 | 27 | int a[n] , gc = 0; 28 | rep(i,0,n) {cin >> a[i]; gc = __gcd(gc,a[i]);} 29 | 30 | for(int i = 1 ; i * i <= gc ; i++){ 31 | if(!(gc % i)){ 32 | ++cnt; 33 | if(i != (gc/i)) ++cnt; 34 | } 35 | 36 | } 37 | 38 | cout << cnt << endl; 39 | } 40 | 41 | int32_t main() 42 | { 43 | FastIO(); 44 | 45 | int t = 1; 46 | // cin >> t; 47 | 48 | rep(i,1,t+1){ 49 | solve(i); 50 | } 51 | return 0; 52 | } 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Number Theory/Problems/Count the factors UVA - 10699 .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 1e7 + 7; 9 | const int MX = 1e6; 10 | std::vector graph[N]; 11 | vector lvl(N, -1); 12 | 13 | void FastIO(){ 14 | #ifndef ONLINE_JUDGE 15 | freopen("in.txt", "r", stdin ); 16 | // freopen("out.txt", "w", stdout); 17 | // freopen("Error.txt", "w", stderr); 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | #define int long long 22 | 23 | // Modified sieve used to determine smallest prime factor 24 | 25 | int spf[N]; 26 | void SPF(){ 27 | rep(i,1,N) spf[i] = i; 28 | 29 | for(int i = 2; i * i <= N ; i++) 30 | for(int j = i * i ; j <= N ; j+=i) 31 | spf[j] = min(spf[j] , i); 32 | } 33 | 34 | void solve(int t){ 35 | while(1){ 36 | int n; 37 | cin >> n; 38 | if(n == 0) break; 39 | 40 | int nn = n; 41 | set ans; 42 | while(nn > 1){ 43 | ans.insert(spf[nn]); 44 | nn /= spf[nn]; 45 | } 46 | 47 | cout << n << " : " << ans.size() << "\n"; 48 | } 49 | } 50 | 51 | int32_t main() 52 | { 53 | FastIO(); 54 | SPF(); 55 | int t = 1; 56 | // cin >> t; 57 | 58 | rep(i,1,t+1){ 59 | solve(i); 60 | } 61 | return 0; 62 | } 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /Number Theory/Problems/Divisor Summation SPOJ - DIVSUM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 5e5 + 500; 9 | const int MX = 1e6; 10 | 11 | void FastIO(){ 12 | #ifndef ONLINE_JUDGE 13 | freopen("in.txt", "r", stdin ); 14 | // freopen("out.txt", "w", stdout); 15 | // freopen("Error.txt", "w", stderr); 16 | #endif // ONLINE_JUDGE 17 | } 18 | 19 | 20 | int ans[N]; 21 | void compute(){ 22 | for (int i = 1; i < N; ++i) 23 | { 24 | for (int j = 1; j*j <= i; ++j) 25 | { 26 | if(i % j == 0 and j != i){ 27 | ans[i] += j; 28 | 29 | if( (j != (i/j)) and (i/j) != i) ans[i] += (i/j); 30 | } 31 | } 32 | } 33 | } 34 | 35 | void solve(int t){ 36 | int n ; 37 | cin >> n; 38 | cout << ans[n] << '\n'; 39 | } 40 | 41 | int32_t main() 42 | { 43 | FastIO(); 44 | std::ios::sync_with_stdio(false);cin.tie(NULL); 45 | compute(); 46 | 47 | int t = 1; 48 | cin >> t; 49 | 50 | rep(i,1,t+1){ 51 | solve(i); 52 | } 53 | return 0; 54 | } 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Number Theory/Problems/Goldbach's Conjecture LightOJ - 1259 .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | int N = 1e7 + 7; 17 | set divs; 18 | vector check(N,1); 19 | 20 | void sieve(){ 21 | for (int i = 2; i * i <= N; ++i) 22 | { 23 | for (int j = i * i; j <= N; j+=i) 24 | { 25 | check[j] = 0; 26 | } 27 | } 28 | } 29 | 30 | void solve(int t){ 31 | int n , ans = 0; 32 | cin >> n; 33 | 34 | rep(i,2,n/2 + 1){ 35 | if(check[i]){ 36 | if(check[n-i]) 37 | ++ans; 38 | } 39 | } 40 | cout << "Case " << t << ": " << ans << endl; 41 | } 42 | 43 | int32_t main() 44 | { 45 | IO(); 46 | std::ios::sync_with_stdio(false);cin.tie(NULL); 47 | 48 | sieve(); 49 | int t = 1; 50 | 51 | cin >> t; 52 | 53 | rep(i,1,t+1){ 54 | solve(i); 55 | } 56 | return 0; 57 | } 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /Number Theory/Problems/Iftar Party LightOJ - 1014.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | 7 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 8 | #define endl "\n" 9 | 10 | void IO(){ 11 | #ifndef ONLINE_JUDGE 12 | freopen("in.txt", "r", stdin ); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt", "w", stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | 18 | 19 | 20 | void solve(int t){ 21 | set ans; 22 | int n,m; 23 | cin >> n >> m; 24 | 25 | n -= m ; 26 | 27 | for (int i = 1; i * i <= n; ++i) 28 | { 29 | if(n % i == 0){ 30 | if(i > m) ans.insert(i); 31 | if(n/i > m) ans.insert(n/i); 32 | } 33 | } 34 | 35 | cout << "Case " << t << ": " ; 36 | if(!ans.size()){ cout << "impossible\n"; return;} 37 | 38 | for(auto x:ans){ cout << x << ' ';} 39 | cout << "\n"; 40 | 41 | } 42 | 43 | int32_t main() 44 | { 45 | IO(); 46 | std::ios::sync_with_stdio(false);cin.tie(NULL); 47 | 48 | int t = 1; 49 | cin >> t; 50 | 51 | rep(i,1,t+1){ 52 | solve(i); 53 | } 54 | return 0; 55 | } 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Number Theory/Problems/Intelligent Factorial Factorization LightOJ - 1035.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void IO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | const int N = 105; 17 | int spf[N]; 18 | vector ans[N]; 19 | void SPF(){ 20 | 21 | rep(i,1,N) spf[i] = i; 22 | 23 | for(int i = 2; i * i <= N ; i++) 24 | for(int j = i * i ; j <= N ; j+=i) 25 | spf[j] = min(spf[j] , i); 26 | } 27 | 28 | void solve(int t){ 29 | int n ; 30 | cin >> n; 31 | int divOfN [n + 1] = {}; 32 | 33 | for(int i = 2 ; i <= n ; i++){ 34 | for(auto x:ans[i]) 35 | ++divOfN[x]; 36 | } 37 | 38 | int cnt = 0; 39 | for(int x:divOfN){ 40 | if(x) ++cnt; 41 | } 42 | 43 | cout << "Case " << t << ": " << n << " = "; 44 | 45 | rep(i,2,n+1){ 46 | if(divOfN[i]){ 47 | cout << i << " (" << divOfN[i] << ") "; 48 | --cnt; 49 | 50 | if(cnt) {cout << "* ";} 51 | else {cout << "\n"; break;} 52 | } 53 | 54 | } 55 | } 56 | 57 | int32_t main() 58 | { 59 | IO(); 60 | std::ios::sync_with_stdio(false);cin.tie(NULL); 61 | SPF(); 62 | 63 | rep(i,2,N){ 64 | int n = i; 65 | while(n > 1){ 66 | ans[i].push_back(spf[n]); 67 | n /= spf[n]; 68 | } 69 | } 70 | 71 | int t = 1; 72 | 73 | cin >> t; 74 | 75 | rep(i,1,t+1){ 76 | solve(i); 77 | } 78 | return 0; 79 | } 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /Number Theory/Problems/SPOJ - TDPRIMES.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | const int N = 100000007; 6 | 7 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 8 | #define endl "\n" 9 | 10 | void IO(){ 11 | #ifndef ONLINE_JUDGE 12 | freopen("in.txt", "r", stdin ); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt", "w", stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | 18 | 19 | vector prime(N,1); 20 | vector ans; 21 | void sieve(){ 22 | int cnt = 0; 23 | for (int i = 2; i * i < N; ++i) 24 | { 25 | if(prime[i]){ 26 | 27 | for (int j = i * i; j < N; j += i)prime[j] = 0; 28 | 29 | } 30 | } 31 | 32 | rep(i,2,N) if(prime[i]){ ++ cnt; if(cnt % 100 == 1) ans.push_back(i);} 33 | } 34 | 35 | void solve(int t){ 36 | for(auto x:ans) 37 | cout << x << "\n"; 38 | } 39 | 40 | int32_t main() 41 | { 42 | IO(); 43 | std::ios::sync_with_stdio(false);cin.tie(NULL); 44 | 45 | 46 | sieve(); 47 | int t = 1; 48 | // cin >> t; 49 | 50 | rep(i,1,t+1){ 51 | solve(i); 52 | } 53 | return 0; 54 | } 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Number Theory/Problems/Trailing Zeroes (I) LightOJ - 1028.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | #define endl "\n" 6 | 7 | const int N = 1e6 + 500; 8 | const int MX = 1e6; 9 | std::vector graph[N]; 10 | vector lvl(N, -1); 11 | 12 | void FastIO(){ 13 | #ifndef ONLINE_JUDGE 14 | freopen("in.txt", "r", stdin ); 15 | // freopen("out.txt", "w", stdout); 16 | // freopen("Error.txt", "w", stderr); 17 | #endif // ONLINE_JUDGE 18 | } 19 | 20 | #define int long long 21 | 22 | // Modified sieve used to determine smallest prime factor 23 | 24 | vector primes; 25 | vector check(N,1); 26 | void SPF(){ 27 | 28 | for(int i = 2; i * i <= N ; i++){ 29 | if(check[i]){ 30 | for(int j = i * i ; j <= N ; j+=i) 31 | check[j] = 0; 32 | } 33 | } 34 | 35 | rep(i,2,N) 36 | if(check[i]) primes.push_back(i); 37 | } 38 | void solve(int t){ 39 | 40 | int n,sum = 1; 41 | cin >> n; 42 | 43 | int cntOfTheprime = 0; 44 | 45 | for(int i = 0 ; primes[i] * primes[i] <= n & i < N ; i++){ 46 | cntOfTheprime = 0; 47 | while(n % primes[i] == 0){ 48 | ++cntOfTheprime; 49 | n /= primes[i]; 50 | } 51 | sum *= cn+1; 52 | } 53 | 54 | if(n != 1) sum *= 2; // it means there is another divisor which will be multiplied by each of the other divisors 55 | cout << "Case " << t << ": "; 56 | cout << sum -1 << "\n"; 57 | } 58 | 59 | int32_t main() 60 | { 61 | FastIO(); 62 | SPF(); 63 | int t = 1; 64 | cin >> t; 65 | 66 | rep(i,1,t+1){ 67 | solve(i); 68 | } 69 | return 0; 70 | } 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Number Theory/Problems/Twin Primes UVA - 10394.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | void FastIO(){ 9 | #ifndef ONLINE_JUDGE 10 | freopen("in.txt", "r", stdin ); 11 | // freopen("out.txt", "w", stdout); 12 | // freopen("Error.txt", "w", stderr); 13 | #endif // ONLINE_JUDGE 14 | } 15 | 16 | int N = 2e7 + 7; 17 | vector> ans; 18 | vector check(N,1); 19 | 20 | void sieve(){ 21 | for (int i = 2; i * i <= N; ++i) 22 | { 23 | for (int j = i * i; j <= N; j+=i) 24 | { 25 | check[j] = 0; 26 | } 27 | } 28 | 29 | rep(i,5,N) if(check[i] and check[i-2]) ans.push_back({i-2,i}); 30 | } 31 | 32 | void solve(int t){ 33 | int n; 34 | while(cin >> n){ 35 | cout << "(" << ans[n].first << ", "<< ans[n].second << ")\n"; 36 | } 37 | 38 | } 39 | 40 | int32_t main() 41 | { 42 | FastIO(); 43 | ans.push_back({0,0}); 44 | sieve(); 45 | 46 | int t = 1; 47 | 48 | // cin >> t; 49 | 50 | rep(i,1,t+1){ 51 | solve(i); 52 | } 53 | return 0; 54 | } 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Number Theory/Problems/UVa 374.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define rev(i,a,b) for(int i = a ; i >= b ; i--) 8 | 9 | #define endl '\n' 10 | #define lcm(a,b) (a*b)/(__gcd(a,b)) 11 | 12 | void IO() 13 | { 14 | std::ios::sync_with_stdio(false);cin.tie(NULL); 15 | #ifndef ONLINE_JUDGE 16 | freopen( "in.txt" , "r" ,stdin); 17 | // freopen("out.txt", "w", stdout); 18 | freopen("Error.txt","w",stderr); 19 | #endif // ONLINE_JUDGE 20 | } 21 | // #define int long long int 22 | 23 | #define all(x) x.begin(),x.end() 24 | const int N = 1e7 + 9; 25 | 26 | int BigMod(int B, int P , int M){ 27 | if(!P) return 1; 28 | if(P % 2){ 29 | int a = B % M; 30 | int b = BigMod(B,P-1,M); 31 | 32 | return (a * b) % M; 33 | } 34 | else{ 35 | int a = BigMod(B,P/2,M); 36 | return (a * a) % M; 37 | } 38 | } 39 | int32_t main() 40 | { 41 | IO(); 42 | int B,P,M; 43 | 44 | while(cin >> B >> P >> M){ 45 | cout << BigMod(B,P,M); 46 | cout << '\n'; 47 | } 48 | 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Number Theory/Problems/UVa12856 - Counting substhreengs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define rev(i,a,b) for(int i = a ; i >= b ; i--) 8 | 9 | #define endl '\n' 10 | #define lcm(a,b) (a*b)/(__gcd(a,b)) 11 | 12 | void IO() 13 | { 14 | std::ios::sync_with_stdio(false);cin.tie(NULL); 15 | #ifndef ONLINE_JUDGE 16 | freopen( "in.txt" , "r" ,stdin); 17 | // freopen("out.txt", "w", stdout); 18 | freopen("Error.txt","w",stderr); 19 | #endif // ONLINE_JUDGE 20 | } 21 | #define int long long 22 | 23 | #define all(x) x.begin(),x.end() 24 | const int N = 1e7 + 9; 25 | 26 | void solve(string s){ 27 | int ans = 0 ,curr = 0; 28 | int cnt[3] = {}; cnt[0] = 1; 29 | 30 | 31 | for(auto c:s){ 32 | if(!isdigit(c)){ 33 | cnt[0] = 1; 34 | cnt[1] = 0; cnt[2] = 0; 35 | curr = 0; 36 | 37 | continue; 38 | } 39 | 40 | curr = (curr + c - '0') % 3; 41 | ans += cnt[curr]; 42 | cnt[curr]++; 43 | } 44 | 45 | cout << ans << '\n'; 46 | } 47 | 48 | int32_t main() 49 | { 50 | IO(); 51 | int t = 1; 52 | // cin >> t; 53 | 54 | string s; 55 | 56 | while(cin >> s){ 57 | solve(s); 58 | } 59 | 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Number Theory/Problems/Uva 136 Ugly numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // #define int long long int 5 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 6 | #define endl "\n" 7 | 8 | const int N = 107; 9 | const int MX = 1e6; 10 | std::vector graph[N]; 11 | vector lvl(N, -1); 12 | 13 | void FastIO(){ 14 | #ifndef ONLINE_JUDGE 15 | freopen("in.txt", "r", stdin ); 16 | // freopen("out.txt", "w", stdout); 17 | // freopen("Error.txt", "w", stderr); 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | #define int long long int 22 | set res; 23 | 24 | void bfs(){ 25 | set q; 26 | q.insert(2); 27 | q.insert(3); 28 | q.insert(5); 29 | 30 | rep(i,0,2000){ 31 | int curr = *q.begin(); 32 | q.erase(q.begin()); 33 | 34 | q.insert(curr * 2); 35 | q.insert(curr * 3); 36 | q.insert(curr * 5); 37 | 38 | res.insert(curr * 2); 39 | res.insert(curr * 3); 40 | res.insert(curr * 5); 41 | 42 | // for(auto x:q) cout << x << ' '; cout << endl; 43 | } 44 | 45 | } 46 | 47 | void solve(int t){ 48 | res.insert(1); 49 | res.insert(2); 50 | res.insert(3); 51 | res.insert(5); 52 | 53 | bfs(); 54 | 55 | vector ans; 56 | for(auto x:res) ans.push_back(x); 57 | 58 | cout << "The 1500'th ugly number is "<< ans[1499] << ".\n"; 59 | } 60 | 61 | int32_t main() 62 | { 63 | FastIO(); 64 | 65 | int t = 1; 66 | // cin >> t; 67 | 68 | rep(i,1,t+1){ 69 | solve(i); 70 | } 71 | return 0; 72 | } 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /Number Theory/Problems/spf_lpf_divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | 7 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 8 | #define endl "\n" 9 | 10 | void IO(){ 11 | #ifndef ONLINE_JUDGE 12 | freopen("in.txt", "r", stdin ); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt", "w", stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | 18 | const int N = 1e6 + 7; 19 | int spf[N]; 20 | int lpf[N]; 21 | int dpf[N]; 22 | int tpf[N]; 23 | int divsum[N]; 24 | int divs[N]; 25 | vector primes; 26 | 27 | 28 | void SPF(){ 29 | rep(i,1,N) spf[i] = i; 30 | 31 | for (int i = 2; i * i < N; ++i) 32 | { 33 | for (int j = i * i; j < N; j += i) 34 | { 35 | spf[j] = min(spf[j],i); 36 | } 37 | } 38 | 39 | rep(i,2,N) if(spf[i] == i) primes.push_back(i); 40 | } 41 | 42 | void LPF(){ 43 | lpf[1] = 1; 44 | rep(i,2,N){ 45 | map factors; 46 | int n = i , mx = 0 , sm = 1 , prev = 1; 47 | while(n > 1){ 48 | factors[spf[n]]++; 49 | mx = max(mx , spf[n]); 50 | n /= spf[n]; 51 | } 52 | 53 | for(auto x: factors){ 54 | int p = x.first , e = x.second; 55 | int divisorSum = 0; 56 | for(int i = 0 ; i <= e ; i++) divisorSum += (int)(pow(p,i)); 57 | sm *= divisorSum; 58 | } 59 | 60 | divsum[i] = sm; 61 | for(auto x:factors) tpf[i] += x.second; 62 | dpf[i] = factors.size(); 63 | lpf[i] = mx; 64 | } 65 | 66 | } 67 | void DIVS(){ 68 | rep(j,2,N){ 69 | int n = j , div = 1; 70 | for(int i = 0 ; i < N and primes[i] * primes[i] <= n ; i++){ 71 | if(!(n % primes[i])){ 72 | int k = 0 ; 73 | while(!(n % primes[i])){ 74 | n /= primes[i]; ++k; 75 | } 76 | div *= (k+1); 77 | } 78 | } 79 | if(n != 1) div *= 2; 80 | divs[j] = div; 81 | } 82 | 83 | } 84 | 85 | void solve(int t){ 86 | int n; 87 | cin >> n; 88 | 89 | rep(i,0,n){ 90 | int a; 91 | cin >> a; 92 | cout << spf[a] << ' ' << lpf[a] << ' ' << dpf[a] << ' ' << tpf[a] << ' ' << divs[a] << ' ' << divsum[a] << '\n'; 93 | } 94 | } 95 | 96 | int32_t main() 97 | { 98 | IO(); 99 | std::ios::sync_with_stdio(false);cin.tie(NULL); 100 | 101 | SPF(); 102 | LPF(); 103 | DIVS(); 104 | 105 | int t = 1; 106 | // cin >> t; 107 | 108 | rep(i,1,t+1){ 109 | solve(i); 110 | } 111 | return 0; 112 | } 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /Number Theory/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/banner gif.gif -------------------------------------------------------------------------------- /Number Theory/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/codejam.png -------------------------------------------------------------------------------- /Number Theory/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Number Theory/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Number Theory/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/cses.PNG -------------------------------------------------------------------------------- /Number Theory/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/cses2.jpg -------------------------------------------------------------------------------- /Number Theory/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/icpc.jpeg -------------------------------------------------------------------------------- /Number Theory/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/icpc.jpg -------------------------------------------------------------------------------- /Number Theory/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/icpc.png -------------------------------------------------------------------------------- /Number Theory/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/images.png -------------------------------------------------------------------------------- /Number Theory/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/mix.png -------------------------------------------------------------------------------- /Number Theory/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/mix2.jpg -------------------------------------------------------------------------------- /Number Theory/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Number Theory/assets/my logo.jpeg -------------------------------------------------------------------------------- /Prefix sum/CSES Static Range Sum Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a; i < b; i++) 5 | 6 | void solve() 7 | { 8 | int n,k; 9 | cin >> n >> k; 10 | vector v(n+1); 11 | 12 | rep(i,1,n+1) 13 | cin >> v[i]; 14 | 15 | 16 | vector ans(n+1); 17 | ans[1] = v[1]; 18 | 19 | rep(i,2,n+1){ 20 | ans[i]=ans[i-1]+v[i]; 21 | } 22 | 23 | rep(i,0,k){ 24 | int st,end; 25 | cin >> st >> end; 26 | 27 | cout << ans[end]-ans[st-1] << '\n'; 28 | } 29 | 30 | } 31 | 32 | 33 | int main() 34 | { 35 | solve(); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Prefix sum/README.md: -------------------------------------------------------------------------------- 1 | > **Problems** 2 | 3 | 1. [**Problem - Static Range Queries**](https://cses.fi/problemset/task/1646)
4 | [**Solution - Static Range Queries**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Prefix%20sum/CSES%20Static%20Range%20Sum%20Queries.cpp) -------------------------------------------------------------------------------- /Prefix sum/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/banner gif.gif -------------------------------------------------------------------------------- /Prefix sum/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/codejam.png -------------------------------------------------------------------------------- /Prefix sum/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Prefix sum/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Prefix sum/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/cses.PNG -------------------------------------------------------------------------------- /Prefix sum/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/cses2.jpg -------------------------------------------------------------------------------- /Prefix sum/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/icpc.jpeg -------------------------------------------------------------------------------- /Prefix sum/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/icpc.jpg -------------------------------------------------------------------------------- /Prefix sum/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/icpc.png -------------------------------------------------------------------------------- /Prefix sum/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/images.png -------------------------------------------------------------------------------- /Prefix sum/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/mix.png -------------------------------------------------------------------------------- /Prefix sum/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/mix2.jpg -------------------------------------------------------------------------------- /Prefix sum/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Prefix sum/assets/my logo.jpeg -------------------------------------------------------------------------------- /Probabilities/README.md: -------------------------------------------------------------------------------- 1 | > Problems

2 | 3 | 4 | 1. Problem -> [CF 312B](https://codeforces.com/problemset/problem/312/B)
5 | Solution -> [Github](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Probabilities/CF%20312B.cpp) 6 | 7 | -------------------------------------------------------------------------------- /Probabilities/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/banner gif.gif -------------------------------------------------------------------------------- /Probabilities/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/codejam.png -------------------------------------------------------------------------------- /Probabilities/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Probabilities/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Probabilities/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/cses.PNG -------------------------------------------------------------------------------- /Probabilities/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/cses2.jpg -------------------------------------------------------------------------------- /Probabilities/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/icpc.jpeg -------------------------------------------------------------------------------- /Probabilities/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/icpc.jpg -------------------------------------------------------------------------------- /Probabilities/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/icpc.png -------------------------------------------------------------------------------- /Probabilities/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/images.png -------------------------------------------------------------------------------- /Probabilities/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/mix.png -------------------------------------------------------------------------------- /Probabilities/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/mix2.jpg -------------------------------------------------------------------------------- /Probabilities/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Probabilities/assets/my logo.jpeg -------------------------------------------------------------------------------- /Segment tree/1.Seg Tree (build update query)/segmentTree Lazy propagation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | std::ios::sync_with_stdio(false);cin.tie(NULL); 11 | #ifndef ONLINE_JUDGE 12 | freopen( "in.txt" , "r" ,stdin); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt","w",stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | 18 | const int N = 2e5 + 7; 19 | int num[N]; 20 | int sum[4*N] , lazy[4*N]; 21 | 22 | void build(int at , int L , int R) 23 | { 24 | if(L == R){ 25 | sum[at] = num[L]; 26 | return; 27 | } 28 | 29 | int mid = (L + R) / 2; 30 | 31 | build(at * 2 , L , mid); 32 | build(at * 2 + 1, mid + 1 , R); 33 | 34 | sum[at] = sum[at * 2] + sum[at * 2 + 1]; 35 | } 36 | 37 | int query(int at , int L , int R , int l , int r) 38 | { 39 | if(lazy[at]){ 40 | int pendingUpdate = lazy[at]; 41 | lazy[at] = 0; 42 | sum[at] += pendingUpdate * (R - L + 1); 43 | 44 | if(L != R){ 45 | lazy[2*at] += pendingUpdate; 46 | lazy[2*at+1] += pendingUpdate; 47 | } 48 | } 49 | 50 | if(r < L || R < l) return 0; 51 | if(l <= L && R <= r) return sum[at]; 52 | 53 | int mid = (L + R) / 2; 54 | int x = query(at * 2 , L , mid , l , r); 55 | int y = query(at * 2 + 1 , mid + 1 , R , l , r); 56 | 57 | return x + y; 58 | } 59 | 60 | void update(int at,int L,int R,int l, int r,int u) 61 | { 62 | if(lazy[at]){ 63 | int pendingUpdate = lazy[at]; 64 | lazy[at] = 0; 65 | sum[at] += pendingUpdate * (R - L + 1); 66 | 67 | if(L != R){ 68 | lazy[2*at] += pendingUpdate; 69 | lazy[2*at+1] += pendingUpdate; 70 | } 71 | } 72 | 73 | if(r < L || R < l) return; 74 | 75 | if(L >= l and R <= r){ 76 | int pendingUpdate = (r - l + 1) * u; 77 | sum[at] += pendingUpdate; 78 | 79 | if(l != r){ 80 | lazy[2*at] += u; 81 | lazy[2*at+1] += u; 82 | return; 83 | } 84 | } 85 | 86 | int mid = (L + R) / 2; 87 | 88 | update(at * 2 , L , mid, l, r, u); 89 | update(at * 2 + 1 , mid + 1, R , l, r, u); 90 | 91 | sum[at] = sum[at * 2] + sum[at * 2 + 1]; 92 | } 93 | 94 | void solve(int t){ 95 | int n,q; cin >> n >> q; 96 | rep(i,1,n+1) cin >> num[i]; 97 | 98 | build(1,1,n); 99 | rep(i,0,q){ 100 | int type; 101 | cin >> type; 102 | if(type == 1){ 103 | int l,r,u; 104 | cin >> l >> r >> u; 105 | update(1,1,n,l,r,u); 106 | } 107 | else{ 108 | int l,r; 109 | cin >> l >> r; 110 | cout << query(1,1,n,l,r) << endl; 111 | } 112 | } 113 | 114 | } 115 | 116 | int32_t main() 117 | { 118 | IO(); 119 | int t = 1; 120 | // cin >> t; 121 | 122 | rep(i,1,t+1) solve(i); 123 | 124 | return 0; 125 | } -------------------------------------------------------------------------------- /Segment tree/1.Seg Tree (build update query)/segmentTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | std::ios::sync_with_stdio(false);cin.tie(NULL); 11 | #ifndef ONLINE_JUDGE 12 | freopen( "in.txt" , "r" ,stdin); 13 | // freopen("out.txt", "w", stdout); 14 | freopen("Error.txt","w",stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | #define int long long 18 | 19 | #define all(x) x.begin(),x.end() 20 | 21 | const int N = 2e5 + 7; 22 | int num[N]; 23 | int sum[4*N]; 24 | 25 | void build(int at , int L , int R) 26 | { 27 | if(L == R){ 28 | sum[at] = num[L]; 29 | return; 30 | } 31 | 32 | int mid = (L + R) / 2; 33 | 34 | build(at * 2 , L , mid); 35 | build(at * 2 + 1, mid + 1 , R); 36 | 37 | sum[at] = sum[at * 2] + sum[at * 2 + 1]; 38 | } 39 | void update(int at,int L,int R,int pos,int u) 40 | { 41 | if(L == R){ 42 | sum[at] = u; 43 | return; 44 | } 45 | 46 | int mid = (L + R) / 2; 47 | 48 | if(pos <= mid) update(at * 2 , L , mid, pos, u); 49 | else update(at * 2 + 1 , mid + 1, R , pos , u); 50 | 51 | sum[at] = sum[at * 2] + sum[at * 2 + 1]; 52 | } 53 | 54 | int query(int at , int L , int R , int l , int r) 55 | { 56 | if(r < L || R < l) return 0; 57 | if(l <= L && R <= r) return sum[at]; 58 | 59 | int mid = (L + R) / 2; 60 | int x = query(at * 2 , L , mid , l , r); 61 | int y = query(at * 2 + 1 , mid + 1 , R , l , r); 62 | 63 | return x + y; 64 | } 65 | 66 | void solve(int t){ 67 | int n,q; cin >> n >> q; 68 | rep(i,1,n+1) cin >> num[i]; 69 | 70 | build(1,1,n); 71 | rep(i,0,q){ 72 | int type; 73 | cin >> type; 74 | if(type == 1){ 75 | int k,u; cin >> k >> u; 76 | update(1,1,n,k,u); 77 | } 78 | else{ 79 | int l,r; cin >> l >> r; 80 | cout << query(1,1,n,l,r) << endl; 81 | } 82 | } 83 | 84 | } 85 | 86 | int32_t main() 87 | { 88 | IO(); 89 | int t = 1; 90 | // cin >> t; 91 | 92 | rep(i,1,t+1) solve(i); 93 | 94 | return 0; 95 | } -------------------------------------------------------------------------------- /Segment tree/2.CSES Problems/CSES Range Xor Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | #define endl '\n' 6 | 7 | void IO() 8 | { 9 | std::ios::sync_with_stdio(false);cin.tie(NULL); 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | #endif // ONLINE_JUDGE 13 | } 14 | 15 | const int N = 2e5 + 7; 16 | const int M = 4 * N + 7; 17 | int a[N]; 18 | int minimum[M]; 19 | 20 | void build(int at , int L , int R) 21 | { 22 | if(L == R) 23 | { 24 | minimum[at] = a[L]; 25 | return ; 26 | } 27 | 28 | int mid = (L + R) / 2; 29 | build(at * 2, L , mid); 30 | build(at * 2 + 1 , mid + 1 , R); 31 | 32 | minimum[at] = minimum[at * 2] ^ minimum[at * 2 + 1]; 33 | } 34 | void update(int at , int L , int R , int pos , int u) 35 | { 36 | if(L == R){ 37 | minimum[at] = u; return; 38 | } 39 | int mid = (L + R) / 2; 40 | if(pos <= mid) update(at * 2 , L , mid , pos , u); 41 | else update(at * 2 + 1 , mid + 1 , R , pos , u); 42 | 43 | minimum[at] = min ( minimum[at * 2] , minimum[at * 2 + 1]); 44 | } 45 | 46 | int query(int at , int L , int R , int l , int r) 47 | { 48 | if(L > r or R < l) return 0; 49 | if(L >= l and R <= r) return minimum[at]; 50 | 51 | int mid = (L + R) / 2; 52 | int left = query(at * 2 , L , mid , l , r); 53 | int right = query(at * 2 + 1 , mid + 1 , R , l , r); 54 | 55 | return left ^ right; 56 | } 57 | 58 | int main() 59 | { 60 | IO(); 61 | int n , q; 62 | cin >> n >> q; 63 | 64 | rep(i,1,n+1) cin >> a[i]; 65 | build(1,1,n); 66 | 67 | rep(i,0,q){ 68 | int l , r; cin >> l >> r; cout << query(1,1,n,l,r) << endl; 69 | 70 | } 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Segment tree/2.CSES Problems/CSES Static Range Minimum Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | #define endl '\n' 6 | 7 | void IO() 8 | { 9 | std::ios::sync_with_stdio(false);cin.tie(NULL); 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | 17 | const int N = 2e5 + 7; 18 | const int M = 4 * N + 7; 19 | int a[N]; 20 | int minimum[M]; 21 | 22 | void build(int at , int L , int R) 23 | { 24 | if(L == R) 25 | { 26 | minimum[at] = a[L]; 27 | return ; 28 | } 29 | 30 | int mid = (L + R) / 2; 31 | build(at * 2, L , mid); 32 | build(at * 2 + 1 , mid + 1 , R); 33 | 34 | minimum[at] = min(minimum[at * 2] , minimum[at * 2 + 1]); 35 | } 36 | 37 | int query(int at , int L , int R , int l , int r) 38 | { 39 | if(L > r or R < l) return INT_MAX; 40 | if(L >= l and R <= r) return minimum[at]; 41 | 42 | int mid = (L + R) / 2; 43 | int left = query(at * 2 , L , mid , l , r); 44 | int right = query(at * 2 + 1 , mid + 1 , R , l , r); 45 | 46 | return min(left,right); 47 | } 48 | 49 | int main() 50 | { 51 | IO(); 52 | int n , q; 53 | cin >> n >> q; 54 | 55 | rep(i,1,n+1) cin >> a[i]; 56 | build(1,1,n); 57 | 58 | rep(i,0,q){ 59 | int l , r; 60 | cin >> l >> r; 61 | int ans = query(1,1,n,l,r); 62 | cout << ans << endl; 63 | } 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Segment tree/2.CSES Problems/CSES_Dynamic Range Minimum Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | #define endl '\n' 6 | 7 | void IO() 8 | { 9 | std::ios::sync_with_stdio(false);cin.tie(NULL); 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | #endif // ONLINE_JUDGE 13 | } 14 | 15 | const int N = 2e5 + 7; 16 | const int M = 4 * N + 7; 17 | int a[N]; 18 | int minimum[M]; 19 | 20 | void build(int at , int L , int R) 21 | { 22 | if(L == R) 23 | { 24 | minimum[at] = a[L]; 25 | return ; 26 | } 27 | 28 | int mid = (L + R) / 2; 29 | build(at * 2, L , mid); 30 | build(at * 2 + 1 , mid + 1 , R); 31 | 32 | minimum[at] = min(minimum[at * 2] , minimum[at * 2 + 1]); 33 | } 34 | void update(int at , int L , int R , int pos , int u) 35 | { 36 | if(L == R){ 37 | minimum[at] = u; return; 38 | } 39 | int mid = (L + R) / 2; 40 | if(pos <= mid) update(at * 2 , L , mid , pos , u); 41 | else update(at * 2 + 1 , mid + 1 , R , pos , u); 42 | 43 | minimum[at] = min ( minimum[at * 2] , minimum[at * 2 + 1]); 44 | } 45 | 46 | int query(int at , int L , int R , int l , int r) 47 | { 48 | if(L > r or R < l) return INT_MAX; 49 | if(L >= l and R <= r) return minimum[at]; 50 | 51 | int mid = (L + R) / 2; 52 | int left = query(at * 2 , L , mid , l , r); 53 | int right = query(at * 2 + 1 , mid + 1 , R , l , r); 54 | 55 | return min(left,right); 56 | } 57 | 58 | int main() 59 | { 60 | IO(); 61 | int n , q; 62 | cin >> n >> q; 63 | 64 | rep(i,1,n+1) cin >> a[i]; 65 | build(1,1,n); 66 | 67 | rep(i,0,q){ 68 | int type; cin >> type; 69 | switch(type){ 70 | case 1: int pos , u; cin >> pos >> u; update(1,1,n,pos,u); break; 71 | default: int l , r; cin >> l >> r; cout << query(1,1,n,l,r) << endl; 72 | } 73 | } 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Segment tree/2.CSES Problems/CSES_Dynamic Range Sum Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | std::ios::sync_with_stdio(false);cin.tie(NULL); 11 | #ifndef ONLINE_JUDGE 12 | freopen( "in.txt" , "r" ,stdin); 13 | // freopen("out.txt", "w", stdout); 14 | freopen("Error.txt","w",stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | #define int long long 18 | 19 | #define all(x) x.begin(),x.end() 20 | 21 | const int N = 2e5 + 7; 22 | int num[N]; 23 | int sum[4*N]; 24 | 25 | void build(int at , int L , int R) 26 | { 27 | sum[at] = 0; 28 | 29 | if(L == R){ 30 | // here we will do our desired task 31 | // (we have done it for sum over a range) 32 | sum[at] = num[L]; 33 | 34 | return; 35 | } 36 | 37 | int mid = (L + R) / 2; 38 | 39 | build(at * 2 , L , mid); 40 | build(at * 2 + 1, mid + 1 , R); 41 | 42 | sum[at] = sum[at * 2] + sum[at * 2 + 1]; 43 | } 44 | void update(int at,int L,int R,int pos,int u) 45 | { 46 | if(L == R){ 47 | sum[at] = u; 48 | return; 49 | } 50 | 51 | int mid = (L + R) / 2; 52 | 53 | if(pos <= mid) update(at * 2 , L , mid, pos, u); 54 | else update(at * 2 + 1 , mid + 1, R , pos , u); 55 | 56 | sum[at] = sum[at * 2] + sum[at * 2 + 1]; 57 | } 58 | 59 | int query(int at , int L , int R , int l , int r) 60 | { 61 | if(r < L || R < l) return 0; 62 | if(l <= L && R <= r) return sum[at]; 63 | 64 | int mid = (L + R) / 2; 65 | int x = query(at * 2 , L , mid , l , r); 66 | int y = query(at * 2 + 1 , mid + 1 , R , l , r); 67 | 68 | return x + y; 69 | } 70 | 71 | void solve(int t){ 72 | int n,q; cin >> n >> q; 73 | rep(i,1,n+1) cin >> num[i]; 74 | 75 | build(1,1,n); 76 | rep(i,0,q){ 77 | int type; 78 | cin >> type; 79 | if(type == 1){ 80 | int k,u; cin >> k >> u; 81 | update(1,1,n,k,u); 82 | } 83 | else{ 84 | int l,r; cin >> l >> r; 85 | cout << query(1,1,n,l,r) << endl; 86 | } 87 | } 88 | 89 | } 90 | 91 | int32_t main() 92 | { 93 | IO(); 94 | int t = 1; 95 | // cin >> t; 96 | 97 | rep(i,1,t+1) solve(i); 98 | 99 | return 0; 100 | } -------------------------------------------------------------------------------- /Segment tree/README.md: -------------------------------------------------------------------------------- 1 | 2 | > Concepts

3 | 4 | 1. [segmentTree](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Segment%20tree/1.Seg%20Tree%20(build%20update%20query)/segmentTree.cpp) 5 | 1. [SegmentTree Lazy propagation](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Segment%20tree/1.Seg%20Tree%20(build%20update%20query)/segmentTree%20Lazy%20propagation.cpp) 6 | 7 | > Problems

8 | 9 | 1. [CSES Range Xor Queries](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Segment%20tree/2.CSES%20Problems/CSES%20Range%20Xor%20Queries.cpp) 10 | 1. [CSES Static Range Minimum Queries](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Segment%20tree/2.CSES%20Problems/CSES%20Static%20Range%20Minimum%20Queries.cpp) 11 | 1. [CSES_Dynamic Range Minimum Queries](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Segment%20tree/2.CSES%20Problems/CSES_Dynamic%20Range%20Minimum%20Queries.cpp) 12 | 1. [CSES_Dynamic Range Sum Queries](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Segment%20tree/2.CSES%20Problems/CSES_Dynamic%20Range%20Sum%20Queries.cpp) 13 | -------------------------------------------------------------------------------- /Segment tree/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/banner gif.gif -------------------------------------------------------------------------------- /Segment tree/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/codejam.png -------------------------------------------------------------------------------- /Segment tree/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Segment tree/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Segment tree/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/cses.PNG -------------------------------------------------------------------------------- /Segment tree/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/cses2.jpg -------------------------------------------------------------------------------- /Segment tree/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/icpc.jpeg -------------------------------------------------------------------------------- /Segment tree/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/icpc.jpg -------------------------------------------------------------------------------- /Segment tree/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/icpc.png -------------------------------------------------------------------------------- /Segment tree/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/images.png -------------------------------------------------------------------------------- /Segment tree/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/mix.png -------------------------------------------------------------------------------- /Segment tree/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/mix2.jpg -------------------------------------------------------------------------------- /Segment tree/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Segment tree/assets/my logo.jpeg -------------------------------------------------------------------------------- /Sorting and Searching/CSES Collecting Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | 7 | 8 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 9 | #define endl '\n' 10 | void IO() 11 | { 12 | #ifndef ONLINE_JUDGE 13 | freopen( "in.txt" , "r" ,stdin); 14 | // freopen("out.txt", "w", stdout); 15 | // freopen("Error.txt","w",stderr); 16 | #endif // ONLINE_JUDGE 17 | } 18 | #define int long long int 19 | #define all(x) x.begin(),x.end() 20 | const int N = 2e5 + 7; 21 | 22 | int pos[N]; 23 | 24 | void solve(int t){ 25 | int n; cin >> n; 26 | rep(i,1,n+1){ 27 | int a; 28 | cin >> a; 29 | pos[a] = i; 30 | } 31 | int ans = 1; 32 | 33 | rep(i,1,n+1){ 34 | if(pos[i] < pos[i-1]) ++ans; 35 | } 36 | 37 | cout << ans << '\n'; 38 | } 39 | int32_t main() 40 | { 41 | std::ios::sync_with_stdio(false);cin.tie(NULL); 42 | IO(); 43 | int t = 1; 44 | // cin >> t; 45 | 46 | rep(i,1,t+1){ 47 | solve(i); 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES Maximum Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | 7 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 8 | #define endl '\n' 9 | void IO() 10 | { 11 | #ifndef ONLINE_JUDGE 12 | freopen( "in.txt" , "r" ,stdin); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt","w",stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | #define int long long int 18 | #define all(x) x.begin(),x.end() 19 | 20 | 21 | void solve(int t){ 22 | int mx = LLONG_MIN; 23 | int n; 24 | cin >> n; 25 | 26 | int a[n]; 27 | rep(i,0,n){ 28 | cin >> a[i]; 29 | mx = max(mx,a[i]); 30 | } 31 | 32 | int curr = 0; 33 | 34 | rep(i,0,n){ 35 | curr += a[i]; 36 | mx = max(mx,curr); 37 | 38 | if(curr < 0) curr = 0; 39 | } 40 | cout << mx << '\n'; 41 | } 42 | int32_t main() 43 | { 44 | std::ios::sync_with_stdio(false);cin.tie(NULL); 45 | IO(); 46 | int t = 1; 47 | // cin >> t; 48 | 49 | rep(i,1,t+1){ 50 | solve(i); 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES Movie Festival.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | // #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | 20 | void solve(int t){ 21 | int n; cin >> n; 22 | pair time[n]; 23 | 24 | rep(i,0,n){ int a,b; cin >> a >> b; time[i] = {b,a};} 25 | sort(time,time+n); 26 | 27 | int ans = 1 , ending = time[0].first; 28 | 29 | rep(i,1,n){ 30 | if(time[i].second >= ending){ 31 | ++ans; 32 | ending = time[i].first; 33 | } 34 | } 35 | 36 | cout << ans << '\n'; 37 | } 38 | int32_t main() 39 | { 40 | std::ios::sync_with_stdio(false);cin.tie(NULL); 41 | IO(); 42 | 43 | int t = 1; 44 | // cin >> t; 45 | 46 | rep(i,1,t+1){ 47 | solve(i); 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES Restaurant Customers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | 20 | void solve(int t){ 21 | int n; cin >> n; 22 | 23 | vector> time; 24 | 25 | rep(i,0,n){ 26 | int a,b; cin >> a >> b; 27 | time.push_back({a,1}); time.push_back({b,-1}); 28 | } 29 | 30 | sort(time.begin(),time.end()); 31 | 32 | int res = 0 , ans = 0; 33 | for(auto x:time){ 34 | res += x.second; 35 | ans = max(ans,res); 36 | } 37 | 38 | cout << ans << '\n'; 39 | } 40 | int32_t main() 41 | { 42 | std::ios::sync_with_stdio(false);cin.tie(NULL); 43 | IO(); 44 | 45 | int t = 1; 46 | // cin >> t; 47 | 48 | rep(i,1,t+1){ 49 | solve(i); 50 | } 51 | 52 | return 0; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES Stick Lengths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | 7 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 8 | #define endl '\n' 9 | void IO() 10 | { 11 | #ifndef ONLINE_JUDGE 12 | freopen( "in.txt" , "r" ,stdin); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt","w",stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | #define int long long int 18 | #define all(x) x.begin(),x.end() 19 | 20 | 21 | void solve(int t){ 22 | int n; 23 | cin >> n; 24 | 25 | int a[n]; 26 | rep(i,0,n) cin >> a[i]; 27 | sort(a,a+n); 28 | 29 | int mid = a[n/2]; 30 | 31 | int sm = 0; 32 | 33 | rep(i,0,n) sm += abs(mid - a[i]); 34 | cout << sm << '\n'; 35 | } 36 | int32_t main() 37 | { 38 | std::ios::sync_with_stdio(false);cin.tie(NULL); 39 | IO(); 40 | int t = 1; 41 | // cin >> t; 42 | 43 | rep(i,1,t+1){ 44 | solve(i); 45 | } 46 | 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES Sum of Two Values.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | 7 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 8 | #define endl '\n' 9 | void IO() 10 | { 11 | #ifndef ONLINE_JUDGE 12 | freopen( "in.txt" , "r" ,stdin); 13 | // freopen("out.txt", "w", stdout); 14 | // freopen("Error.txt","w",stderr); 15 | #endif // ONLINE_JUDGE 16 | } 17 | // #define int long long int 18 | #define all(x) x.begin(),x.end() 19 | 20 | const int N = 1e5 + 7; 21 | vector ans; 22 | int n,m; 23 | int indeg[N] = {}; 24 | vector g[N]; 25 | bool vis[N]; 26 | 27 | 28 | void solve(int t){ 29 | map count; 30 | map> pos; 31 | 32 | int n,sm; 33 | bool ok = 0; 34 | cin >> n >> sm; 35 | 36 | int a[n+1]; 37 | rep(i,1,n+1){ 38 | cin >> a[i]; 39 | count[a[i]]++; 40 | pos[a[i]].push_back(i); 41 | } 42 | 43 | rep(i,1,n){ 44 | int need = sm - a[i]; 45 | --count[a[i]]; 46 | 47 | if(count[need]){ 48 | if(need == a[i]) cout << pos[a[i]][0] << ' ' << pos[a[i]][1] << '\n'; 49 | else cout << pos[a[i]][0] << ' ' << pos[need][0] << '\n'; 50 | 51 | ok = 1; 52 | break; 53 | } 54 | } 55 | if(!ok) cout << "IMPOSSIBLE\n"; 56 | } 57 | int32_t main() 58 | { 59 | std::ios::sync_with_stdio(false);cin.tie(NULL); 60 | IO(); 61 | int t = 1; 62 | // cin >> t; 63 | 64 | rep(i,1,t+1){ 65 | solve(i); 66 | } 67 | 68 | return 0; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES Towers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 7 | #define endl '\n' 8 | void IO() 9 | { 10 | #ifndef ONLINE_JUDGE 11 | freopen( "in.txt" , "r" ,stdin); 12 | // freopen("out.txt", "w", stdout); 13 | // freopen("Error.txt","w",stderr); 14 | #endif // ONLINE_JUDGE 15 | } 16 | #define int long long int 17 | #define all(x) x.begin(),x.end() 18 | 19 | void solve() { 20 | int n; cin >> n; 21 | 22 | multiset stack; 23 | 24 | rep(i,0,n){ 25 | int x; 26 | cin >> x; 27 | 28 | auto idx = stack.upper_bound(x); 29 | if(idx != stack.end()) stack.erase(idx); 30 | 31 | stack.insert(x); 32 | } 33 | cout << stack.size() << '\n'; 34 | } 35 | int32_t main() 36 | { 37 | std::ios::sync_with_stdio(false);cin.tie(NULL); 38 | IO(); 39 | solve(); 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES apartments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a ; i < b ; i++) 5 | #define int long long int 6 | #define all(x) x.begin(),x.end() 7 | 8 | const int N = 2e5 + 7; 9 | 10 | void solve(){ 11 | int n,m,k; cin >> n >> m >> k; 12 | 13 | int client[n] ,apartment[m]; 14 | 15 | rep(i,0,n) cin >> client[i]; 16 | rep(i,0,m) cin >> apartment[i]; 17 | 18 | sort(client,client+n); sort(apartment,apartment+m); 19 | 20 | int cnt = 0 , i = 0 , j = 0; 21 | while(i < n and j < m){ 22 | if(abs(client[i] - apartment[j]) <= k){ ++cnt ; ++i; ++j;} 23 | else if(apartment[j] - client[i] > k) ++i; 24 | else ++j; 25 | } 26 | cout << cnt << '\n'; 27 | } 28 | int32_t main() 29 | { 30 | std::ios::sync_with_stdio(false);cin.tie(NULL); 31 | 32 | solve(); 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES concert tickets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | #define int long long 6 | 7 | //Starting in the name of Allah(THE ALMIGHTY) 8 | void FastIO() 9 | { 10 | std::ios::sync_with_stdio(false);cin.tie(NULL); // Reduced 1200 ms time 11 | #ifndef ONLINE_JUDGE 12 | freopen("in.txt","r",stdin); 13 | freopen("out.txt","w",stdout); 14 | #endif // ONLINE_JUDGE 15 | } 16 | 17 | 18 | 19 | void solve() 20 | { 21 | multiset tickets; // using vector will be give TLE 22 | int n , m; cin >> n >> m; 23 | 24 | for (int i = 0; i < n; ++i) 25 | { 26 | int num; cin >> num; tickets.insert(num); 27 | } 28 | 29 | for (int i = 0; i < m; ++i) 30 | { 31 | int num; cin >> num; 32 | 33 | auto index = tickets.upper_bound(num); 34 | 35 | if(index == tickets.begin()) cout << -1 << '\n'; 36 | else{ --index; cout << *index << '\n'; tickets.erase(index);} 37 | } 38 | } 39 | 40 | 41 | int32_t main() 42 | { 43 | FastIO(); 44 | 45 | solve(); 46 | 47 | return 0; 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES subarray sums I [using set].cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | //Starting in the name of Allah(THE ALMIGHTY) 6 | 7 | #define rep(i,a,b) for(int i = a; i < b;++i) 8 | #define rev(i,a,b) for(int i = a; i >= b;--i) 9 | #define int long long int 10 | 11 | void solve(int t){ 12 | int n,x; cin >> n >> x; 13 | int a[n] , curr = 0,ans = 0;; 14 | 15 | set totals; 16 | totals.insert(0); 17 | 18 | rep(i,0,n){ 19 | cin >> a[i]; 20 | curr += a[i]; 21 | ans += totals.count(curr-x); 22 | totals.insert(curr); 23 | } 24 | 25 | cout << ans << '\n'; 26 | 27 | } 28 | 29 | int32_t main() 30 | { 31 | // freopen("in.txt","r",stdin); 32 | int T = 1; 33 | // cin >> T; 34 | 35 | rep(i,1,T+1){ 36 | solve(i); 37 | } 38 | 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Sorting and Searching/CSES subarray sums I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | //Starting in the name of Allah(THE ALMIGHTY) 6 | 7 | #define rep(i,a,b) for(int i = a; i < b;++i) 8 | #define rev(i,a,b) for(int i = a; i >= b;--i) 9 | #define int long long int 10 | 11 | void solve(int t){ 12 | int n,x; cin >> n >> x; 13 | 14 | int a[n]; 15 | rep(i,0,n) cin >> a[i]; 16 | 17 | int ans = 0 , curr = 0; 18 | map m; 19 | m[0] = 1; 20 | 21 | rep(i,0,n){ 22 | curr += a[i]; 23 | ans += m[curr - x]; 24 | m[curr]++; 25 | } 26 | 27 | cout << ans << '\n'; 28 | 29 | } 30 | 31 | int32_t main() 32 | { 33 | 34 | int T = 1; 35 | // cin >> T; 36 | 37 | rep(i,1,T+1){ 38 | solve(i); 39 | } 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Sorting and Searching/Distinct Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | typedef long long ll; 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | 12 | ll n,move=1,diff; 13 | cin>>n; 14 | ll ar[n]; 15 | 16 | for(ll i=0;i>ar[i]; 19 | } 20 | 21 | sort(ar,ar+n); 22 | 23 | for(ll j=0;j Problems with solutions

3 | 4 | 5 | 1. [CSES Collecting Numbers](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20Collecting%20Numbers.cpp) 6 | 1. [CSES Maximum Subarray Sum](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20Maximum%20Subarray%20Sum.cpp) 7 | 1. [CSES Movie Festival](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20Movie%20Festival.cpp) 8 | 1. [CSES Restaurant Customers](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20Restaurant%20Customers.cpp) 9 | 1. [CSES Stick Lengths](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20Stick%20Lengths.cpp) 10 | 1. [CSES Sum of Two Values](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20Sum%20of%20Two%20Values.cpp) 11 | 1. [CSES Towers](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20Towers.cpp) 12 | 1. [CSES apartments](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20apartments.cpp) 13 | 1. [CSES concert tickets](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/CSES%20concert%20tickets.cpp) 14 | 1. [Distinct Numbers](https://github.com/khalid586/CSES-Problemset-solutions/blob/main/2.Sorting%20and%20Searching/Distinct%20Numbers.cpp) 15 | 1. [CSES subarray sums I](https://github.com/khalid586/CSES-Problemset-Solutions/blob/main/2.Sorting%20and%20Searching/CSES%20subarray%20sums%20I.cpp) 16 | - [CSES subarray sums I [using set]](https://github.com/khalid586/CSES-Problemset-Solutions/blob/main/2.Sorting%20and%20Searching/CSES%20subarray%20sums%20I%20%5Busing%20set%5D.cpp) -------------------------------------------------------------------------------- /Sorting and Searching/assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/banner gif.gif -------------------------------------------------------------------------------- /Sorting and Searching/assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/codejam.png -------------------------------------------------------------------------------- /Sorting and Searching/assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/cp banner.jpeg -------------------------------------------------------------------------------- /Sorting and Searching/assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/cses banner.jpeg -------------------------------------------------------------------------------- /Sorting and Searching/assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/cses.PNG -------------------------------------------------------------------------------- /Sorting and Searching/assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/cses2.jpg -------------------------------------------------------------------------------- /Sorting and Searching/assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/icpc.jpeg -------------------------------------------------------------------------------- /Sorting and Searching/assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/icpc.jpg -------------------------------------------------------------------------------- /Sorting and Searching/assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/icpc.png -------------------------------------------------------------------------------- /Sorting and Searching/assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/images.png -------------------------------------------------------------------------------- /Sorting and Searching/assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/mix.png -------------------------------------------------------------------------------- /Sorting and Searching/assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/mix2.jpg -------------------------------------------------------------------------------- /Sorting and Searching/assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/Sorting and Searching/assets/my logo.jpeg -------------------------------------------------------------------------------- /Sorting and Searching/concept/sum of two numbers within a range.cpp: -------------------------------------------------------------------------------- 1 | //Starting in the name of Allah(THE ALMIGHTY) 2 | 3 | #include 4 | using namespace std; 5 | 6 | void FastIO() 7 | { 8 | #ifndef ONLINE_JUDGE 9 | freopen("in.txt","r", stdin); 10 | //freopen("out.txt", "w", stdout); 11 | freopen("Error.txt", "w", stderr); 12 | #endif // ONLINE_JUDGE 13 | } 14 | 15 | const int N = 1e6+7; 16 | 17 | #define int long long 18 | #define rep(i,a,b) for(int i = a; i < b;++i) 19 | #define rev(i,a,b) for(int i = a; i >= b;--i) 20 | 21 | void solve(int t){ 22 | int n; 23 | cin >> n; 24 | 25 | int x,y; 26 | cin >> x >> y; 27 | 28 | vector a(n); 29 | 30 | rep(i,0,n) 31 | cin >> a[i]; 32 | 33 | sort(a.begin(),a.end()); 34 | 35 | int ans = 0; 36 | 37 | rep(i,0,n){ 38 | int low = lower_bound(a.begin()+i+1,a.end(),x-a[i]) - a.begin(); 39 | int high = upper_bound(a.begin()+i+1,a.end(),y-a[i]) - a.begin(); 40 | 41 | 42 | int ele = high - low; 43 | 44 | ans += ele; 45 | } 46 | 47 | cout << "Case " << t << ": " << ans << '\n'; 48 | } 49 | 50 | int32_t main() 51 | { 52 | FastIO(); 53 | 54 | int T = 1; 55 | cin >> T; 56 | 57 | rep(i,1,T+1){ 58 | solve(i); 59 | } 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /assets/banner gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/banner gif.gif -------------------------------------------------------------------------------- /assets/codejam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/codejam.png -------------------------------------------------------------------------------- /assets/cp banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/cp banner.jpeg -------------------------------------------------------------------------------- /assets/cses banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/cses banner.jpeg -------------------------------------------------------------------------------- /assets/cses.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/cses.PNG -------------------------------------------------------------------------------- /assets/cses2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/cses2.jpg -------------------------------------------------------------------------------- /assets/icpc.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/icpc.jpeg -------------------------------------------------------------------------------- /assets/icpc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/icpc.jpg -------------------------------------------------------------------------------- /assets/icpc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/icpc.png -------------------------------------------------------------------------------- /assets/images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/images.png -------------------------------------------------------------------------------- /assets/mix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/mix.png -------------------------------------------------------------------------------- /assets/mix2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/mix2.jpg -------------------------------------------------------------------------------- /assets/my logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/my logo.jpeg -------------------------------------------------------------------------------- /assets/purple work.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khalid586/Competitive-Programming-Topics/f90f2a13c17034fb8b654445e64fcdcf5c0a63af/assets/purple work.gif -------------------------------------------------------------------------------- /contribution.md: -------------------------------------------------------------------------------- 1 | ## Readme Update 2 | Have a look at backtracking folder's markdown file [Backtracking](https://github.com/khalid586/Competitive-programming-Topics/blob/main/Backtracking/README.md). 3 | 4 | All the other topics also need to have same readme like this one. 5 | 6 | **All you have to do is that** 7 | 8 | 1. Create a table 9 | 1. Attach the problem link 10 | > we will find the link by using google search 11 | 1. Attach the solution URL 12 | > You will find it in the readme file. 13 | 14 |
15 | 16 | Besides You can also add useful algorithms and problems accroding to topics. Don't forget to update the readme file. -------------------------------------------------------------------------------- /string algorithms/Basic string implementation/CF 1104B Game with string.cpp: -------------------------------------------------------------------------------- 1 | //Starting in the name of Allah(THE ALMIGHTY) 2 | 3 | #include 4 | using namespace std; 5 | const int N = 1e6+7; 6 | 7 | #define int long long 8 | #define rep(i,a,b) for(int i = a; i < b;++i) 9 | #define rev(i,a,b) for(int i = a; i >= b;--i) 10 | 11 | void FastIO() 12 | { 13 | #ifndef ONLINE_JUDGE 14 | freopen("in.txt","r", stdin); 15 | //freopen("out.txt", "w", stdout); 16 | freopen("Error.txt", "w", stderr); 17 | 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | void solve(int t){ 22 | string s; 23 | cin >> s; 24 | 25 | int n = s.size() , cnt = 0; 26 | stack ans; 27 | 28 | rep(i,0,n){ 29 | if(ans.empty()){ 30 | ans.push(s[i]); 31 | } 32 | else { 33 | if(ans.top() != s[i]) 34 | ans.push(s[i]); 35 | else 36 | ++cnt, 37 | ans.pop(); 38 | } 39 | 40 | } 41 | 42 | cout << (cnt % 2 ? "Yes\n":"No\n"); 43 | } 44 | 45 | int32_t main() 46 | { 47 | FastIO(); 48 | int T = 1; 49 | // cin >> T; 50 | 51 | rep(i,1,T+1){ 52 | solve(i); 53 | } 54 | return 0; 55 | } 56 | 57 | -------------------------------------------------------------------------------- /string algorithms/Basic string implementation/CF 665C simple strings.cpp: -------------------------------------------------------------------------------- 1 | //Starting in the name of Allah(THE ALMIGHTY) 2 | 3 | #include 4 | using namespace std; 5 | const int N = 1e6+7; 6 | 7 | #define int long long 8 | #define rep(i,a,b) for(int i = a; i < b;++i) 9 | #define rev(i,a,b) for(int i = a; i >= b;--i) 10 | 11 | void FastIO() 12 | { 13 | #ifndef ONLINE_JUDGE 14 | freopen("in.txt","r", stdin); 15 | //freopen("out.txt", "w", stdout); 16 | freopen("Error.txt", "w", stderr); 17 | 18 | #endif // ONLINE_JUDGE 19 | } 20 | 21 | void solve(int t){ 22 | string s; 23 | cin >> s; 24 | 25 | int n = s.size(); 26 | 27 | rep(i,1,n){ 28 | if(s[i] == s[i-1]){ 29 | while(1){ 30 | s[i] = (s[i]-'a' + 1) % 26 + 'a'; 31 | 32 | if(i+1 >= n) break; 33 | else if(s[i+1] != s[i]) break; 34 | } 35 | } 36 | 37 | } 38 | 39 | cout << s << '\n'; 40 | } 41 | 42 | int32_t main() 43 | { 44 | FastIO(); 45 | int T = 1; 46 | // cin >> T; 47 | 48 | rep(i,1,T+1){ 49 | solve(i); 50 | } 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /string algorithms/Basic string implementation/Chef and Pick digit.cpp: -------------------------------------------------------------------------------- 1 | //Starting in the name of Allah(THE ALMIGHTY) 2 | 3 | #include 4 | using namespace std; 5 | const int N = 1e6+7; 6 | 7 | #define int long long 8 | #define rep(i,a,b) for(int i = a; i < b;++i) 9 | #define rev(i,a,b) for(int i = a; i >= b;--i) 10 | 11 | void FastIO() 12 | { 13 | #ifndef ONLINE_JUDGE 14 | freopen("in.txt","r", stdin); 15 | //freopen("out.txt", "w", stdout); 16 | freopen("Error.txt", "w", stderr); 17 | #endif // ONLINE_JUDGE 18 | } 19 | 20 | bool check(string s){ 21 | int n = s.size(); 22 | rep(i,0,n/2){ 23 | if(s[i] != s[n-1-i]) return 0; 24 | } 25 | 26 | return 1; 27 | } 28 | 29 | void solve(int t){ 30 | string s; 31 | cin >> s; 32 | 33 | map cnt; 34 | set ans; 35 | int n = s.size(); 36 | 37 | rep(i,0,n) cnt[s[i]]++; 38 | 39 | rep(i,65,91){ 40 | char a = i/10 + '0'; 41 | char b = i%10 + '0'; 42 | 43 | if(a == b){ 44 | if(cnt[a] > 1) 45 | ans.insert(i); 46 | } 47 | else{ 48 | if(cnt[a] and cnt[b]) 49 | ans.insert(i); 50 | } 51 | } 52 | 53 | for(auto x:ans) cout << x ; cout << '\n'; 54 | } 55 | 56 | int32_t main() 57 | { 58 | FastIO(); 59 | int T = 1; 60 | cin >> T; 61 | 62 | rep(i,1,T+1){ 63 | solve(i); 64 | } 65 | return 0; 66 | } 67 | 68 | -------------------------------------------------------------------------------- /string algorithms/Basic string implementation/Kattis Palindrome substring.cpp: -------------------------------------------------------------------------------- 1 | //Starting in the name of Allah(THE ALMIGHTY) 2 | 3 | #include 4 | using namespace std; 5 | const int N = 1e6+7; 6 | 7 | #define int long long 8 | #define rep(i,a,b) for(int i = a; i < b;++i) 9 | #define rev(i,a,b) for(int i = a; i >= b;--i) 10 | 11 | void FastIO() 12 | { 13 | #ifndef ONLINE_JUDGE 14 | freopen("in.txt","r", stdin); 15 | //freopen("out.txt", "w", stdout); 16 | freopen("Error.txt", "w", stderr); 17 | #endif // ONLINE_JUDGE 18 | } 19 | 20 | bool check(string s){ 21 | int n = s.size(); 22 | rep(i,0,n/2){ 23 | if(s[i] != s[n-1-i]) return 0; 24 | } 25 | 26 | return 1; 27 | } 28 | 29 | void solve(int t){ 30 | vector p; 31 | string s; 32 | 33 | while(cin >> s){ 34 | p.push_back(s); 35 | } 36 | 37 | 38 | rep(i,0,p.size()){ 39 | int n = p[i].size(); 40 | set ans; 41 | s = p[i]; 42 | 43 | rep(i,0,n){ 44 | string curr; 45 | curr += s[i]; 46 | 47 | rep(j,i+1,n){ 48 | curr += s[j]; 49 | 50 | if(check(curr)) 51 | ans.insert(curr); 52 | } 53 | } 54 | 55 | for(auto x:ans) cout << x << '\n'; 56 | 57 | if(i != p.size()-1) cout << '\n'; 58 | } 59 | } 60 | 61 | int32_t main() 62 | { 63 | FastIO(); 64 | int T = 1; 65 | // cin >> T; 66 | 67 | rep(i,1,T+1){ 68 | solve(i); 69 | } 70 | return 0; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /string algorithms/Basic string implementation/remove common char & concate.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // C++ program Find concatenated string with 3 | // uncommon characters of given strings 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | 11 | class Solution 12 | { 13 | public: 14 | //Function to remove common characters and concatenate two strings. 15 | string concatenatedString(string s1, string s2) 16 | { 17 | set first,second; 18 | string ans; 19 | 20 | for(auto x:s1) first.insert(x); 21 | for(auto x:s2) second.insert(x); 22 | 23 | for(auto x:s1) 24 | if(second.count(x) == 0) ans += x; 25 | 26 | for(auto x:s2) 27 | if(first.count(x) == 0) ans += x; 28 | 29 | if(ans.size() == 0) return "-1"; 30 | 31 | return ans; 32 | } 33 | 34 | }; 35 | 36 | //{ Driver Code Starts. 37 | 38 | /* Driver program to test above function */ 39 | int main() 40 | { 41 | int t; 42 | cin >> t; 43 | 44 | while(t--){ 45 | string s1, s2; 46 | cin >> s1 >> s2; 47 | Solution obj; 48 | string res = obj.concatenatedString(s1, s2); 49 | cout< 2 | using namespace std; 3 | 4 | #define rep(i,a,b) for(int i = a; i < b ; i++) 5 | 6 | int main(){ 7 | string s; 8 | cin >> s; 9 | 10 | int n = s.size(); 11 | stack ans; 12 | 13 | rep(i,0,n){ 14 | if(ans.size()){ 15 | if(s[i] != ans.top()) 16 | ans.push(s[i]); 17 | else 18 | ans.pop(); 19 | } 20 | else 21 | ans.push(s[i]); 22 | 23 | } 24 | 25 | cout << s.size() << '\n'; 26 | } 27 | -------------------------------------------------------------------------------- /string algorithms/README.md: -------------------------------------------------------------------------------- 1 | [**Problem**](https://www.geeksforgeeks.org/shortest-string-possible-after-removal-of-all-pairs-of-similar-adjacent-characters/) 2 | > **You are given a string. You can remove any two identical adjacent characters. Now you have to tell the minimum length possible after performing unlimited operations?**

Solution given in the link has time complexity of O(n^2). But the solution given below has O(n) time complexity. 3 |
4 | 5 | [**Solution**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/string%20algorithms/remove%20duplicates.cpp) 6 | 7 |
8 | 9 | [**Problem**](https://practice.geeksforgeeks.org/problems/remove-common-characters-and-concatenate-1587115621/1) 10 | >Given two strings s1 and s2. Modify both the strings such that all the common characters of s1 and s2 are to be removed and the uncommon characters of s1 and s2 are to be concatenated. 11 | Note: If all characters are removed print -1. 12 | 13 | [**Solution**](https://github.com/khalid586/Competitive-programming-Topics/blob/main/string%20algorithms/remove%20common%20char%20%26%20concate.cpp) --------------------------------------------------------------------------------