From e81336a57cd5bb1948ff3dcbc610d1ec985a057f Mon Sep 17 00:00:00 2001 From: codersinghal <48293384+codersinghal@users.noreply.github.com> Date: Tue, 14 Jan 2020 01:01:34 +0530 Subject: [PATCH] Add files via upload --- Karatsuba.cpp | 76 ++++++++++++++++++++++++++++++++++++++ Lucas.cpp | 22 +++++++++++ all-pair-shortest-path.cpp | 40 ++++++++++++++++++++ bellman_ford.cpp | 63 +++++++++++++++++++++++++++++++ bitmask.cpp | 25 +++++++++++++ dijkstra.cpp | 43 +++++++++++++++++++++ dijsktra-dense.cpp | 35 ++++++++++++++++++ divisorCount.cpp | 31 ++++++++++++++++ divisorsInRange.cpp | 14 +++++++ eulerphi.cpp | 71 +++++++++++++++++++++++++++++++++++ fact.cpp | 30 +++++++++++++++ fast input.cpp | 55 +++++++++++++++++++++++++++ floyd_warshall.cpp | 17 +++++++++ gcd.cpp | 10 +++++ inttostring.cpp | 26 +++++++++++++ isConnected.cpp | 27 ++++++++++++++ monotonePriorityQueue.cpp | 17 +++++++++ power.cpp | 15 ++++++++ primality.cpp | 49 ++++++++++++++++++++++++ prime_sieve.cpp | 28 ++++++++++++++ scanline.cpp | 18 +++++++++ segmentedSieve.cpp | 29 +++++++++++++++ subset_generation.cpp | 14 +++++++ ternary.cpp | 24 ++++++++++++ toposort.cpp | 44 ++++++++++++++++++++++ treediameter.cpp | 43 +++++++++++++++++++++ 26 files changed, 866 insertions(+) create mode 100644 Karatsuba.cpp create mode 100644 Lucas.cpp create mode 100644 all-pair-shortest-path.cpp create mode 100644 bellman_ford.cpp create mode 100644 bitmask.cpp create mode 100644 dijkstra.cpp create mode 100644 dijsktra-dense.cpp create mode 100644 divisorCount.cpp create mode 100644 divisorsInRange.cpp create mode 100644 eulerphi.cpp create mode 100644 fact.cpp create mode 100644 fast input.cpp create mode 100644 floyd_warshall.cpp create mode 100644 gcd.cpp create mode 100644 inttostring.cpp create mode 100644 isConnected.cpp create mode 100644 monotonePriorityQueue.cpp create mode 100644 power.cpp create mode 100644 primality.cpp create mode 100644 prime_sieve.cpp create mode 100644 scanline.cpp create mode 100644 segmentedSieve.cpp create mode 100644 subset_generation.cpp create mode 100644 ternary.cpp create mode 100644 toposort.cpp create mode 100644 treediameter.cpp diff --git a/Karatsuba.cpp b/Karatsuba.cpp new file mode 100644 index 0000000..e866d46 --- /dev/null +++ b/Karatsuba.cpp @@ -0,0 +1,76 @@ +/** + * Description: Karatsuba Algorithm (Fast polynomial multiplication) + * Usage: multiply O(N^1.583) + * Source: https://github.com/dragonslayerx + */ + +#include +#include +using namespace std; + +#define MOD 99991 + +//m should be a power of 2 +class Karatsuba { + public: + + static void multiply(int *A, int *B, int *C, int lA, int rA, int lB, int rB){ + int m = rA-lA+1; + if (m == 1) { + C[0] = ((long long)A[lA]*B[lB]) % MOD; + return; + } + + int z0[m], z1[m], z2[m]; + + int midA = (lA + rA) >> 1; + int midB = (lB + rB) >> 1; + + multiply(A, B, z0, lA, midA, lB, midB); + multiply(A, B, z1, midA+1, rA, midB+1, rB); + + int a[m], b[m]; + int shift = m>>1; + int mid = m>>1; + for (int i = lA, j = 0; i <= midA; i++, j++) { + a[j] = A[i] + A[i+shift]; + if (a[j] >= MOD) a[j] -= MOD; + } + for (int i = lB, j = 0; i <= midB; i++, j++) { + b[j] = B[i] + B[i+shift]; + if (b[j] >= MOD) b[j] -= MOD; + } + multiply(a, b, z2, 0, mid-1, 0, mid-1); + + for (int i = 0; i <= m-2; i++) { + C[i] = z0[i]; + if (C[i] >= MOD) C[i] -= MOD; + } + C[m-1] = 0; + + shift = m; + for (int i = 0; i <= m-2; i++) { + C[i+shift] = z1[i]; + if (C[i+shift] >= MOD) C[i+shift] -= MOD; + } + + shift = m>>1; + for (int i = 0; i <= m-2; i++) { + C[i+shift] += (z2[i] + (MOD-z1[i]) + (MOD-z0[i])) % MOD; + if (C[i+shift] >= MOD) { + C[i+shift] -= MOD; + } + } + } +}; + +int main() +{ + int A[] = {1, 1, 1, 1}; + int B[] = {1, 1, 0, 0}; + int C[7] = {}; + Karatsuba::multiply(A, B, C, 0, 3, 0, 3); + for (int i = 0; i < 7; i++) { + cerr << C[i] << endl;; + } +} diff --git a/Lucas.cpp b/Lucas.cpp new file mode 100644 index 0000000..e18242c --- /dev/null +++ b/Lucas.cpp @@ -0,0 +1,22 @@ +long long Lucas_nCr(long long n, long long m){ + long long m0 = m % MOD; + long long m1 = m / MOD; + long long n0 = n % MOD; + long long n1 = n / MOD; + + long long answer = 1; + if (n0 >= m0) { + answer *= C(n0, m0); + answer %= MOD; + } else { + answer = 0; + } + if (n1 >= m1) { + answer *= C(n1, m1); + answer %= MOD; + } else { + answer = 0; + } + + return answer; +} \ No newline at end of file diff --git a/all-pair-shortest-path.cpp b/all-pair-shortest-path.cpp new file mode 100644 index 0000000..783fff3 --- /dev/null +++ b/all-pair-shortest-path.cpp @@ -0,0 +1,40 @@ +/** + * Description: All pair shortest paths (Returns a matrix B where B[i][j] is the M-length shortest path from vertex i to vertex j) + * Usage: getShortestPath O(N^3 log M) + * Source: https://github.com/dragonslayerx + */ + +// Set the w(u, v) = INF if no edge exists between u and v. + +const int MAX = 50; +const int INF = 1e9; + +void multiply(int A[][MAX], int B[][MAX], int n) { + int C[MAX][MAX]; + for(int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + C[i][j] =INF; + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + for (int k = 0; k < n; k++) { + if (A[i][k] != INF && B[k][j] != INF) { + C[i][j] = min(C[i][j], A[i][k] + B[k][j]); + } + } + ` } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + A[i][j] = C[i][j]; + } + } +} + +void getShotestPath(int A[][MAX], int B[][MAX], int n, int m) { + if (m == 1)return; + getShotestPath(A, B, n, m/2); + multiply(A, A, n); + if (m & 1) multiply(A, B, n); +} \ No newline at end of file diff --git a/bellman_ford.cpp b/bellman_ford.cpp new file mode 100644 index 0000000..0e22b2e --- /dev/null +++ b/bellman_ford.cpp @@ -0,0 +1,63 @@ +/** + * Descrption: BellmanFord (Finds the shortest path from source s to all vertices v. Detects a negative weight cycle if present.) + * Usage: See below O(V E) + * Source: https://github.com/dragonslayerx + */ + +struct edges { + int u; + int v; + long long w; + edges(int u, int v, long long w): u(u), v(v), w(w) {} +}; + +int main(){ + int n, m; + scanf("%d %d", &n, &m); + + vector edge; + + for (int i = 0; i < m; i++) { + int a, b; + long long w; + scanf("%d%d%lld", &a, &b, &w); + edge.push_back(edges(a, b, w)); + } + + int parent[MAX]; + long long dist[MAX]; + for (int i = 0; i < n; i++) { + parent[i] = 0; + dist[i] = INF; + } + + dist[0] = 0; + for (int i = 0; i < n-1; i++) { + for (int j = 0; j < edge.size(); j++) { + int u = edge[j].u; + int v = edge[j].v; + long long w = edge[j].w; + if (dist[u] != INF) { + if (dist[v] > dist[u] + w){ + dist[v] = dist[u] + w; + parent[v] = u; + } + } + } + } + + bool negCycleExists = false; + for (int j = 0; j < edge.size(); j++) { + int u = edge[j].u; + int v = edge[j].v; + long long w = edge[j].w; + if (dist[v] > (dist[u] + w)) { + negCycleExists = true; + break; + } + } + + for (int i = 0; i < n; i++) { + cout << i << " " << dist[i] << endl; + } +} diff --git a/bitmask.cpp b/bitmask.cpp new file mode 100644 index 0000000..2ec6b30 --- /dev/null +++ b/bitmask.cpp @@ -0,0 +1,25 @@ +/** + * Description: Bitmask (Support set, unset and get bit operation) + * Usage: set O(1), unset O(1), get O(1) + * Source: https://github.com/dragonslayerx + */ + +class Bitmask { + int mask; + public: + Bitmask() { + mask = 0; + } + + void set(int i) { + mask |= (1 << i); + } + + void unset(int i) { + mask &= ~(1 << i); + } + + int get(int i) { + return (mask & (1 << i)); + } +} \ No newline at end of file diff --git a/dijkstra.cpp b/dijkstra.cpp new file mode 100644 index 0000000..ebaeeba --- /dev/null +++ b/dijkstra.cpp @@ -0,0 +1,43 @@ +/** + * Description: Dijkstra (Find shortest path from single source) + * Usage: dijkstra O((V + E) lg(V)) + * Source: https://github.com/dragonslayerx + */ + +class Dijkstra { + static const int MAX = 100050; + static const int INF = 1e9; + + priority_queue, vector >, greater > > pq; + bool isvisited[MAX]; + + public: + + void dijkstra(vector > > &G, int v, int e, int s, int dist[]) + { + for (int i = 0; i < v; i++) { + dist[i] = INF; + isvisited[i] = false; + } + dist[s] = 0; + pq.push(make_pair(0, s)); + while (!pq.empty()){ + pair tp = pq.top(); + pq.pop(); + int node = tp.second; + int d = tp.first; + if (isvisited[node]) continue; + else { + isvisited[node] = true; + for (int i = 0; i < G[node].size(); i++) { + int v = G[node][i].first; + int w = G[node][i].second; + if (dist[v] > d + w) { + dist[v] = d + w; + pq.push(make_pair(dist[v], v)); + } + } + } + } + } +}; diff --git a/dijsktra-dense.cpp b/dijsktra-dense.cpp new file mode 100644 index 0000000..3767893 --- /dev/null +++ b/dijsktra-dense.cpp @@ -0,0 +1,35 @@ +/** + * Description: Dijkstra (Find shortest path from single source in dense graph) + * Usage: dijkstra O(V^2) + * Source: https://github.com/dragonslayerx + */ + +const int MAX = 1005 +const int INF = 1e9 + +void dijkstra(int v, int source, int path_estimate[], int W[][MAX]) { + bool isvisited[MAX]; + for (int i = 0; i < v; i++) { + isvisited[i] = false; + path_estimate[i] = INF; + } + + path_estimate[source] = 0; + for (int i = 0; i < v; i++) { + int mindist = INF, vertex; + for (int j = 0; j < v; j++) { + if (!isvisited[j] && mindist > path_estimate[j]) { + mindist = path_estimate[j]; + vertex = j; + } + } + isvisited[vertex] = true; + for (int i = 0; i < v; i++) { + if (!isvisited[i]) { + if (path_estimate[i] > path_estimate[vertex] + W[vertex][i]) { + path_estimate[i] = path_estimate[vertex] + W[vertex][i]; + } + } + } + } +} diff --git a/divisorCount.cpp b/divisorCount.cpp new file mode 100644 index 0000000..78c69ac --- /dev/null +++ b/divisorCount.cpp @@ -0,0 +1,31 @@ +/** + * Description: No of prime and distinct prime divisors. + * Eg. 12 is 2*2*3. It has 3 prime factors but 2 distinct prime factors. + * Usage: sieve O(Nlog(N)) + * Source: https://github.com/dragonslayerx + */ + +long long noPrimeDivisors[5000100]; +long long noDistinctPrimeDivisors[5000100]; +bool isPrime[5000100]; + +void sieve(int n){ + for (int i = 1; i <= n; i++) { + isPrime[i] = true; + } + isPrime[0] = isPrime[1] = false; + for (int i = 2; i <= n; i++) { + if (isPrime[i] || (noDistinctPrimeDivisors[i] == 1)) { + for (int j = i; j <= n; j+=i) { + ++noPrimeDivisors[j]; + if (isPrime[i]) { + ++noDistinctPrimeDivisors[j]; + } + if (j > i) { + isPrime[j] = false; + } + } + } + } + return; +} diff --git a/divisorsInRange.cpp b/divisorsInRange.cpp new file mode 100644 index 0000000..4b83f0f --- /dev/null +++ b/divisorsInRange.cpp @@ -0,0 +1,14 @@ +/** + * Description: Finds no of divisors of p from l to r + * Usage: getCount O(1) + * Source: https://github.com/dragonslayerx + */ + +ll getCount(ll l, ll r, ll p) { + ll start = l/p + ((l%p)? 1: 0); + ll end = r/p; + if (end < start) + return 0; + else + return (end-start+1); +} \ No newline at end of file diff --git a/eulerphi.cpp b/eulerphi.cpp new file mode 100644 index 0000000..7ff1be4 --- /dev/null +++ b/eulerphi.cpp @@ -0,0 +1,71 @@ +/** + * Description: Euler Totient function + * Usage: See below + * Note: The code is taken from http://www.geeksforgeeks.org/eulers-totient-function/ + * Source: https://github.com/dragonslayerx + */ + +#include +#include +#include + +using namespace std; + +vector isprime; +vector primes; + +void sieve(int n){ + isprime.resize(n, 1); + for (int i = 2; i < n; i++) { + if (isprime[i]) { + for (int j = 2; i*j < n; j++) { + isprime[i*j] = 0; + } + } + } + for (int i = 2; i < n; i++) + if (isprime[i])primes.push_back(i); +} + +int phi(const int n){ + if ( n < 2 ) + return 0; + if (isprime[n] ) + return n-1; + if ( n & 1 == 0 ) { + int m = n >> 1; + return !(m & 1) ? phi(m)<<1 : phi(m); + } + for ( std::vector::iterator p = primes.begin(); p != primes.end() && *p <= n; ++p ){ + int m = *p; + if ( n % m ) continue; + + // phi is multiplicative + int o = n/m; + int d = __gcd(m, o); + return d==1? phi(m)*phi(o) : phi(m)*phi(o)*d/phi(d); + } +} + +int main() +{ + sieve(1000005); + vector ephi(1000005); + for (int i = 1; i <= 1000005; i++) + ephi[i] = phi(i); + + ios::sync_with_stdio(false); + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + if (m > n) {cout << 0 << endl; continue;} + int k = n/m; + long long answer = 0; + for (int i = 1; i <= k; i++) { + answer += ephi[i]; + } + cout << answer + 1 << endl; + } +} diff --git a/fact.cpp b/fact.cpp new file mode 100644 index 0000000..1579bab --- /dev/null +++ b/fact.cpp @@ -0,0 +1,30 @@ +/** + * Description: Preprocessing for finding factorial and inverse factorial. Support efficient binomial coefficient, Catalan no. calculation. + * Complexity: initfact O(N), + * Source: https://github.com/dragonslayerx + */ + + +#define MOD 1000000007 +#define MAX 2000010 +long long fact[MAX]; +long long ifact[MAX]; +long long power(long long n,int m){ + if(m==0) return 1; + long long x=power(n,m/2); + if(m%2==0) return (x*x)%MOD; + else return (((x*x)%MOD)*n)%MOD; +} + +void initfact(int n,long long fact[],long long ifact[]){ + int i; + fact[0]=1; + for(i=1;i<=n;i++) + fact[i] = i*fact[i-1]%MOD; + ifact[n] = power(fact[n],MOD-2); + for(i=n;i>0;i--) + ifact[i-1] = ifact[i]*i%MOD; +} + +#define C(n, r) ((((fact[n]*ifact[r])%MOD)*ifact[n - r])%MOD) +#define Catalan(n) ((((fact[2*n]*ifact[n])%MOD)*ifact[n+1])%MOD) diff --git a/fast input.cpp b/fast input.cpp new file mode 100644 index 0000000..f7ae9e9 --- /dev/null +++ b/fast input.cpp @@ -0,0 +1,55 @@ +/** + * Description: Fast Input & Output + * Usage: readInt, writeInt + * Source: https://github.com/dragonslayerx + */ + +#ifdef ONLINE_JUDGE + #define GETCHAR getchar_unlocked + #define PUTCHAR putchar_unlocked +#endif +#ifndef ONLINE_JUDGE + #define GETCHAR getchar + #define PUTCHAR putchar +#endif + +template +inline void readInt(T &n){ + n = 0; + bool flag=1; + char c; + int sign=1; + while (1){ + c = GETCHAR(); + if(c=='-') sign=-1; + else if(c>='0'&&c<='9') {n = n * 10 + c - '0';flag=0;} + else if(flag!=1) break; + } + n *= sign; +} + + +template +inline void writeInt(T x) { + bool sign = false; + if (x < 0) { + x = -x; + } + bool flag = false; + char buf[20]; + int i = 0; + while (x > 0) { + flag = true; + buf[i++] = x % 10 + '0'; + x /= 10; + } + if (flag) { + if (sign) PUTCHAR('-'); + i--; + for (; i >=0; i--) { + PUTCHAR(buf[i]); + } + } else { + PUTCHAR('0'); + } +} \ No newline at end of file diff --git a/floyd_warshall.cpp b/floyd_warshall.cpp new file mode 100644 index 0000000..d3c0ee1 --- /dev/null +++ b/floyd_warshall.cpp @@ -0,0 +1,17 @@ +/** + * Description: Floyd Warshall (Find the all pair shortest path distances) + * Usage: See below O(N^3) + * Source: https://github.com/dragonslayerx + */ + +for (int k = 0; k < n; k++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (dist[i][k] != INF and dist[k][j] != INF) { + if (dist[i][j] > dist[i][k] + dist[k][j]) { + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + } + } diff --git a/gcd.cpp b/gcd.cpp new file mode 100644 index 0000000..dc18698 --- /dev/null +++ b/gcd.cpp @@ -0,0 +1,10 @@ +/** + * Description: Finds GCD of two integers + * Source: https://github.com/dragonslayerx + */ + +long long gcd (long long a, long long b) { + if (b==0) return a; + else return gcd(b,a%b); +} + diff --git a/inttostring.cpp b/inttostring.cpp new file mode 100644 index 0000000..6f1410a --- /dev/null +++ b/inttostring.cpp @@ -0,0 +1,26 @@ +/** + * Description: Converts integer to string and vice versa + * Usage: toString (Converts an integer to its string representation) + * toValue (Converts a string to its interger representation) + * Source: https://github.com/dragonslayerx + */ + +template +string toString(T x){ + string s; + while (x) { + s.push_back(x % 10 + '0'); + x /= 10; + } + reverse(s.begin(), s.end()); + return s; +} + +long long toValue(string &x){ + long long sum = 0; + for (int i = 0; i < x.size(); i++) { + sum *= 10; + sum += (x[i]-'0'); + } + return sum; +} \ No newline at end of file diff --git a/isConnected.cpp b/isConnected.cpp new file mode 100644 index 0000000..445cd58 --- /dev/null +++ b/isConnected.cpp @@ -0,0 +1,27 @@ +/** + * Description: BFS (Checks if graph is connected using BFS) + * Usgae: isConnected O(V + E) + * Source: https://github.com/dragonslayerx + */ + +int isConnected(vector > &G, int src){ + bool isVisited[MAX] = {false}; + queue Q; + Q.push(src); + while (Q.size()) { + int u = Q.front(); + Q.pop(); + isVisited[u] = true; + for (int v : G[u]) { + if (!isVisited[v]) { + Q.push(v); + } + } + } + for (int i = 0; i < G.size(); i++) { + if (isVisited[i] == false) { + return false; + } + } + return true; +} diff --git a/monotonePriorityQueue.cpp b/monotonePriorityQueue.cpp new file mode 100644 index 0000000..ded97cf --- /dev/null +++ b/monotonePriorityQueue.cpp @@ -0,0 +1,17 @@ +/** + * Description: Monotone Priority Queues (Supports sliding window queries) + * Usage: pushmax, pushmin, O(N) + * Source: https://github.com/dragonslayerx + */ + +deque maxq, minq; +void pushmax(int element) { + while (!maxq.empty() && element > maxq.back()) maxq.pop_back(); + maxq.push_back(element); +} + + +void pushmin(int element) { + while (!minq.empty() && element < minq.back()) minq.pop_back(); + minq.push_back(element); +} diff --git a/power.cpp b/power.cpp new file mode 100644 index 0000000..66ab725 --- /dev/null +++ b/power.cpp @@ -0,0 +1,15 @@ +/** + * Description: Calculates n^m (Binary exponentiation) + * Usage: power O(lg(M)) + * Source: https://github.com/dragonslayerx + */ + +#define MOD 1000000007 +long long power(long long n, long long m) +{ + if (m == 0) return 1; + long long x = power(n, m / 2); + if (!(m & 1)) return (x * x) % MOD; + else return (((x * x) % MOD) * n) % MOD; +} + diff --git a/primality.cpp b/primality.cpp new file mode 100644 index 0000000..686c08d --- /dev/null +++ b/primality.cpp @@ -0,0 +1,49 @@ +/** + * Description: Fermats Primality Testing + * Usage: fermat + * Note: increating the no of iterations in fermat function improves accuracy (See Carmichael numbers). Usually I keep it 50. + * Source: https://github.com/dragonslayerx + */ + +long long mul(long long a,long long b,long long MOD){ + long long a_high = a/1000000000; + long long a_low = a%1000000000; + + long long b_high = b/1000000000; + long long b_low = b%1000000000; + + long long result = (a_high*b_high)%MOD; + for(int i=0;i<9;i++){ + result=(result*10)%MOD; + } + result=(result+a_high*b_low+a_low*b_high)%MOD; + for(int i=0;i<9;i++){ + result=(result*10)%MOD; + } + result=(result+a_low*b_low)%MOD; + return result; +} + +long long p(long long a,long long b,long long MOD){ + if(b==0) return 1; + long long x=p(a,b/2,MOD); + if((b&1)==0) { + return mul(x,x,MOD); + } else { + return mul(mul(x,x,MOD),a,MOD); + } +} + +bool fermat(long long num,int iterations){ + if(num==1) { + return false; + } else if(num==2) { + return true; + } else { + for(int i=0;i isprime; +vector primes; +void sieve(int n) { + isprime.resize(n + 1); + for (int i = 0; i <= n; i++) { + isprime[i] = true; + } + isprime[1] = false; isprime[2] = true; + for (int i = 2; i <= n; i++) { + if (isprime[i]) { + for (int j = 2*i; j < n; j+=i) { + isprime[j] = false; + } + } + } + for (int i = 2; i < n; i++) { + if (isprime[i]) { + primes.push_back(i); + } + } + return; +} diff --git a/scanline.cpp b/scanline.cpp new file mode 100644 index 0000000..685fc7f --- /dev/null +++ b/scanline.cpp @@ -0,0 +1,18 @@ +/** + * Description: Scanline (Merge all overalapping intervals into a single interval) + * Usage: O(N) + * Source: https://github.com/dragonslayerx + */ + +void scanline(vii p, vii &interval) { + int beg = p[0].first, last = p[0].second; + for (int i = 1; i < m; i++) { + if (p[i].first <= last) + last = max(last, p[i].second); + else { + interval.pb(make_pair(beg, last)); + beg = p[i].first, last = p[i].second; + } + } + interval.pb(make_pair(beg, last)); +} diff --git a/segmentedSieve.cpp b/segmentedSieve.cpp new file mode 100644 index 0000000..c850b9d --- /dev/null +++ b/segmentedSieve.cpp @@ -0,0 +1,29 @@ +/** + * Description: Get primes in range [ll, ul]. + * Usage: getPrimes. O(NlgN) where N = ul-ll+1 + * Source: https://github.com/dragonslayerx + */ + +void getPrimes(int ll, int ul, set &largePrimes) { + vector isprm; + isprm.resize(ul - ll + 1); + for(int i = 0; i < ul - ll + 1; i++) { + isprm[i] = 1; + } + for (int i = 2; i * i <= ul; i++) { + if (isprime[i]) { + int j; + j = ll / i; + for(; i * j <= ul; j++) { + if (i * j >= ll && j > 1) { + isprm[i * j - ll] = 0; + } + } + } + } + for (int i = ll; i <= ul; i++) { + if (isprm[i - ll] && i > 1) { + largePrimes.insert(i); + } + } +} \ No newline at end of file diff --git a/subset_generation.cpp b/subset_generation.cpp new file mode 100644 index 0000000..638a1ef --- /dev/null +++ b/subset_generation.cpp @@ -0,0 +1,14 @@ +/** + * Description: Subset generation + * Usage: See below. O(2^n) + * Source: https://github.com/dragonslayerx + */ + +int size = a.size(); +int ways = 1 << size; +for (int i = 1; i < ways; i++) { + vector s; + for (int j = 0; j < a.size(); j++) + if (i & (1 << j)) + s.push_back(a[j]); +} diff --git a/ternary.cpp b/ternary.cpp new file mode 100644 index 0000000..1ea1b6b --- /dev/null +++ b/ternary.cpp @@ -0,0 +1,24 @@ +/** + * Description: Ternary search + * Usage: getMax O(lg(N)) + * Note: The function f should be strictly increasing and then strictly decreasing. + * See http://codeforces.com/blog/entry/11497 for more help. + * Source: https://github.com/dragonslayerx + */ + +ll f(int mid) { + +} + +ll getMax(int ll, int rr) { + int lo = ll, hi = rr; + while(lo < hi) { + int mid = (lo + hi) >> 1; + if(f(mid) > f(mid+1)) { + hi = mid; + } else { + lo = mid+1; + } + } + return lo+1; +} diff --git a/toposort.cpp b/toposort.cpp new file mode 100644 index 0000000..e306fde --- /dev/null +++ b/toposort.cpp @@ -0,0 +1,44 @@ +/** + * Description: Topological sorting + * Usage: See below O(V + E) + * Source: https://github.com/dragonslayerx + */ + +const int MAX = 10050; + +bool isvisited[MAX]; +vector s; +vector > g; + +void dfs(int u) +{ + isvisited[u] = 1; + for (int i = 0; i < g[u].size(); i++) { + int v = g[u][i]; + if (!isvisited[v]) { + dfs(v); + } + } + s.push_back(u); +} + +int main() +{ + int n, m; + cin >> n >> m; + g.resize(n); + for (int i = 0; i < m; i++) { + int a, b; + cin >> a >> b; + a--, b--; + g[a].push_back(b); + } + for (int i = 0; i < n; i++) { + if (!isvisited[i]) dfs(i); + } + reverse(s.begin(), s.end()); + for (int i = 0; i < s.size(); i++) { + cout << s[i]+1 << " "; + } + cout << endl; +} diff --git a/treediameter.cpp b/treediameter.cpp new file mode 100644 index 0000000..ed6927f --- /dev/null +++ b/treediameter.cpp @@ -0,0 +1,43 @@ +/** + * Description: Find the diameter of the tree. + * Usage: getDiameter O(V + E) + * Source: https://github.com/dragonslayerx + */ + +typedef vector > > tree; +void dfs(tree &g, int u, int d, bool isVisited[], int dt[]){ + isVisited[u] = 1; + dt[u] = d; + for (int i = 0; i < g[u].size(); i++) { + int v = g[u][i].first; + if (!isVisited[v]) { + dfs(g, v, d + g[u][i].second, isVisited, dt); + } + } +} + +const int MAX = 100005; +bool isVisited[MAX]; +int dt[MAX]; + +int getDiameter(tree &g) { + memset(dt, 0, sizeof(dt)); + memset(isVisited, 0, sizeof(isVisited)); + dfs(g, 0, 0, isVisited, dt); + int max_dist = 0, max_pos = 0; + int n = g.size(); + for (int i = 0; i < n; i++) { + if (max_dist < dt[i]) { + max_dist = dt[i]; + max_pos = i; + } + } + memset(dt, 0, sizeof(dt)); + memset(isVisited, 0, sizeof(isVisited)); + dfs(g, max_pos, 0, isVisited, dt); + max_dist = 0; + for (int i = 0; i < n; i++) { + max_dist = max(max_dist, dt[i]); + } + return max_dist; +}