From 32641286dd4309966f061d887f6b4b8eda4e9094 Mon Sep 17 00:00:00 2001 From: Kushagra Nigam Date: Fri, 22 Mar 2019 13:44:59 +0530 Subject: [PATCH 1/6] Create Readme.md --- Misc./Readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc./Readme.md diff --git a/Misc./Readme.md b/Misc./Readme.md new file mode 100644 index 00000000..e08e777b --- /dev/null +++ b/Misc./Readme.md @@ -0,0 +1 @@ +These are some basic codes which does not fall under any category but you should look at them once. From dc0a79abf02f79cbf4dca1eaef6f388df746075b Mon Sep 17 00:00:00 2001 From: Kushagra Nigam Date: Fri, 22 Mar 2019 13:45:28 +0530 Subject: [PATCH 2/6] Add files via upload --- Misc./10ary_to_m-ary.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Misc./10ary_to_m-ary.cpp diff --git a/Misc./10ary_to_m-ary.cpp b/Misc./10ary_to_m-ary.cpp new file mode 100644 index 00000000..60c4914c --- /dev/null +++ b/Misc./10ary_to_m-ary.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +char a[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; + +string tenToM(int n, int m) +{ + int temp=n; + string result=""; + while (temp!=0) + { + result=a[temp%m]+result; + temp/=m; + } +return result; +} + +int main() +{ + int n = 15; + int m = 8; + string str = tenToM(n,m); + cout< Date: Fri, 22 Mar 2019 13:47:05 +0530 Subject: [PATCH 3/6] Update 10ary_to_m-ary.cpp --- Misc./10ary_to_m-ary.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc./10ary_to_m-ary.cpp b/Misc./10ary_to_m-ary.cpp index 60c4914c..2fbc672f 100644 --- a/Misc./10ary_to_m-ary.cpp +++ b/Misc./10ary_to_m-ary.cpp @@ -1,3 +1,4 @@ +//Converting decimal number to m-ary number. #include using namespace std; @@ -21,4 +22,4 @@ int main() int m = 8; string str = tenToM(n,m); cout< Date: Fri, 22 Mar 2019 13:54:05 +0530 Subject: [PATCH 4/6] Rename 10ary_to_m-ary.cpp to decimal_to_m-ary.cpp --- Misc./{10ary_to_m-ary.cpp => decimal_to_m-ary.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc./{10ary_to_m-ary.cpp => decimal_to_m-ary.cpp} (100%) diff --git a/Misc./10ary_to_m-ary.cpp b/Misc./decimal_to_m-ary.cpp similarity index 100% rename from Misc./10ary_to_m-ary.cpp rename to Misc./decimal_to_m-ary.cpp From 20007055dd69f4eab7707d1a6c1cda2263588219 Mon Sep 17 00:00:00 2001 From: Kushagra Nigam Date: Fri, 22 Mar 2019 13:54:51 +0530 Subject: [PATCH 5/6] Create m-ary_to_decimal.cpp --- Misc./m-ary_to_decimal.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Misc./m-ary_to_decimal.cpp diff --git a/Misc./m-ary_to_decimal.cpp b/Misc./m-ary_to_decimal.cpp new file mode 100644 index 00000000..f7adc561 --- /dev/null +++ b/Misc./m-ary_to_decimal.cpp @@ -0,0 +1,24 @@ +// Convert an m-ary number to decimal number. +#include +using namespace std; + +string num="0123456789ABCDEF"; +int mToTen(string n, int m) +{ + int multi=1; + int result=0; + for (int i=n.size()-1;i>=0;i--) + { + result+=num.find(n[i])*multi; + multi*=m; + } +return result; +} + +int main() +{ + string str = "FF"; + int m = 16; + int ans = mToTen(str,m); + cout< Date: Wed, 27 Mar 2019 02:18:05 +0530 Subject: [PATCH 6/6] Included Matrix Expo and Mo's Algorithm --- Matrix Expo/matrix-expo.cpp | 139 +++++++++++++++ Mo's Algorithm/Distinct_elements_in_range.cpp | 159 ++++++++++++++++++ Mo's Algorithm/Mo's.cpp | 97 +++++++++++ 3 files changed, 395 insertions(+) create mode 100644 Matrix Expo/matrix-expo.cpp create mode 100644 Mo's Algorithm/Distinct_elements_in_range.cpp create mode 100644 Mo's Algorithm/Mo's.cpp diff --git a/Matrix Expo/matrix-expo.cpp b/Matrix Expo/matrix-expo.cpp new file mode 100644 index 00000000..f9f1733a --- /dev/null +++ b/Matrix Expo/matrix-expo.cpp @@ -0,0 +1,139 @@ +/* +Matrix Exponentiation. If the problem can be solved with DP but constaints are high. + +ai = bi (for i <= k) +ai = c1*ai-1 + c2*ai-2 + ... + ck*ai-k (for i > k) + +Taking the example of Fibonacci series, K=2 +b1 = 0, b2=1 +c1 = 1, c2=1 + +a = 0 1 1 2 .... +This way you can find the 10^18 fibonacci number%MOD. + +I have given a general way to use it. The program takes the input of B and C matrix. +Steps for Matrix Expo +1. Create vector F1 : which is the copy of B. +2. Create transpose matrix (Learn more abput it on the internet) +3. Perform T^(n-1) [transpose matrix to the power n-1] +4. Multiply with F to get the last matrix of size (1xk). The first element of this matrix is the required result. + +*/ + + +#include +using namespace std; + +#define ll long long +#define ull unsigned long long +#define endl '\n' +#define pb push_back +#define mp make_pair +#define trace1(x) cout<<#x<<": "<0LL?x:-x;} +ll k; +vector a,b,c; + +//To multiply 2 matrix +vector > multiply(vector > A, vector > B) +{ + vector > C(k+1,vector(k+1)); + for(int i=1; i<=k; i++){ + for(int j=1; j<=k; j++){ + for(int z=1; z<=k; z++){ + C[i][j] = (C[i][j]+ (A[i][z]*B[z][j])%MOD)%MOD; + } + } + } + return C; +} + +//computing power of a matrix +vector > power(vector > A, ll p) +{ + if(p==1) + return A; + if(p%2==1) + return multiply(A,power(A,p-1)); + else{ + vector > X = power(A,p/2); + return multiply(X,X); + } + +} + +//main function +ll ans(ll n) +{ + if(n==0) + return 0; + if(n<=k) + return b[n-1]; + //F1 + vector F1(k+1); + for(int i=1; i<=k; i++) + F1[i]=b[i-1]; + + //Transpose matrix + vector > T(k+1,vector(k+1)); + for(int i=1; i<=k; i++){ + for(int j=1; j<=k; j++){ + if(i>t; + ll i,j,x; + while(t--) + { + cin>>k; + for(i=0; i>x; + b.pb(x); + } + for(i=0; i>x; + c.pb(x); + } + cin>>x; + cout< +using namespace std; + +// Used in frequency array (maximum value of an +// array element). +const int MAX = 1000000; + +// Variable to represent block size. This is made +// global so compare() of sort can use it. +int block; + +// Structure to represent a query range and to store +// index and result of a particular query range +struct Query { + int L, R, index, result; +}; + +// Function used to sort all queries so that all queries +// of same block are arranged together and within a block, +// queries are sorted in increasing order of R values. +bool compare(Query x, Query y) +{ + // Different blocks, sort by block. + if (x.L / block != y.L / block) + return x.L / block < y.L / block; + + // Same block, sort by R value + return x.R < y.R; +} + +// Function used to sort all queries in order of their +// index value so that results of queries can be printed +// in same order as of input +bool compare1(Query x, Query y) +{ + return x.index < y.index; +} + +// calculate distinct elements of all query ranges. +// m is number of queries n is size of array a[]. +void queryResults(int a[], int n, Query q[], int m) +{ + // Find block size + block = (int)sqrt(n); + + // Sort all queries so that queries of same + // blocks are arranged together. + sort(q, q + m, compare); + + // Initialize current L, current R and current + // different elements + int currL = 0, currR = 0; + int curr_Diff_elements = 0; + + // Initialize frequency array with 0 + int freq[MAX] = { 0 }; + + // Traverse through all queries + for (int i = 0; i < m; i++) { + + // L and R values of current range + int L = q[i].L, R = q[i].R; + + // Remove extra elements of previous range. + // For example if previous range is [0, 3] + // and current range is [2, 5], then a[0] + // and a[1] are subtracted + while (currL < L) { + + // element a[currL] is removed + freq[a[currL]]--; + if (freq[a[currL]] == 0) + curr_Diff_elements--; + + currL++; + } + + // Add Elements of current Range + // Note:- during addition of the left + // side elements we have to add currL-1 + // because currL is already in range + while (currL > L) { + freq[a[currL - 1]]++; + + // include a element if it occurs first time + if (freq[a[currL - 1]] == 1) + curr_Diff_elements++; + + currL--; + } + while (currR <= R) { + freq[a[currR]]++; + + // include a element if it occurs first time + if (freq[a[currR]] == 1) + curr_Diff_elements++; + + currR++; + } + + // Remove elements of previous range. For example + // when previous range is [0, 10] and current range + // is [3, 8], then a[9] and a[10] are subtracted + // Note:- Basically for a previous query L to R + // currL is L and currR is R+1. So during removal + // of currR remove currR-1 because currR was + // never included + while (currR > R + 1) { + + // element a[currL] is removed + freq[a[currR - 1]]--; + + // if ocurrence of a number is reduced + // to zero remove it from list of + // different elements + if (freq[a[currR - 1]] == 0) + curr_Diff_elements--; + + currR--; + } + q[i].result = curr_Diff_elements; + } +} + +// print the result of all range queries in +// initial order of queries +void printResults(Query q[], int m) +{ + sort(q, q + m, compare1); + for (int i = 0; i < m; i++) { + cout << "Number of different elements" << + " in range " << q[i].L << " to " + << q[i].R << " are " << q[i].result << endl; + } +} + +// Driver program +int main() +{ + int a[] = { 1, 1, 2, 1, 3, 4, 5, 2, 8 }; + int n = sizeof(a) / sizeof(a[0]); + Query q[] = { { 0, 4, 0, 0 }, { 1, 3, 1, 0 }, + { 2, 4, 2, 0 } }; + int m = sizeof(q) / sizeof(q[0]); + queryResults(a, n, q, m); + printResults(q, m); + return 0; +} diff --git a/Mo's Algorithm/Mo's.cpp b/Mo's Algorithm/Mo's.cpp new file mode 100644 index 00000000..cbea2ab5 --- /dev/null +++ b/Mo's Algorithm/Mo's.cpp @@ -0,0 +1,97 @@ +/* +To find the sum from L to R in an array(m-queries,n-size of array). The naive approac is O(mn) +However Mo's can be used when you know about the queries before hand. +This solves the problem in O((m+n)*root(n)) time. +*/ +#include +using namespace std; + +// Variable to represent block size. This is made global +int block; + +// Structure to represent a query range +struct Query +{ + int L, R; +}; + +// Function used to sort all queries so that all queries +// of the same block are arranged together and within a block, +// queries are sorted in increasing order of R values. +bool compare(Query x, Query y) +{ + // Different blocks, sort by block. + if (x.L/block != y.L/block) + return x.L/block < y.L/block; + + // Same block, sort by R value + return x.R < y.R; +} + +// Prints sum of all query ranges. m is number of queries +// n is size of array a[]. +void queryResults(int a[], int n, Query q[], int m) +{ + // Find block size + block = (int)sqrt(n); + + // Sort all queries so that queries of same blocks + // are arranged together. + sort(q, q + m, compare); + + // Initialize current L, current R and current sum + int currL = 0, currR = 0; + int currSum = 0; + + // Traverse through all queries + for (int i=0; i L) + { + currSum += a[currL-1]; + currL--; + } + while (currR <= R) + { + currSum += a[currR]; + currR++; + } + + // Remove elements of previous range. For example + // when previous range is [0, 10] and current range + // is [3, 8], then a[9] and a[10] are subtracted + while (currR > R+1) + { + currSum -= a[currR-1]; + currR--; + } + + // Print sum of current range + cout << "Sum of [" << L << ", " << R + << "] is " << currSum << endl; + } +} + +// Driver program +int main() +{ + int a[] = {1, 1, 2, 1, 3, 4, 5, 2, 8}; + int n = sizeof(a)/sizeof(a[0]); + Query q[] = {{0, 4}, {1, 3}, {2, 4}}; + int m = sizeof(q)/sizeof(q[0]); + queryResults(a, n, q, m); + return 0; +} \ No newline at end of file