From d3fccab653202f46534486daa40074debab97e20 Mon Sep 17 00:00:00 2001 From: Aryan-Jha29 <68564456+Aryan-Jha29@users.noreply.github.com> Date: Thu, 14 Oct 2021 11:07:33 +0530 Subject: [PATCH 01/10] Create matrixmultiplication.cpp --- BasicsofC/matrixmultiplication.cpp | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 BasicsofC/matrixmultiplication.cpp diff --git a/BasicsofC/matrixmultiplication.cpp b/BasicsofC/matrixmultiplication.cpp new file mode 100644 index 0000000..7b5be40 --- /dev/null +++ b/BasicsofC/matrixmultiplication.cpp @@ -0,0 +1,47 @@ +#include +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; +} From 71bd8daf07902fbea224a8549c94decaef4c1211 Mon Sep 17 00:00:00 2001 From: Aryan Jha <68564456+Aryan-Jha29@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:33:22 +0530 Subject: [PATCH 02/10] Create odd_even_increment --- ExtraDSAQ/odd_even_increment | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 ExtraDSAQ/odd_even_increment diff --git a/ExtraDSAQ/odd_even_increment b/ExtraDSAQ/odd_even_increment new file mode 100644 index 0000000..6146821 --- /dev/null +++ b/ExtraDSAQ/odd_even_increment @@ -0,0 +1,88 @@ +#include +using namespace std; + +#define ff first +#define ss second +#define int long long +#define pb push_back +#define pii pair +#define vi vector +#define setbits(x) __builtin_popcountll(x) +#define mod 1000000007 +#define inf 1e18 +#define all(v) v.begin(),v.end() +#define ps(x,y) fixed<>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 void _print(pair p); +template void _print(vector v); +template void _print(set v); +template void _print(map v); +template void _print(multiset v); +template void _print(pair p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} +template void _print(vector v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(map 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() +{ + int n; cin >> n; + vector v(n); + for (auto &x : v)cin >> x; + for (int i = 2; i < n; i++) + { + if (v[i] % 2 != v[i % 2] % 2) + {cout << "NO\n"; return;} + } + cout << "YES\n"; +} +int32_t main() +{ +#ifdef SIEVE + sieve(); +#endif +#ifdef NCR + init(); +#endif + + FIO + c_a_j(); + + w(x)solve(); + return 0; +} From 36ffbd25f06b6a6ad40b089fee4165688963a54d Mon Sep 17 00:00:00 2001 From: Aryan Jha <68564456+Aryan-Jha29@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:36:18 +0530 Subject: [PATCH 03/10] Rename odd_even_increment to odd_even_increment.cpp --- ExtraDSAQ/{odd_even_increment => odd_even_increment.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ExtraDSAQ/{odd_even_increment => odd_even_increment.cpp} (100%) diff --git a/ExtraDSAQ/odd_even_increment b/ExtraDSAQ/odd_even_increment.cpp similarity index 100% rename from ExtraDSAQ/odd_even_increment rename to ExtraDSAQ/odd_even_increment.cpp From fedcdcd2ce684d245876d53beb88a5eae748862f Mon Sep 17 00:00:00 2001 From: Aryan Jha <68564456+Aryan-Jha29@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:37:53 +0530 Subject: [PATCH 04/10] Create co_growing_sequence.cpp --- ExtraDSAQ/co_growing_sequence.cpp | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 ExtraDSAQ/co_growing_sequence.cpp diff --git a/ExtraDSAQ/co_growing_sequence.cpp b/ExtraDSAQ/co_growing_sequence.cpp new file mode 100644 index 0000000..f6fd449 --- /dev/null +++ b/ExtraDSAQ/co_growing_sequence.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; +typedef long long ll; +const ll MOD = 1e9+7; +#define rep(i,a,b) for(ll i=a;i + +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< Date: Wed, 12 Oct 2022 21:38:27 +0530 Subject: [PATCH 05/10] Create division_by_two.cpp --- ExtraDSAQ/division_by_two.cpp | 98 +++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 ExtraDSAQ/division_by_two.cpp diff --git a/ExtraDSAQ/division_by_two.cpp b/ExtraDSAQ/division_by_two.cpp new file mode 100644 index 0000000..84c25a8 --- /dev/null +++ b/ExtraDSAQ/division_by_two.cpp @@ -0,0 +1,98 @@ +#include +using namespace std; + +#define ff first +#define ss second +#define int long long +#define pb push_back +#define pii pair +#define vi vector +#define setbits(x) __builtin_popcountll(x) +#define mod 1000000007 +#define inf 1e18 +#define all(v) v.begin(),v.end() +#define ps(x,y) fixed<>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 void _print(pair p); +template void _print(vector v); +template void _print(set v); +template void _print(map v); +template void _print(multiset v); +template void _print(pair p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} +template void _print(vector v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(map 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() +{ + int n; cin >> n; + vector v(n); + unordered_map m; + for (auto &x : v)cin >> x; + sort(all(v)); + for (int i = 0; i < n; i++) + { + if (v[i] >= 1 && v[i] <= n && m[v[i]] == 0) m[v[i]]++; + else + { + while (v[i] != 0) + { + v[i] /= 2; + if (v[i] >= 1 && v[i] <= n && m[v[i]] == 0) {m[v[i]]++; break;} + else if (v[i] == 0) {cout << "NO\n"; return;} + } + } + } + cout << "YES\n"; +} +int32_t main() +{ +#ifdef SIEVE + sieve(); +#endif +#ifdef NCR + init(); +#endif + + FIO + c_a_j(); + + w(x)solve(); + return 0; +} From e2dc72ec6d17d81a3d837fa6cdd8ffa0063095f3 Mon Sep 17 00:00:00 2001 From: Aryan Jha <68564456+Aryan-Jha29@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:38:56 +0530 Subject: [PATCH 06/10] Create atm_students.cpp --- ExtraDSAQ/atm_students.cpp | 106 +++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 ExtraDSAQ/atm_students.cpp diff --git a/ExtraDSAQ/atm_students.cpp b/ExtraDSAQ/atm_students.cpp new file mode 100644 index 0000000..bf9d8dd --- /dev/null +++ b/ExtraDSAQ/atm_students.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + +#define ff first +#define ss second +#define int long long +#define pb push_back +#define pii pair +#define vi vector +#define setbits(x) __builtin_popcountll(x) +#define mod 1000000007 +#define inf 1e18 +#define all(v) v.begin(),v.end() +#define ps(x,y) fixed<>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 void _print(pair p); +template void _print(vector v); +template void _print(set v); +template void _print(map v); +template void _print(multiset v); +template void _print(pair p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} +template void _print(vector v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(map 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 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; +} From 6792f24d4ad91c10a086d65069ec966d326f5063 Mon Sep 17 00:00:00 2001 From: Aryan Jha <68564456+Aryan-Jha29@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:39:22 +0530 Subject: [PATCH 07/10] Create weighted_tree.cpp --- ExtraDSAQ/weighted_tree.cpp | 118 ++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 ExtraDSAQ/weighted_tree.cpp diff --git a/ExtraDSAQ/weighted_tree.cpp b/ExtraDSAQ/weighted_tree.cpp new file mode 100644 index 0000000..d251677 --- /dev/null +++ b/ExtraDSAQ/weighted_tree.cpp @@ -0,0 +1,118 @@ +#include +using namespace std; + +#define ff first +#define ss second +#define int long long +#define pb push_back +#define pii pair +#define vi vector +#define setbits(x) __builtin_popcountll(x) +#define mod 1000000007 +#define inf 1e18 +#define all(v) v.begin(),v.end() +#define ps(x,y) fixed<>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 void _print(pair p); +template void _print(vector v); +template void _print(set v); +template void _print(map v); +template void _print(multiset v); +template void _print(pair p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} +template void _print(vector v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(map 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() +{ + int n; cin >> n; + vector v(n), parent(n + 1, -1), permute(n); + int root; + for (int i = 0; i < n; i++) + { + cin >> v[i]; + parent[i + 1] = v[i]; + if (i + 1 == v[i]) + { + root = v[i]; + } + } + for (int i = 0; i < n; i++)cin >> permute[i]; + + if (permute[0] != root) {cout << -1 << endl; return;} + vector curr_dist(n + 1, -1), overall_dist(n + 1, -1); + vector> ans; + curr_dist[root] = 0; + overall_dist[root] = 0; + int cnt = 0; + + for (int i = 0; i < n; i++) + { + if (curr_dist[parent[permute[i]]] == -1) + { + cout << -1 << endl; + return; + } + if (permute[i] != root) + { + overall_dist[permute[i]] = cnt; + curr_dist[permute[i]] = overall_dist[permute[i]] - overall_dist[parent[permute[i]]]; + } + cnt++; + ans.pb({permute[i], curr_dist[permute[i]]}); + } + sort(all(ans)); + for (auto x : ans)cout << x.ss << " "; + cout << endl; +} +int32_t main() +{ +#ifdef SIEVE + sieve(); +#endif +#ifdef NCR + init(); +#endif + + FIO + c_a_j(); + + w(x)solve(); + return 0; +} From 478da986ea02d206501d5ec377fcf9590bce991a Mon Sep 17 00:00:00 2001 From: Aryan Jha <68564456+Aryan-Jha29@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:49:49 +0530 Subject: [PATCH 08/10] Create making_sum.cpp --- ExtraDSAQ/making_sum.cpp | 99 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 ExtraDSAQ/making_sum.cpp diff --git a/ExtraDSAQ/making_sum.cpp b/ExtraDSAQ/making_sum.cpp new file mode 100644 index 0000000..4f44a6c --- /dev/null +++ b/ExtraDSAQ/making_sum.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + +#define ff first +#define ss second +#define int long long +#define pb push_back +#define pii pair +#define vi vector +#define setbits(x) __builtin_popcountll(x) +#define mod 1000000007 +#define inf 1e18 +#define all(v) v.begin(),v.end() +#define ps(x,y) fixed<>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 void _print(pair p); +template void _print(vector v); +template void _print(set v); +template void _print(map v); +template void _print(multiset v); +template void _print(pair p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} +template void _print(vector v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(map 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 +} +int sod(int n) +{ + int sum = 0; + while (n) + { + sum += n % 10; + n /= 10; + } + return sum; +} +int gen(int x) +{ + int ans = 0; + while (x--)ans = ans * 10 + 9; + return ans; +} +void solve() +{ + int n; cin >> n; + if (n < 10) {cout << n << endl; return;} + int b = log10(n) + 1; + cout << (b - 1) * 9 + sod(n - gen(b - 1)) << endl; +} +int32_t main() +{ +#ifdef SIEVE + sieve(); +#endif +#ifdef NCR + init(); +#endif + + FIO + c_a_j(); + + solve(); + return 0; +} From a5862cd56fa624fe05739f3b8c7f6ea653ebb7fe Mon Sep 17 00:00:00 2001 From: Aryan Jha <68564456+Aryan-Jha29@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:54:43 +0530 Subject: [PATCH 09/10] Create memory_scene.cpp --- Arrays/memory_scene.cpp | 113 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Arrays/memory_scene.cpp diff --git a/Arrays/memory_scene.cpp b/Arrays/memory_scene.cpp new file mode 100644 index 0000000..4bb6e85 --- /dev/null +++ b/Arrays/memory_scene.cpp @@ -0,0 +1,113 @@ +#include +using namespace std; + +#define ff first +#define ss second +#define int long long +#define pb push_back +#define pii pair +#define vi vector +#define setbits(x) __builtin_popcountll(x) +#define mod 1000000007 +#define inf 1e18 +#define all(v) v.begin(),v.end() +#define ps(x,y) fixed<>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 void _print(pair p); +template void _print(vector v); +template void _print(set v); +template void _print(map v); +template void _print(multiset v); +template void _print(pair p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} +template void _print(vector v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(map 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; +} From 11a2f6c1eaf26fa4627942b0deb23cdd733c1d85 Mon Sep 17 00:00:00 2001 From: Aryan Jha <68564456+Aryan-Jha29@users.noreply.github.com> Date: Wed, 12 Oct 2022 22:00:14 +0530 Subject: [PATCH 10/10] Add files via upload File_uploaded --- Arrays/Fast exponentiation.cpp | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Arrays/Fast exponentiation.cpp diff --git a/Arrays/Fast exponentiation.cpp b/Arrays/Fast exponentiation.cpp new file mode 100644 index 0000000..23e69fd --- /dev/null +++ b/Arrays/Fast exponentiation.cpp @@ -0,0 +1,70 @@ +#include +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 +#define vi vector +#define vii vector +#define vpii vector> +#define mii map +#define pqb priority_queue +#define pqs priority_queue > +#define setbits(x) __builtin_popcountll(x) +#define zrobits(x) __builtin_ctzll(x) +#define mod 1000000007 +#define inf 1e18 +#define yes cout<<"YES"<>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 v(n);for(int i=0;i>v[i]; +#define decl(n,v) int n; cin>>n; vector 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; +} \ No newline at end of file