-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#19 Bit-manipulation in C++ #23
Open
AkshatGupta468
wants to merge
4
commits into
Synergise-IIT-Bhubaneswar:main
Choose a base branch
from
AkshatGupta468:Bitmanipulation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//Fenwick Tree(Binary Indexed Tree) | ||
//Make a structure to handle Dynamic Range queries | ||
#include<bits/stdc++.h> | ||
#define ll long long int | ||
using namespace std; | ||
/* | ||
LSB-Least Significant Bit | ||
it is the rightmost set Bit in the Binary representation of a number. | ||
|
||
One's complement: | ||
it is the value obtained by inverting all the zeros of binary representation of the number | ||
|
||
Two's complement: | ||
=One's complement + 1 | ||
|
||
How LSB can be found? | ||
LSB is the common set bit among the number and it's Two's complement. | ||
|
||
How to find Two's complement? | ||
Negative of a number is stored as Two's complement the number in computers. | ||
|
||
Hence LSB= num & (-num) | ||
*/ | ||
|
||
class Fenwick | ||
{ | ||
//ith element of the tree stores the sum of last LSB(i) indexes(including ith index) | ||
//where LSB(k) represents the Decimal equivalent of k | ||
vector<ll> tree; | ||
public: | ||
int n; | ||
vector<ll> arr; | ||
//constructor to recieve size of tree and the vector | ||
Fenwick(vector<ll> &tarr) | ||
{ | ||
n=tarr.size(); | ||
arr.clear(); | ||
|
||
arr.push_back(0); //Push zero to front of array to make it one based | ||
for(auto &i:tarr) | ||
{ | ||
arr.push_back(i); | ||
} | ||
|
||
//using one based indexing for tree | ||
tree.resize(n+1); | ||
build(); | ||
} | ||
|
||
//Build function to add the array values to the tree | ||
//Adding elements using update query for each index | ||
void build() | ||
{ | ||
tree[0]=0; | ||
for(int i=1;i<=n;i++) | ||
{ | ||
treeupdate_add(i,arr[i]); | ||
} | ||
} | ||
|
||
//Function to add val to xth index of arr | ||
//Pass the index and the value to be added | ||
//Time-complexity => O(log(n)) | ||
void treeupdate_add(int x,ll val) | ||
{ | ||
while(x<=n) | ||
{ | ||
tree[x]+=val; | ||
x+=(x&(-x)); //Next update occurs after LSB(x) indexes | ||
} | ||
} | ||
|
||
//sum_qurey gives the sum of first x indexes | ||
//Time-Complexity => O(log(n)) | ||
ll sum_query(int x) | ||
{ | ||
if(x==0) | ||
return 0; | ||
//Since tree[x] stores the sum of last LSB indexes | ||
//we make a recursive call of sum query of first x-LSB(x) indexes. | ||
return tree[x]+sum_query(x -(x&(-x))); | ||
} | ||
|
||
//Sum of range [l,r] can be calculated using sum_query(r)-sum_query(l-1) | ||
ll range_sum(int l,int r) | ||
{ | ||
return sum_query(r)-sum_query(l-1); | ||
} | ||
}; | ||
|
||
//Driver code | ||
int main() | ||
{ | ||
|
||
vector<ll> arr={4,3,2,5,1,6,7,8}; | ||
Fenwick f(arr); //passing the array in constructor | ||
//sum of range [3,6] | ||
cout<<f.range_sum(3,6)<<"\n"; | ||
|
||
f.treeupdate_add(4,6); | ||
|
||
cout<<f.range_sum(3,6)<<"\n"; | ||
|
||
// int n,q; | ||
// cin>>n>>q; | ||
// vector<ll> arr(n); | ||
// for(auto &i:arr) cin>>i; | ||
// Fenwick f(arr); | ||
// for(int i=0;i<q;i++) | ||
// { | ||
// int w; | ||
// cin>>w; | ||
// if(w==1) | ||
// { | ||
// int k,u; | ||
// cin>>k>>u; | ||
// f.treeupdate_add(k,u-arr[k-1]); | ||
// arr[k-1]=u; | ||
// } | ||
// else | ||
// { | ||
// int l,r; | ||
// cin>>l>>r; | ||
// cout<<f.range_sum(l,r)<<"\n"; | ||
// } | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
//Modular exponentation and Modular Multiplication | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
#define MOD 1000000007 | ||
|
||
/* | ||
Modular Exponentiation is used to calculate | ||
the value of (a^b)mod m | ||
|
||
A number can be written in its binary form as | ||
b=p[0]*2^0 + p[1]*2^1 + p[2]*2^2 ..... | ||
where p is the binary representation of b.(p[i] is ith bit from left) | ||
p[i] can be 0 or 1 | ||
so (a^b)=(a^(p[0]*2^0))*(a^(p[1]*2^1))*(a^(p[2]*2^2))*.... | ||
(a^b)%M=( | ||
( | ||
((a^(2^0))^p[0])%M | ||
* | ||
((a^(2^1))^p[1])%M | ||
)%M | ||
*((a^(2^2))^p[2])%M | ||
)%M*.... | ||
|
||
This expression is implemented to calculate the value of (a^b) mod M | ||
*/ | ||
/* | ||
Modular Multiplication is used to calculate | ||
value of (a*b)%M. | ||
|
||
When a and b are of order 10^9 this can be calculated just by (a*1ll*b)%M | ||
but when a*b exceeds the LONG_LONG_MAX limit then we have to use Modular multiplication to calculate (a*b)%M | ||
|
||
(a*b)=(a*(p[0]*2^0))+(a*(p[1]*2^1))+(a*(p[2]*2^2))+.... | ||
|
||
(a*b)%M=( | ||
( | ||
((a*(2^0))*p[0])%M | ||
+ | ||
((a*(2^1))*p[1])%M | ||
)%M | ||
+((a*(2^2))*p[2])%M | ||
)%M*.... | ||
|
||
This expression is implemented to calculate the value of (a*b) mod M | ||
|
||
*/ | ||
|
||
//Modular Exponentiation function such that 0<=a,b,M<=10^9 | ||
//Time-Complexity => O(log(b)) | ||
int ModularExp(int a,int b,int M) | ||
{ | ||
// base a | ||
//power b | ||
//modular M | ||
//(a^b)%M | ||
|
||
int ans=1; | ||
while(b>0) | ||
{ | ||
//Each time we check the rightmost bit of b and then we right-shift it. | ||
if(b&1) | ||
{ | ||
ans=(ans*1ll*a)%M; | ||
} | ||
//In the exxpression it can be seen that value of a gets squared in each step. | ||
a=(a*1ll*a)%M; | ||
b>>=1; | ||
} | ||
return ans; | ||
} | ||
|
||
//Modular Multiplication such that 0<=a,b,M<=10^18 | ||
//Time-Complexity O(log(b)) | ||
long long ModularMultiply_Big(long long a,long long b,long long M) | ||
{ | ||
// (a*b) mod M | ||
long long ans=0; | ||
while(b>0) | ||
{ | ||
if(b&1) | ||
{ | ||
ans=(ans + a)%M; | ||
} | ||
a=(a + a)%M; | ||
b>>=1; | ||
} | ||
return ans; | ||
} | ||
|
||
//Modular Exponentiation such that 0<=a,b,M<=10^18 | ||
//Time-Complexity => O(log(b)*log(M)) | ||
long long ModularExp_Big(long long a,long long b,long long M) | ||
{ | ||
long long ans=1; | ||
while(b>0) | ||
{ | ||
if(b&1) | ||
{ | ||
//Direct multiplication cannot be used as (ans*a) can exceed LONG_LONG_MAX | ||
ans=ModularMultiply_Big(ans,a,M); | ||
} | ||
//Direct multiplication cannot be used as (a*a) can exceed LONG_LONG_MAX | ||
a=ModularMultiply_Big(a,a,M); | ||
b>>=1; | ||
} | ||
return ans; | ||
} | ||
|
||
//Driver Code | ||
int main() | ||
{ | ||
cout<<ModularMultiply_Big(12233234124ll,1234122324ll,MOD); | ||
cout<<ModularExp_Big(1233444343434ll,123434343434ll,234234224)<<"\n"; | ||
cout<<ModularExp(1234,1234,2234224)<<"\n"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//Subset Generation using Bitmasks | ||
//Print all the subsets of a given array of n elements n<=20 | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long int | ||
|
||
/* | ||
Each element of set can be either chosen or not chosen so for each element we have 2 choices(to take or not take) | ||
hence for a set of n unique elements there are 2^n subsets that can be formed. | ||
|
||
Each subset can be represented by a binary representation where 1 represents element is taken and 0 represents element is not taken | ||
so by each integer's binary representation represents a unique subset of the original set. | ||
when the integer is from [0,2^n-1] | ||
*/ | ||
//Time-Compleity => O(n*(2^n)) //n is the size of original set | ||
vector<vector<int>> subsets_generate(vector<int>& arr) | ||
{ | ||
//here n is arr.size() | ||
long long sz=1<<arr.size(); //sz=2^n | ||
|
||
vector<vector<int>> subsets(sz); | ||
for(long long i=0;i<sz;i++) //iterating from 0 to 2^n-1 | ||
{ | ||
long long temp=1; | ||
//This loop check which bits are set in binary representation of i. | ||
for(int j=0;j<arr.size();j++,temp<<=1) | ||
{ | ||
if(i & temp) | ||
subsets[i].push_back(arr[j]); //for every set bit the element is pushed in the subset | ||
} | ||
} | ||
return subsets; | ||
} | ||
|
||
//Driver Code | ||
int main() | ||
{ | ||
vector<int> arr={3,4,7}; //Sample input array | ||
vector<vector<int>> subsets=subsets_generate(arr); | ||
//Printing each subset in each line | ||
for(auto &i:subsets) | ||
{ | ||
cout<<"{"; | ||
for(auto &j:i) | ||
{ | ||
cout<<j<<" "; | ||
} | ||
cout<<"}\n"; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the commented code and add the example along with the output at the end of each code