Skip to content
70 changes: 70 additions & 0 deletions Arrays/Fast exponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
#define ull unsigned long long int
#define ll long long int
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<int,int>
#define vpii vector<pair<int,int>>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define dec(n,v) int n; cin>>n; vector<int> v(n);for(int i=0;i<n;i++)cin>>v[i];
#define decl(n,v) int n; cin>>n; vector<int> v(n+1);for(int i=1;i<=n;i++)cin>>v[i];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void c_a_j()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int FastExp(int a, int n)
{
if (n == 0)return 1;
int x = FastExp(a, n / 2); //subproblem

if (n & 1) // if n is odd
{
return a * x * x; //a^n = a*(a^n/2)^2;
}
return x * x; //a^n = (a^n/2)^2;
}
void solve()
{
int a, n;
cin >> a >> n;
cout << FastExp(a, n) << "\n";
}
int32_t main()
{
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif

FIO
c_a_j();

solve();
return 0;
}
113 changes: 113 additions & 0 deletions Arrays/memory_scene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define inf 1e18
#define all(v) v.begin(),v.end()
#define ps(x,y) fixed<<setprecision(y)<<x
#define w(x) int x; cin>>x; while(x--)
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

// for debug
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

//some maths
int M;
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline int lcm(int a, int b) { return (a * b) / gcd(a, b); }
inline int Mod(int x) { return (x % M + M) % M; }
inline int add_Mod(int a, int b) {return Mod(Mod(a) + Mod(b));}
inline int sub_Mod(int a, int b) {return Mod(Mod(a) - Mod(b) + M);}
inline int mul_Mod(int a, int b) {return Mod(Mod(a) * Mod(b));}
inline int FastModExp(int a, int b) {M = mod; int res = 1; while (b > 0) {if (b & 1)res = mul_Mod(res, a); a = mul_Mod(a, a); b = b >> 1;} return res;}
inline int mmi(int y) { return FastModExp(y, M - 2);}
inline int div_Mod(int a, int b) { return mul_Mod(a, mmi(b));}
inline void extendedEuclidMethod(int a, int b, int &x, int &y) { if (b == 0) {x = 1; y = 0; return;} extendedEuclidMethod(b, a % b, x, y); int cX = y; int cY = x - (a / b) * y; x = cX; y = cY;}

void c_a_j()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
}
bool check_prime(int n)
{
for (int i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)return false;
}
return true;
}
void solve()
{
int k; cin >> k;
string s; cin >> s;
for (int i = 0; i < k; i++)
{
if (s[i] == '1' || s[i] == '4' || s[i] == '6' || s[i] == '8' || s[i] == '9')
{
cout << 1 << endl;
cout << s[i] - '0' << endl;
return;
}
}

//now number only consists of digits 2,3,5,7 so checking all combinations
for (int i = 0; i < k; i++)
{
for (int j = i + 1; j < k; j++)
{
int x = s[i] - '0', y = s[j] - '0';
if (!check_prime(x * 10 + y))
{
cout << 2 << endl;
cout << x << y << endl;
return;
}
}
}
}
int32_t main()
{
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif

FIO
c_a_j();

w(x)solve();
return 0;
}
47 changes: 47 additions & 0 deletions BasicsofC/matrixmultiplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<bits/stdc++.h>
using namespace std;

void solve()
{
int n; cin >> n;
int arr1[n][n], arr2[n][n], res[n][n];
memset(res, 0, sizeof res);

//input part
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)cin >> arr1[i][j];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)cin >> arr2[i][j];
}

//initialisation
int i = 0, j = 0, k = 0;

//solving part
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
res[i][j] += arr1[i][k] * arr2[k][j];
}
}
}

//printing
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)cout << res[i][j] << " ";

cout << endl;
}
}
int main()
{
solve();
return 0;
}
106 changes: 106 additions & 0 deletions ExtraDSAQ/atm_students.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define inf 1e18
#define all(v) v.begin(),v.end()
#define ps(x,y) fixed<<setprecision(y)<<x
#define w(x) int x; cin>>x; while(x--)
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

// for debug
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

//some maths
int M;
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline int lcm(int a, int b) { return (a * b) / gcd(a, b); }
inline int Mod(int x) { return (x % M + M) % M; }
inline int add_Mod(int a, int b) {return Mod(Mod(a) + Mod(b));}
inline int sub_Mod(int a, int b) {return Mod(Mod(a) - Mod(b) + M);}
inline int mul_Mod(int a, int b) {return Mod(Mod(a) * Mod(b));}
inline int FastModExp(int a, int b) {M = mod; int res = 1; while (b > 0) {if (b & 1)res = mul_Mod(res, a); a = mul_Mod(a, a); b = b >> 1;} return res;}
inline int mmi(int y) { return FastModExp(y, M - 2);}
inline int div_Mod(int a, int b) { return mul_Mod(a, mmi(b));}
inline void extendedEuclidMethod(int a, int b, int &x, int &y) { if (b == 0) {x = 1; y = 0; return;} extendedEuclidMethod(b, a % b, x, y); int cX = y; int cY = x - (a / b) * y; x = cX; y = cY;}

void c_a_j()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
}
void solve() //sliding window approach
{
int n, s;
cin >> n >> s;
vector<int> v(n);
for (auto &x : v)cin >> x;
int last = 0;
int sum = 0, st = -1, end = -1, ans = 0;
for (int i = 0; i < n; i++)
{
sum += v[i];
while ( sum + s < 0)
{
sum -= v[last];
last++;
}

if ( ans < i - last + 1 )
{
ans = i - last + 1;
st = last + 1;
end = i + 1;
}
}

if (st == -1)
cout << "-1\n";
else
cout << st << " " << end << "\n";
}
int32_t main()
{
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif

FIO
c_a_j();

w(x)solve();
return 0;
}
40 changes: 40 additions & 0 deletions ExtraDSAQ/co_growing_sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define pii pair<int,int>

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int tt=1;
cin>>tt;
while(tt--)
{
int n;
cin>>n;
int a[n];
rep(i,0,n)
cin>>a[i];
cout<<"0 ";
int p=a[0];
rep(i,1,n)
{
int c=0;
rep(j,0,31)
{
int t = (1<<j);
if(p & t)
{
if(!(a[i]&t))
c|=t,a[i]|=t;
}
}
cout<<c<<" ";
p=a[i];
}
cout<<"\n";
}
}
Loading