Skip to content

Commit

Permalink
nt
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan2309 committed Jul 14, 2021
1 parent 4f5c4a6 commit 2acdf6b
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 2 deletions.
Binary file modified Number Theory/a.exe
Binary file not shown.
67 changes: 67 additions & 0 deletions Number Theory/binaryExp.cpp
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;
}
82 changes: 82 additions & 0 deletions Number Theory/pigeon.cpp
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;
}
4 changes: 2 additions & 2 deletions Number Theory/topic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
2. Scaline Algorithm
3. Sliding Window + Two Pointer Technique
4. Modular Arithmetic
5. Binary and Modular Exponentiation
5. Binary and Modular Exponentiation -> done
6. Euclid GCD -> done
7. Extended Euclid's Theorem -> done
8. Pigeonhole Principle
8. Pigeonhole Principle -> done
9. Prime sieve -> done

0 comments on commit 2acdf6b

Please sign in to comment.