diff --git a/Power_of_a_number_using_binary_exponentiation.cpp b/Power_of_a_number_using_binary_exponentiation.cpp new file mode 100644 index 0000000..a2e0639 --- /dev/null +++ b/Power_of_a_number_using_binary_exponentiation.cpp @@ -0,0 +1,36 @@ +#include +typedef long long int ll; +#define M 1000000007 +using namespace std; + +// Time Complexity-> O(Log(n)) +ll power(ll base, ll n, ll m) +{ + ll ans = 1; + base = base % m; + while (n != 0) + { + if (n % 2 == 1) + { + n = n - 1; + ans = (ans * base) % m; + } + else + { + n = n / 2; + base = (base * base) % m; + } + } + return ans; +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + cout << power(2, 10, M) << endl; + //(2^10)%M this will be calculated + // 1024 + return 0; +} \ No newline at end of file diff --git a/dsu.cpp b/dsu.cpp new file mode 100644 index 0000000..405d942 --- /dev/null +++ b/dsu.cpp @@ -0,0 +1,53 @@ +#include +typedef long long int ll; +#define M 1000000007 +using namespace std; + +// Program for disjoint set union + +ll parent[100005]; +ll sz[100005]; + +ll findParent(ll node) +{ + if (parent[node] == node) + { + return node; + } + return parent[node] = findParent(parent[node]); +} + +void uni(ll u, ll v) +{ + ll paru = findParent(u); + ll parv = findParent(v); + if (sz[paru] < sz[parv]) + { + parent[paru] = parv; + sz[parv] += sz[paru]; + } + else + { + parent[parv] = paru; + sz[paru] += sz[parv]; + } +} + +void init(ll n) +{ + for (ll i = 1; i <= n; i++) + { + parent[i] = i; + sz[i] = 1; + } +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + ll n; + cin >> n; + init(n); + return 0; +} \ No newline at end of file