From 98a632c944843f47be7ef4869f9d68ebec03780f Mon Sep 17 00:00:00 2001 From: Milan Pokharna Date: Sat, 24 Oct 2020 08:15:15 +0530 Subject: [PATCH] conceptual algorithms --- Fast_Exponentiation.cpp | 22 ++++++++++++ Modular Exponentation.cpp | 31 ++++++++++++++++ Segmented Sieve.cpp | 76 +++++++++++++++++++++++++++++++++++++++ linearRecurssion.cpp | 47 ++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 Fast_Exponentiation.cpp create mode 100644 Modular Exponentation.cpp create mode 100644 Segmented Sieve.cpp create mode 100644 linearRecurssion.cpp diff --git a/Fast_Exponentiation.cpp b/Fast_Exponentiation.cpp new file mode 100644 index 00000000..9ed8e9b6 --- /dev/null +++ b/Fast_Exponentiation.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +typedef long long int ll; + +ll fastExpo(int x,int n) +{ + if (n==0) return 1; + if (n & 1) + return fastExpo(x,n-1) * x; + int y = fastExpo(x,n/2); + return y*y; +} + +int main() +{ + int base,power; + cin>>base>>power; + cout< +using namespace std ; + +int power(int x, unsigned int y, int p) +{ + int res = 1; // Initialize result + + x = x % p; // Update x if it is more than or + // equal to p + + while (y > 0) + { + // If y is odd, multiply x with result + if (y & 1) + res = (res*x) % p; + + // y must be even now + y = y>>1; // y = y/2 + x = (x*x) % p; + } + return res; +} + +int main() +{ + int x = 2; + int y = 5; + int p = 13; + cout << "Power is " << power(x, y, p); + return 0; +} \ No newline at end of file diff --git a/Segmented Sieve.cpp b/Segmented Sieve.cpp new file mode 100644 index 00000000..c2001fe8 --- /dev/null +++ b/Segmented Sieve.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + + +void simpleSieve(int limit, vector &prime) +{ + bool mark[limit+1]; + memset(mark, true, sizeof(mark)); + + for (int p=2; p*p prime; + simpleSieve(limit, prime); + + int low = limit; + int high = 2*limit; + + while (low < n) + { + bool mark[limit+1]; + memset(mark, true, sizeof(mark)); + + for (int i = 0; i < prime.size(); i++) + { + int loLim = floor(low/prime[i]) * prime[i]; + if (loLim < low) + loLim += prime[i]; + + for (int j=loLim; j= n) high = n; + } +} + +// Driver program to test above function +int main() +{ + int n = 100; + cout << "Primes smaller than " << n << ":n"; + segmentedSieve(n); + return 0; +} diff --git a/linearRecurssion.cpp b/linearRecurssion.cpp new file mode 100644 index 00000000..8864e828 --- /dev/null +++ b/linearRecurssion.cpp @@ -0,0 +1,47 @@ +//Template for solving Linear Recurence Relations in LogN time by using fast exponentiation +#include +using namespace std; + +typedef long long ll; +typedef unsigned long long ull; +typedef unsigned long long int ulli; +#define flash ios_base::sync_with_stdio(false); cin.tie(NULL); +#define mat(x, y, name) vector< vector > name (x, vector(y)); +#define printMat(name) for (int i = 0; i < name.size(); i++) {for (int j = 0; j < res[i].size(); j++) cout << res[i][j] << " "; cout << endl;} + +#define MOD 98765431 + +vector< vector > matMul(vector< vector > A, vector< vector > B) +{ + vector< vector > C(A.size(), vector(B[0].size())); + for (int i = 0; i < A.size(); i++) + { + for (int j = 0; j < B[0].size(); j++) + { + C[i][j] = 0; + for (int k = 0; k < B.size(); k++) + C[i][j] = (C[i][j] + ((A[i][k] * B[k][j]) % MOD)) % MOD; + } + } + return C; +} + +vector< vector > matPow(vector< vector > A, int p) +{ + if (p == 1) + return A; + if (p&1) + return matMul(A, matPow(A, p-1)); + else + { + vector< vector > C = matPow(A, p/2); + return matMul(C, C); + } +} + +int main() +{ + flash; + + return 0; +}