Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
codersinghal authored Jan 13, 2020
1 parent 3e7ed8d commit e81336a
Show file tree
Hide file tree
Showing 26 changed files with 866 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Karatsuba.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Description: Karatsuba Algorithm (Fast polynomial multiplication)
* Usage: multiply O(N^1.583)
* Source: https://github.com/dragonslayerx
*/

#include <iostream>
#include <cstdio>
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;;
}
}
22 changes: 22 additions & 0 deletions Lucas.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
40 changes: 40 additions & 0 deletions all-pair-shortest-path.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
63 changes: 63 additions & 0 deletions bellman_ford.cpp
Original file line number Diff line number Diff line change
@@ -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<edges> 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;
}
}
25 changes: 25 additions & 0 deletions bitmask.cpp
Original file line number Diff line number Diff line change
@@ -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));
}
}
43 changes: 43 additions & 0 deletions dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -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<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
bool isvisited[MAX];

public:

void dijkstra(vector<vector<pair<int,int> > > &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<int, int> 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));
}
}
}
}
}
};
35 changes: 35 additions & 0 deletions dijsktra-dense.cpp
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
}
}
31 changes: 31 additions & 0 deletions divisorCount.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
14 changes: 14 additions & 0 deletions divisorsInRange.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit e81336a

Please sign in to comment.