# OAI NPDang - BTNguyen: [TOC] ## I. Implementation Fundamentals ### I.1 - Statements #### I.1-a: Single Statement `...;` #### I.1-b: Compound Statement `{...}` #### I.1-c: `if else` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if(n = 69) cout << 1; else cout << 0; return 0; } ``` ::: #### I.1-d: `switch case` `[[fallthrough]];` #### I.1-e: `ternary operator` #### I.1-f: `while` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; int ans = 0; cin >> n; while( n > 9) { ans += n/10; ans *= 10; n /= 10; } cout << ans << '\n'; return 0; } ``` ::: #### I.1-g: `do while` :::spoiler Code ```cpp #include <bits/stdc++.h> using namespace std; int main() { int i = 0; do { cout << i << "\n"; i++; } while (i < 5); } ``` ::: #### I.1-h: `for` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for(int i = 1; i <= n; ++i) cout << i; return 0; } ``` ::: #### I.1-i: `range-for` :::spoiler ```cpp= vector<int> a; for (int x : a) { } ``` ::: #### I.1-j: `continue` :::spoiler Code ```cpp for (int i = 0; i < 10; i++) { if (i == 4) { break; } cout << i << "\n"; } ``` ::: #### I.1-k: `break` :::spoiler Code ```cpp for (int i = 0; i < 10; i++) { if (i == 4) { continue; } cout << i << "\n"; } ``` ::: #### I.1-l: `goto` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int main() { string key; cin >> key; if(key == "67") goto HugeShortcut; for(size_t i = 0xFFFFFFF; i >= 0; i--) cout << i << '\t'; cout << '\n'; HugeShortcut: cout << "Hello World!"; return 0; } ``` ::: #### I.1-m: `return ;` `return <expressions>;` #### I.1-n: `while(true)` `for (;;)` :::spoiler Code ```cpp= int main() { int n; cin >> n; int m = n; int ans = 0; while(n>0) { ans += n%10; n /= 10; } cout << ans; int s = 0; for(int i = 1; i <= m; ++i) { s += i; } cout << s; } ``` ::: #### I.1-o: `for (auto [u, v] : edges)` (C++17) ### I.2 - Int Types #### I.2-a: `int` (>=2 byte) :::spoiler Code ```cpp= int n; ``` ::: #### I.2-b: `unsigned int` #### I.2-c: `long long` :::spoiler Code ```cpp= long long n; ``` ::: #### I.2-d: `unsigned long long` #### I.2-e: `short` #### I.2-f: `unsigned short` #### I.2-g: `int8_t` #### I.2-h: `int16_t` #### I.2-i: `int32_t` #### I.2-j: `int64_t` #### I.2-k: `uint8_t` #### I.2-l: `uint16_t` #### I.2-m: `uint32_t` #### I.2-n: `uint64_t` #### I.2-o: `size_t` #### I.2-p: `ssize_t` (C++20) ### I.3 - Float Types #### I.3-a: `float` :::spoiler Code ```cpp= float n; ``` ::: #### I.3-b: `double` :::spoiler Code ```cpp= double n; ``` ::: #### I.3-c: `long double` ### I.4 - Char & String Types #### I.4-a: `char` #### I.4-b: `signed char` #### I.4-c: `unsigned char` #### I.4-d: `string` :::spoiler Code ```cpp= string n; ``` ::: #### I.4-e: `string_view` #### I.4-f: `fstream` #### I.4-g: `ifstream` #### I.4-h: `ofstream` #### I.4-i: `stringstream` #### I.4-j: `istringstream` #### I.4-k: `ostringstream` #### I.4-l: `char8_t` (C++20) #### I.4-m: `char16_t` #### I.4-n: `char32_t` #### I.4-o: `istream` #### I.4-p: `ostream` #### I.4-q: `cin` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n; int main() { cin >>n; cout << n; } ``` ::: #### I.4-r: `cout` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n; int main() { cin >>n; cout << n; } ``` ::: #### I.4-s: `cerr` #### I.4-t: `clog` #### I.4-u: `Escape Character ('\n', '\\', '\'', '\"', ...)` #### I.4-v: `Escape Sequence ('\nnn', '\o{nnn}', '\xnnn', '\x{nnn}', ...)` #### I.4-w: `wchar_t` #### I.4-x: `wcin` #### I.4-y: `wcout` ### I.5 - Bool & Bit #### I.5-a: `bool` #### I.5-b: `byte` (C++17) #### I.5-c: `vector<bool>` #### I.5-d: `bitset<SIZE>` ### I.6 - Literals #### I.6-a: `Long Literal (1L)` #### I.6-b: `Unsigned Long Literal (1U)` #### I.6-c: `Long Long Literal (1LL)` #### I.6-d: `Unsigned Long Long Literal (1ULL)` #### I.6-f: `Signed Size Literal (1Z)` (C++23) #### I.6-g: `Octal Literal (0123)` #### I.6-h: `Hex Literal (0x123)` #### I.6-i: `Binary Literal (0b101)` #### I.6-j: `Float Literal (.1f)` #### I.6-k: `Double Literal (.1)` #### I.6-l: `Long Double Literal (.1L)` #### I.6-m: `Char Literal ('\n')` #### I.6-n: `String Literal ("abc")` #### I.6-o: `Raw String Literal (R"(raw\nstring)")` #### I.6-p: `Boolean Literal (true/false)` #### I.6-z: `Digit Separator (123'456'789)` ### I.7 - Cast #### I.7-a: C Cast `(Type)(Data)` #### I.7-b: Static Cast `static_cast<Type>(Data)` #### I.7-c: Const Cast `static_cast<Type>(Data)` #### I.7-d: Reinterpret Cast: `reinterpret_cast<Type>(Data)` #### I.7-e: Dynamic Cast: `dynamic_cast<Type>(Data)` #### I.7-f: Bit Cast: `std::bit_cast` ### I.8 - Scoping #### I.8-a: Global Scope `::x` #### I.8-b: Variable Shadowing #### I.8-c: Namespace `namespace` #### I.8-d: Nameless Namespace `namespace` #### I.8-e: Static Storage Duration `static` (deprecated `register`) #### I.8-f: Static Constant Expressive `constexpr` `constinit` (C++20) #### I.8-g: Thread Storage Duration `thread_local` #### I.8-h: Dynamic Storage Duration (`new`, `new[]`, `delete`, `delete[]`) #### I.8-i: Automatic Storage Duration (no `static`, no `thread_local`, no `new`, no `new[]`) #### I.8-j: External Linkage (`inline`, `extern`) #### I.8-k: Internal Linkage #### I.8-l: No Linkage #### I.8-m: Block Scope `{...}` #### I.8-n: Class Struct Scope (Access Specifier: `public:`, `private:`, `protected:`) #### I.8-o: Lifetime ### I.9 - Initialization #### I.9-a: Default Initialization `int x;` #### I.9-b: Value Initialization `int x{};` #### I.9-c: Zero Initialization `static int x;` #### I.9-d: Copy Initialization `int x = 0;` #### I.9-e: Direct Initialization `int x(0);` #### I.9-f: Scalar List Initialization `int x = {0};` (Narrowing Safe) #### I.9-g: Copy List Initialization `vector<int> x = vector<int>{0, 0};` #### I.9-h: Direct List Initialization `vector<int> x{0, 0};` #### I.9-i: Initializer List Initialization `std::initializer_list<T> var = {..};` #### I.9-j: Reference Initialization `int &x = dp[const_cat][const_card][const_cart];` #### I.9-k: Constant Initialization `const int x = 0;` #### I.9-l: Array Initialization `int x[2]{};` #### I.9-m: Dynamic Initialization `int *x = new int(0);` (`new`) #### I.9-n: Zero-Fill Array Initialization `int *x = new int[2]();` `(new[])` #### I.9-o: Dynamic Array Initialization `int *x = new int[2]{0,0};` `(new[])` #### I.9-p: Member Initialization `struct Int { int member; Int(int param) : member(param) {} }` #### I.9-q: Default Member Initialization `struct Int{ int member = 0; }` (NSDMI) #### I.9-r: String Literal Initialization `const char name[] = "SPy";` (utf8, unicode, wchar, signed char, unsigned chah, char) #### I.9-s: Structured Binding Initialization `for (auto [u, v] : edges) {}` #### I.9-t: Control Flow Initialization `if (int x = 0; x > 0) {}` #### I.9-u: Partial Aggregate Initialization `int x[5]{1, 2};` #### I.9-v: Direct Aggregate Initialization `Pair p(1, 2);` #### I.9-w: Designated Initialization `Pair x = { .first = 1, .second = 2 };` (C++20) #### I.9-x: Designated Aggregate Initialization `T object = { .var1 = arg1, .var2 { arg2 } }` #### I.9-y: Lambda Capture Initialization `auto test = [&bind=target](){};` #### I.9-z: Pointer Null Initialization `int *ptr{};` ### I.10 - Unary Operators #### I.10-a: `+X` `Unary Plus` #### I.10-b: `-X` `Unary Minus` #### I.10-c: `!X` `Negation` #### I.10-d: `~X` `Bitwise NOT` #### I.10-e: `++X` `Pre Increment` #### I.10-f: `--X` `Pre Decrement` #### I.10-g: `X++` `Post Increment` #### I.10-h: `X--` `Post Decrement` ### I.11 - Arithmetic Operators #### I.11-a: `A+B` `Addition` `Plus` #### I.11-b: `A-B` `Subtraction` `Minus` #### I.11-c: `A*B` `Multiplication` `Multiply` #### I.11-d: `A/B` `Division` `Div` #### I.11-e: `A%B` `Remainder` `Mod` #### I.11-f: `A+=B` `Assignment Addition` #### I.11-g: `A-=B` `Assignment Subtraction` #### I.11-h: `A*=B` `Assignment Multiplication` #### I.11-i: `A/=B` `Assignment Division` #### I.11-j: `A%=B` `Assignment Remainder` ### I.12 - Comparison Operators #### I.12-a: `A<B` `Less` `LE` #### I.12-b: `A<=B` `Less Or Equal` `LEQ` #### I.12-c: `A>B` `Greater` `GE` #### I.12-d: `A>=B` `Greater Or Equal` `GEQ` #### I.12-e: `A==B` `Equal` `EQ` #### I.12-f: `A!=B` `Not Equal` `NEQ` #### I.12-g: `A<=>B` `Three-way` `Spaceship Operator` ### I.13 - Logic Operators #### I.13-a: `A&&B` `Logic And` #### I.13-b: `A||B` `Logic Or` #### I.13-c: `There is no Logic Xor Operator` #### I.13-d: `Short Circuit` ### I.14 - Bitwise Operators #### I.14-a: `A<<B` `Shift Left` #### I.14-b: `A>>B` `Shift Right` #### I.14-c: `A^B` `XOR` #### I.14-d: `A&B` `AND` #### I.14-e: `A|B` `OR` #### I.14-f: `A<<=B` `Assignment Shift Left` #### I.14-g: `A>>=B` `Assignment Shift Right` #### I.14-h: `A^=B` `Assignment XOR #### I.14-i: `A&=B` `Assignment AND` #### I.14-j: `A|=B` `Assignment OR` #### I.14-k: `~A` `UNARY NOT` ### I.15 - Operator Overloadding #### I.15-a: Unoverloadable Operators `::`, `.`, `.*`, `?:` #### I.15-b: Custom Assignment Operator `=` #### I.15-c: Custom Function Call Operator `()` #### I.15-d: Custom Subscript Operator `[]` #### I.15-e: Custom Multidimensional Subscript Operator (comma operator, `C++23 []`) #### I.15-f: Custom Logic And Operator `&&` #### I.15-g: Custom Logic And Operator `||` #### I.15-h: Custom Comma Operator `,` #### I.15-i: Custom Member Access Operator `->` #### I.15-j: Custom Pointer Member Access Operator `->*` #### I.15-k: Custom New Operator `new` `new[]` #### I.15-l: Custom Delete Operator `delete` `delete[]` #### I.15-m: User-defined Literals Operator `""_s()` #### I.15-n: Explicit Conversion Operator `explicit operator T()` #### I.15-o: Boolean Explicit (Supressing Implicit Conversion) Operator `explicit operator bool()` #### I.15-z: Comparison Suite: `strong ordering`, `partial ordering`, `poset` ### I.16 - Order #### I.16-a: Operator Prescedence #### I.16-b: Evaluation Order #### I.16-c: Sequence Cases #### I.16-d: Assignment Sequence Points #### I.16-e: Function Call Arguments Order ### I.17 - Enum #### I.17-a: Enum #### I.17-b: Enum Class #### I.17-c: Enum Size ### I.18 - Template #### I.18-a: Generic #### I.18-y: TMP #### I.18-z: SFINAE ### I.19 - Preprocessor #### I.19-a: `#define` :::spoiler <font style = "color:green"><b>GOOD FOR UR SCORES 🤑🤑🤑 AND SPEED AS WELL</b></font> ```cpp= #include <iostream> #define int long long signed main(){ } ``` ::: #### I.19-b: `#pragma` :::spoiler <b>ISHOWSPEEDDDDDDDD!!!</b> ```cpp= #include <iostream> #include <vector> #pragma GCC optimize("unroll-loops", "Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ``` ::: #### I.19-c: File Guard `#ifndef` `#ifdef` `#elif` `#endif` #### I.19-d: Build Flags (`-O2`, `-pipe`, `-static`, `-Wall`, `-Wextra`) ### I.20 - IO #### I.20-a: `ios::sync_with_stdio(false);` #### I.20-b: `cin.tie(nullptr);` #### I.20-c: `cout.tie(nullptr);` #### I.20-d: FastIO (`getchar`, `putchar`) #### I.20-e: Unsafe FastIO (`getchar_unlocked`, `putchar_unlocked`) ### I.21 - Math #### I.21-a: `abs()` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n; int main() { cin >>n; cout << abs(n); } ``` ::: #### I.21-b: `gcd()` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n, m; int main() { cin >>n >> m; cout << __gcd(n,m); } ``` ::: #### I.21-c: `lcm()` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n,m; int main() { cin >>n >> m; int lcm = n*m/__gcd(n,m); cout <<n; } ``` ::: #### I.21-d: `min()` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n,m; int main() { cin >>n>>m; cout << min(n,m); } ``` ::: #### I.21-e: `max()` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n,m; int main() { cin >>n>>m; cout << max(n,m); } ``` ::: #### I.21-f: `sqrt()` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n,m; int main() { cin >>n; cout << sqrt(n); } ``` ::: #### I.21-g: `pow()` :::spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; int n,m; int main() { cin >>n; cout << pow(n,2); } ``` ::: ### I.22 - Debug #### I.22-a: Runtime Assert #### I.22-b: Static Assert ### I.23 - Include #### I.23-a: `<bits/stdc++.h>` #### I.23-a: Free Standing Library ## II. Implementation (Đề hỏi gì làm nấy) ### II.1 - Bitmask #### II.1-a: `IsZero(X)` :::spoiler Code ```cpp= #include <iostream> typedef long long int; bool isZero(int x){ return !(x&(-x)); } ``` ::: #### II.1-b: `IsOdd(X)` `IsEven(X)` :::spoiler Code ```cpp= #include <iostream> typedef long long int bool isOdd(int x){ return x&1; //true if it is odd and false (even) otherwise } bool isEven(int x){ return !(x&1); } ``` ::: #### II.1-c: `IsPowerOfTwo(X)` :::spoiler Code ```cpp= #include <iostream> using namespace std; typedef long long int; bool isPowerOfTwo(int x){ return (x > 0 && !x&(x - 1)); } ``` ::: #### II.1-d: `IsPowerOfFour(X)` `IsPowerOfPowerOfTwo(X, K)` :::spoiler Code ```cpp= #include <iostream> using namespace std; typedef long long int; bool isPowerOfPowerOfTwo(int x, short k){ return (x > 0 && !x&(x - 1));//chx xong } ``` ::: #### II.1-e: `CheckBit(X, B)` `SetBit(X, B)` `UnsetBit(X, B)` `ToggleBit(X, B)` :::spoiler Code ```cpp= #include <iostream> using namespace std; typedef long long ll; bool CheckBit(ll n, int k){ return (n&(1<<(k-1))); } void SetBit(ll &n, int k){ n |= 1<<(k- 1); } void UnsetBit(ll &n, int k) { n &= ~(1<<(k - 1)); } void ToggleBit(ll &n, int k){ n ^= 1<<(k - 1); } ``` ::: #### II.1-f: `AssignBit(X, B, V)` :::spoiler Code ```cpp= #include <iostream> using namespace std; typedef long long ll; void AssignBit(ll n, int k, bool state){ if(state) n |= (1 << (k - 1)); else n &= ~(1 << (k - 1)); } ``` ::: #### II.1-g: `ToBinary(X)` :::spoiler ```cpp= #include <iostream> using namespace std; typedef long long ll; void ToBinary(ll &n){ for(long long i = 63; i >= 0; i--){ cout << ((n >> i)&1LL); } return; } ``` ::: #### II.1-h: `ToOctet(X)` `ToHex(X)` :::spoiler Code C + Cpp ```c= #include <stdio.h> typedef long long ll; void ToOctet(ll n){ printf("%#llo", n); } void ToHex(ll x){ printf("%0#llx", x); } ``` ```cpp= ``` ::: #### II.1-i: `XorSwap(X, Y)` :::spoiler ```cpp= #include <iostream> using namespace std; typedef long long ll; void XorSwap(ll &a, ll &b){ a ^= b ^= a ^= b; return; } ``` ::: #### II.1-j: `BitLength(X)` `Log2(X)` :::spoiler Code ```cpp= #include <iostream> #include <cmath> using namespace std; typedef long long ll; int BitLength(ll &n){ return log2(n) + 1; } ``` ::: #### II.1-k: `CountSetBit(X)` `CountUnsetBit(X)` :::spoiler ```cpp= #include <bits/stdc++.h> using namespace std; unsigned int CountSetBit(unsigned int n) { unsigned int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } int CountUnsetBits(int n) { int count = 0; for (int x = 1; x <= n; x = x<<1) if ((x & n) == 0) count++; return count; } ``` ::: #### II.1-l: `CountTrailingZero(X)` `CountTrailingOne(X)` #### II.1-m: `CountLeadingZero(X)` `CountLeadingOne(X)` #### II.1-n: `LeastSignificantBit(X)` `MostSignificantBit(X)` #### II.1-o: `BitLeftRotation(X, N)` `BitRightRotation(X, N)` #### II.1-p: `SubmaskEnumeration(X)` #### II.1-q: `SignOf(X)` `IsPositive(X)` `IsNegative(X)` #### II.1-r: `HasOppositeSign(X, Y)` #### II.1-t: `BitAbs(X)` #### II.1-u: `BitMin(X, Y)` `BitMax(X, Y)` #### II.1-v: `LexicoNextBitPermutation(X)` `LexicoPrevBitPermutation(X)` #### II.1-w: `NextPowerOfTwo(X)` `PrevPowerOfTwo(X)` #### II.1-x: `GrayCode(X)` `InverseGrayCode(X)` #### II.1-x: `ReverseBit(X)` ```cpp= #include <iostream> using namespace std; typedef long long ll; void ReverseBit(ll &n){ return n ^= (1 << 63) - 1 ; } ``` #### II.1-z: `GenAllBinary(N)` `GenAllTernary(N)` `GenAllQuaternary(N)` `GenAllGrayCode(N)` ### II.2 - Bitmask Bultin #### II.2-a: `Count Set Bit` `__builtin_popcount**(X)` `(**=NULL,L,LL)` #### II.2-b: `Count Trailing Zeros` `__builtin_ctz**(X)` `(**=NULL,L,LL)` #### II.2-c: `Count Leading Zeros` `__builtin_clz**(X)` `(**=NULL,L,LL)` #### II.2-d: `Parity Check` `__builtin_parity**(X)` `(**=NULL,L,LL)` #### II.2-e: `Least Significant Setbit` `__builtin_ffs**(X)` `(**=NULL,L,LL)` #### II.2-f: `Bit Left Rotation` `__builtin_rotateleft**(X, N)` `(**=8,16,32,64)` #### II.2-g: `Bit Right Rotation` `__builtin_rotateright**(X, N)` `(**=8,16,32,64)` #### II.2-h: `Bit Swap Endians` `__builtin_bswap**(X)` `(**=8,16,32,64)` ## III. Implementation Advance ## A. Abstract ## AA. ## AAA. ## O. Observation ## OO. ## OOO. ## Other ### X ### Y ### Z