-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
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,67 @@ | ||
/* | ||
*Binary Exponentiation* | ||
(a^b)%m sometimes becomes out of range | ||
properties of modulo | ||
(a+b)%m = (a%m) + (b%m) | ||
(a*b)%m = (a%m) * (b%m) | ||
(a-b)%m = (a%m) - (b%m) | ||
(a/b)%m = (a%m) * (b^-1%m) | ||
recursive case | ||
a^n | ||
when n is odd | ||
a^n = a^n/2 * a^n/2 * a | ||
when a is even | ||
a^n = a^n/2 * a^n/2 | ||
base case | ||
n=0 then return 1 | ||
iterative | ||
a^45 | ||
45 = 1x2^5 + 0x2^4 + 1x2^3 + 1x2^2 + 0x2^1 + 1x2^0 | ||
45 = (101101)2 | ||
7^45 can be calculated easily | ||
2^3 = 2^2 + 2^2 | ||
start with var x = 1 | ||
keep multiplying it with itself | ||
when there is 1 in binary rep | ||
multiply it with ans | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
const int N = 1e5 + 2, MOD = 1e9 + 7; | ||
|
||
int power(int a, int n) | ||
{ | ||
|
||
if (n == 0) | ||
{ | ||
return 1; | ||
} | ||
int p = (power(a, n / 2) % MOD); | ||
|
||
if (n & 1) //if n is odd | ||
{ | ||
return (((p * p) % MOD) * a) % MOD; | ||
} | ||
else | ||
{ | ||
return (p * p) % MOD; | ||
} | ||
} | ||
|
||
signed main() | ||
{ | ||
int a, n; | ||
cin >> a >> n; | ||
a %= MOD; | ||
cout << power(a, n) << endl; | ||
return 0; | ||
} |
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,82 @@ | ||
/* | ||
Divisible Sub-arrays | ||
*Pigeonhole Principle* | ||
array containing N elements | ||
we have to find number of good sub arrays | ||
sub array whose sum is divisible by N | ||
n no of holes 4 | ||
and n+1 pigeons 3 | ||
allocate pigeons in holes | ||
1 hole will have 2 pigeons | ||
cumulative sum array -> will contain sum upto particular index | ||
1, (3, 2,) 6, 4 | ||
a b | ||
total sum upto this point is b | ||
and total sum upto this point is a | ||
sum of sub array will be b-a | ||
(b-a)%N = 0 | ||
b%N = a%N | ||
1, 3, 2, 6, 4 -> original array | ||
0, 1, 4, 6,12,16 -> sum upto a point | ||
0, 1, 4, 1, 2, 1 -> sum%n (cumulative sum array) 6 diff values after taking mod 5 | ||
x % 5 -> max 5 unique values | ||
atleast 2 boxes will have same value | ||
buckets | ||
0->1 | ||
1->3 if we take any two pos we will get the same sum -> mC2 sub arrays | ||
2->1 | ||
3->0 | ||
4->1 | ||
we will add whereever we have atleast 2 freq | ||
and our ans will be 3c2 as m =3 | ||
ans+= fc2 | ||
where f>=2 | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long //constraints are long multiplying 2 numbers which are 10^9 | ||
|
||
ll a[1000005]; //array | ||
ll pre[1000005]; //frequency array | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
memset(pre, 0, sizeof(pre)); //initializing every bucket with 0 | ||
|
||
pre[0] = 1; //freq of 0 will be 1 because of null sub array | ||
|
||
//read input | ||
int sum = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> a[i]; //input array element | ||
sum += a[i]; //checking sum till that point | ||
sum %= n; //mod with n is cumulative sum | ||
sum = (sum + n) % n; //for negative case | ||
pre[sum]++; //increasing frequency of that number (creating freq array) | ||
} | ||
|
||
ll ans = 0; | ||
for (int i = 0; i < n; i++) //going to all positions | ||
{ | ||
ll m = pre[i]; | ||
ans += (m) * (m - 1) / 2; //nC2 | ||
} | ||
cout << ans << endl; | ||
} | ||
return 0; | ||
} |
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