Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicky-Jha authored Oct 6, 2020
1 parent 9fd1022 commit fc3fbee
Show file tree
Hide file tree
Showing 29 changed files with 1,543 additions and 0 deletions.
32 changes: 32 additions & 0 deletions %inverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
lli Power(lli a, lli n , lli mod)
{
lli res = 1;
while(n)
if(!(n & 1))
n /= 2, a = (a*a)%mod;
else
n--, res = (res*a)%mod;
return res;

}
lli ModularMultiplicativeInverse(lli a, lli n)
{
return Power(a,n-2,n);
}

int main()
{
lli a, n;
scanf("%lld%lld",&a,&n);

cout<<ModularMultiplicativeInverse(a,n)<<"\n";

return 0;
}
//modular multiplicative inverse of a under modulus n
//Modular inverse of a under modulo n exist iff GCD(a,n) = 1 // coprime
//Little fermat's theorem states that if p is prime number then p ^ (n-1) = 1
//under mods(n) // = here is sign of modular congruency
34 changes: 34 additions & 0 deletions Binary_exponentioation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int powerFunc(int a , int n)
{

int res = 1;
while(n)
{
if(!(n & 1))
{
n = n / 2;
a = ((a%mod)*(a%mod))%mod;
}
else
{
n--;
res = ((res%mod) *( a %mod))%mod;
}

}
return res%mod;

}

int main()
{
//freopen("input.txt" , "r", stdin);
//freopen("outpux.txt","w" , stdout);
int a,n;
cin>>a>>n;
cout<<powerFunc(a,n)<<"\n";
return 0;
}
48 changes: 48 additions & 0 deletions Binomial_CoefficientUsingModularInverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <bits/stdc++.h>
#define MAXN 1000
using namespace std;
int factorial[MAXN+1];
// this code can be used when finding combination of large numbers
unsigned long long power(unsigned long long a,
int n, int mod)
{
unsigned long long int res = 1;
while(n)
if(!(n & 1))
n /= 2, a = (a*a)%mod;
else
n--, res = (res*a)%mod;
return res;
}

unsigned long long modInverse(unsigned long long n, int p)
{
return power(n, p - 2, p);
}

unsigned long long nCrModPFermat(unsigned long long n,
int r, int p)
{

if (r == 0)
return 1;

return (factorial[n] * modInverse(factorial[r], p) % p * modInverse(factorial[n - r], p) % p) % p;
}

//nCr % p = (factorial[n]* modIverse(factorial[r]) % p *
//modIverse(factorial[n-r]) % p) % p;
int main()
{

int n = 10, r = 2, p = 13;
factorial[0] = 1;
for (int i = 1; i <= MAXN; i++) {
factorial[i] = factorial[i - 1] * i % p;
}
cout << "Value of nCr % p is "
<< nCrModPFermat(n, r, p)<<"\n";
return 0;
}
//Here p must always be prime number
//coz modular inverse exits if 2 numbers are coprime
19 changes: 19 additions & 0 deletions EuclidGCD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#include <iostream>
#define lli long long int
using namespace std;

lli gcd (lli A , lli B)
{
if(B == 0) return A;
else return gcd ( B , A%B);
}


int main()
{
lli a , b;
cin>>a>>b;
cout<<gcd(a,b)<<"\n";
return 0;
}
70 changes: 70 additions & 0 deletions GCDQueries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
lli pre[100005];
lli post[100005];
lli arr[100005];
lli gcd(lli A , lli B)
{
if(A == B || B == 0)
return A;
if(A == 0)
return B;
lli MIN,MAX;
if(A < B){
MIN = A;
MAX = B;
}
else {MIN = B;MAX=A;}
if(!(MAX % MIN))
return MIN;

lli check = 0;
for(lli i = 1 ; i*i<=MIN ; ++i)
if(!(MIN % i))
{
lli tmp = (MAX)%i;
if(!tmp)
check = max(check , i);
tmp = MAX%(MIN/i);
if(!tmp)
check = max(check , MIN/i);
}
return check;
}
void Pre(lli n)
{
pre[0] = 0;
for(lli i = 1 ; i<=n ; ++i)
pre[i] = gcd(pre[i-1],arr[i]);

}
void Post(lli n)
{
post[n+1] = 0;
for(lli i = n ; i>=1; --i)
post[i] = gcd(post[i+1],arr[i]);

}
int main()
{
lli t;
scanf("%lld",&t);
while(t--)
{
lli n,q;
scanf("%lld %lld",&n,&q);
for(lli i = 1 ; i<=n ; ++i)
scanf("%lld",&arr[i]);
Pre(n);
Post(n);
while(q--)
{
lli l ,r ;
scanf("%lld %lld", &l,&r);
printf("%lld\n",gcd(pre[l-1],post[r+1]));
}
}

return 0;
}
88 changes: 88 additions & 0 deletions GcdQuerie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <bits/stdc++.h>
#define lli long long int
using namespace std;

//c++ 14
#include <bits/stdc++.h>
#define lli long long int
using namespace std;



int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t,n,q,l,r;
cin>>t;

while( t --)
{
cin>>n>>q;

vector<int> arr(n);
for ( int i =0 ; i < n ; ++ i)
cin>>arr[i];
int pre[n+1] {0},suf[n+2] {0};

for ( int i = 0 ; i < n ; ++ i)
pre[i+1] = __gcd(arr[i],pre[i]);

int j = n+1;
for ( int i = n-1 ; i >= 0 ; -- i, --j)
suf[i+1] = __gcd(arr[i],suf[j]);

while(q --)
{
cin>>l>>r;
cout<<__gcd(pre[l-1], suf[r+1])<<"\n";
}
}

return 0;
}









//c++ 17
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t,n,q,l,r;
cin>>t;

while( t --)
{
cin>>n>>q;

vector<int> arr(n);
for ( int i =0 ; i < n ; ++ i)
cin>>arr[i];
vector<int> pre(n+1,0);
vector<int> suf(n+2,0);

for ( int i = 0 ; i < n ; ++ i)
pre[i+1] = gcd(arr[i],pre[i]);

int j = n+1;
for ( int i = n-1 ; i >= 0 ; -- i, --j)
suf[i+1] = gcd(arr[i],suf[j]);

while(q --)
{
cin>>l>>r;
cout<<gcd(pre[l-1], suf[r+1])<<"\n";
}
}

return 0;
}
35 changes: 35 additions & 0 deletions KthPrime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;
vector<int> primes;
void SieveOfEratosthenes(int n)
{

vector<bool> prime(n+1,1);

for (int p=2; p*p<=n; p++)
{

if (prime[p] == true)
{
for (int i=p*2; i<=n; i += p)
prime[i] = false;
}
}

for (int p=2; p<=n; p++)
if (prime[p])
primes.emplace_back(p);

}
int main() {
SieveOfEratosthenes(90000001);
int q,n;
cin>>q;
while(q--)
{
cin>>n;
cout<<primes[n-1]<<"\n";
}
return 0;
}

Loading

0 comments on commit fc3fbee

Please sign in to comment.