Skip to content

Instantly share code, notes, and snippets.

@theSoberSobber
Created January 30, 2023 14:32
Show Gist options
  • Save theSoberSobber/0a5915b2c7bd731a5d91074e117be6f6 to your computer and use it in GitHub Desktop.
Save theSoberSobber/0a5915b2c7bd731a5d91074e117be6f6 to your computer and use it in GitHub Desktop.
some snippets for competetive programming
{
"bp": {
"prefix": "bp",
"body": [
"#include <bits/stdc++.h>",
"using namespace std;\n",
"#define ll long long int",
"#define loop(i,a,b) for(int i=a;i<b;i++)\n",
"// debug purposes",
"#define show(x) cout << (#x) << ` : ` << x << endl;",
"#define logarr(arr,f,l) { auto start=arr.begin(), end=arr.begin(); advance(start,(f)); advance(end,(l)); for(auto it=start; it!=end; it++) cout<<*it<<` `; cout<<`\n`; }\n",
"void pre_process(){",
"\t",
"}\n",
"void solve(){",
"\tint n; cin>>n;",
"\t$1",
"}",
"int32_t main(){",
"\tios_base::sync_with_stdio(0);",
"\tcin.tie(0); cout.tie(0)",
"\tpre_process();",
"\tint t; cin>>t;",
"\twhile(t--) cout<<solve()<<'\\n';",
"}"
],
"description": "cp-template"
},
"pbds" : {
"prefix": "pbds",
"body": [
"#include <ext/pb_ds/assoc_container.hpp>",
"#include <ext/pb_ds/tree_policy.hpp>",
"using namespace __gnu_pbds;",
"typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> op_set;\n"
],
"description": "pbds"
},
"binpow" : {
"prefix": "binpow",
"body": [
"ll binpow(ll x, ll y){",
"\t// iterative binary exponentiation",
"\tll res = 1;",
"\twhile (y > 0) {",
"\tif (y & 1) res = (long long)(res*x); // adding powers wherever bit is set",
"\ty = y>>1;",
"\tx = (long long)(x*x); // increasing power by 2",
"\t}" ,
"\treturn res;" ,
"}\n"
],
"description": "binpow"
},
"bits" : {
"prefix": "bits",
"body": [
"#define "
],
"description": "bits"
},
"debug": {
"prefix": "debug",
"body": [
"namespace Debug {",
"// Color",
"#ifdef TERMINAL",
" #define DEBUG_RED \"\\x1B[0m\\x1B[31m\"",
" #define DEBUG_GREEN \"\\x1B[0m\\x1B[32m\"",
" #define DEBUG_YELLOW \"\\x1B[0m\\x1B[33m\"",
" #define DEBUG_BLUE \"\\x1B[0m\\x1B[34m\"",
" #define DEBUG_MAGENTA \"\\x1B[0m\\x1B[35m\"",
" #define DEBUG_CYAN \"\\x1B[0m\\x1B[36m\"",
" #define DEBUG_WHITE \"\\x1B[0m\\x1B[37m\"",
" #define DEBUG_RESET \"\\x1B[0m\"",
" #define DEBUG_BOLD_GREEN \"\\x1B[0m\\x1B[32;1m\"",
" #define DEBUG_BOLD_YELLOW \"\\x1B[0m\\x1B[33;1m\"",
" #define DEBUG_BOLD_BLUE \"\\x1B[0m\\x1B[34;1m\"",
" #define DEBUG_BOLD_MAGENTA \"\\x1B[0m\\x1B[35;1m\"",
" #define DEBUG_BOLD_CYAN \"\\x1B[0m\\x1B[36;1m\"",
" #define DEBUG_BOLD_WHITE \"\\x1B[0m\\x1B[37;1m\"",
" #define DEBUG_UNDERLINE \"\\x1B[4m\"",
"#else",
" #define DEBUG_RED \"\"",
" #define DEBUG_GREEN \"\"",
" #define DEBUG_YELLOW \"\"",
" #define DEBUG_BLUE \"\"",
" #define DEBUG_MAGENTA \"\"",
" #define DEBUG_CYAN \"\"",
" #define DEBUG_WHITE \"\"",
" #define DEBUG_RESET \"\"",
" #define DEBUG_BOLD_GREEN \"\"",
" #define DEBUG_BOLD_YELLOW \"\"",
" #define DEBUG_BOLD_BLUE \"\"",
" #define DEBUG_BOLD_MAGENTA \"\"",
" #define DEBUG_BOLD_CYAN \"\"",
" #define DEBUG_BOLD_WHITE \"\"",
" #define DEBUG_UNDERLINE \"\"",
"#endif",
"",
"#define DEBUG_BODY DEBUG_BOLD_CYAN",
"#define DEBUG_VAR DEBUG_RED",
"#define DEBUG_PAREN DEBUG_YELLOW",
"#define DEBUG_BRAC DEBUG_GREEN",
"#define DEBUG_COMMA DEBUG_BOLD_MAGENTA",
"#define DEBUG_CONTENT DEBUG_BODY DEBUG_UNDERLINE",
"#define DEBUG_LINE DEBUG_BOLD_YELLOW",
"#define DEBUG_LINENUM DEBUG_BOLD_BLUE",
" // clang-format on",
"",
"#define SFINAE(x, ...) \\",
" template <class, class = void> \\",
" struct x : std::false_type {}; \\",
" template <class T> \\",
" struct x<T, std::void_t<__VA_ARGS__>> : std::true_type {}",
"",
" SFINAE(DefaultIO, decltype(std::cout << std::declval<T &>()));",
" SFINAE(IsTuple, typename std::tuple_size<T>::type);",
" SFINAE(Iterable, decltype(std::begin(std::declval<T>())));",
"",
" template <auto &os>",
" struct Writer {",
" static constexpr auto lbrac = DEBUG_BRAC \"[\";",
" static constexpr auto rbrac = DEBUG_BRAC \"]\";",
" static constexpr auto lparen = DEBUG_PAREN \"(\";",
" static constexpr auto rparen = DEBUG_PAREN \")\";",
" static constexpr auto comma = DEBUG_COMMA \", \";",
" template <class T>",
" void Impl(T const &t) const {",
" if constexpr (DefaultIO<T>::value) {",
" os << DEBUG_CONTENT << t;",
" } else if constexpr (Iterable<T>::value) {",
" int i = 0;",
" os << lbrac;",
" for (const auto x : t) ((i++) ? (os << comma, Impl(x)) : Impl(x));",
" os << rbrac;",
" } else if constexpr (IsTuple<T>::value) {",
" os << lparen;",
" std::apply(",
" [this](auto const &... args) {",
" int i = 0;",
" (((i++) ? (os << comma, Impl(args)) : Impl(args)), ...);",
" },",
" t);",
" os << rparen;",
" } else",
" static_assert(IsTuple<T>::value, \"No matching type for print\");",
" }",
" template <class F, class... Ts>",
" auto &operator()(F const &f, Ts const &... ts) const {",
" return Impl(f), ((os << comma, Impl(ts)), ...), os << '\\n', *this;",
" }",
" };",
"",
" template <auto &is>",
" struct Reader {",
" template <class T>",
" auto &Rd(T &t) const {",
" if constexpr (DefaultIO<T>::value)",
" is >> t;",
" else if constexpr (Iterable<T>::value)",
" for (auto &x : t) Rd(x);",
" else if constexpr (IsTuple<T>::value)",
" std::apply([this](auto &... args) { (Rd(args), ...); }, t);",
" else",
" static_assert(IsTuple<T>::value, \"No matching type for read\");",
" return *this;",
" }",
" template <class T>",
" auto operator()(T t) const {",
" Rd(t);",
" return t;",
" }",
" };",
"",
"",
"#ifdef DEBUG",
" // Change debugging function name here",
" #define d(args...) \\",
" { \\",
" std::string _s = #args; \\",
" std::replace(_s.begin(), _s.end(), ',', ' '); \\",
" std::stringstream _ss(_s); \\",
" std::istream_iterator<std::string> _it(_ss); \\",
" Debug::location_stats(__LINE__); \\",
" Debug::err(_it, args); \\",
" Debug::clear_colours(); \\",
" }",
"",
" void location_stats(int line) {",
" // Uncomment if you want to print line number",
" // std::cout << DEBUG_LINE << \"Line \" << DEBUG_LINENUM << line << \"\\n\" << DEBUG_RESET;",
" }",
" void clear_colours() { std::cout << DEBUG_RESET; }",
" void err(std::istream_iterator<std::string> it) { std::ignore = it; }",
" template <typename T, typename... Args>",
" void err(std::istream_iterator<std::string> it, T a, Args... args) {",
" std::cout << DEBUG_VAR << *it << \" = \";",
" Writer<std::cout>{}(a);",
" err(++it, args...);",
" }",
"",
" #define ASSERT(...) \\",
" if (not(__VA_ARGS__)) throw runtime_error(#__VA_ARGS__)",
"#else",
" // Change debugging function name here",
" #define d(...) 0",
" #define ASSERT(...) 0",
"#endif",
"",
"#undef DEBUG_RED",
"#undef DEBUG_GREEN",
"#undef DEBUG_YELLOW",
"#undef DEBUG_BLUE",
"#undef DEBUG_MAGENTA",
"#undef DEBUG_CYAN",
"#undef DEBUG_WHITE",
"#undef DEBUG_RESET",
"#undef DEBUG_BOLD_GREEN",
"#undef DEBUG_BOLD_YELLOW",
"#undef DEBUG_BOLD_BLUE",
"#undef DEBUG_BOLD_MAGENTA",
"#undef DEBUG_BOLD_CYAN",
"#undef DEBUG_BOLD_WHITE",
"#undef DEBUG_UNDERLINE",
"#undef DEBUG_BODY",
"#undef DEBUG_VAR",
"#undef DEBUG_PAREN",
"#undef DEBUG_BRAC",
"#undef DEBUG_COMMA",
"#undef DEBUG_CONTENT",
"#undef DEBUG_LINE",
"#undef DEBUG_LINENUM",
"",
"} // namespace Debug"
],
"description": ""
},
"mint": {
"prefix": "mint",
"body": [
"struct mi {",
" int64_t v; explicit operator int64_t() const { return v % mod; }",
" mi() { v = 0; }",
" mi(int64_t _v) {",
" v = (-mod < _v && _v < mod) ? _v : _v % mod;",
" if (v < 0) v += mod;",
" }",
" friend bool operator==(const mi& a, const mi& b) {",
" return a.v == b.v; }",
" friend bool operator!=(const mi& a, const mi& b) {",
" return !(a == b); }",
" friend bool operator<(const mi& a, const mi& b) {",
" return a.v < b.v; }",
"",
" mi& operator+=(const mi& m) {",
" if ((v += m.v) >= mod) v -= mod;",
" return *this; }",
" mi& operator-=(const mi& m) {",
" if ((v -= m.v) < 0) v += mod;",
" return *this; }",
" mi& operator*=(const mi& m) {",
" v = v*m.v%mod; return *this; }",
" mi& operator/=(const mi& m) { return (*this) *= inv(m); }",
" friend mi pow(mi a, int64_t p) {",
" mi ans = 1; assert(p >= 0);",
" for (; p; p /= 2, a *= a) if (p&1) ans *= a;",
" return ans;",
" }",
" friend mi inv(const mi& a) { assert(a.v != 0);",
" return pow(a,mod-2); }",
"",
" mi operator-() const { return mi(-v); }",
" mi& operator++() { return *this += 1; }",
" mi& operator--() { return *this -= 1; }",
" mi operator++(int32_t) { mi temp; temp.v = v++; return temp; }",
" mi operator--(int32_t) { mi temp; temp.v = v--; return temp; }",
" friend mi operator+(mi a, const mi& b) { return a += b; }",
" friend mi operator-(mi a, const mi& b) { return a -= b; }",
" friend mi operator*(mi a, const mi& b) { return a *= b; }",
" friend mi operator/(mi a, const mi& b) { return a /= b; }",
" friend ostream& operator<<(ostream& os, const mi& m) {",
" os << m.v; return os;",
" }",
" friend istream& operator>>(istream& is, mi& m) {",
" int64_t x; is >> x;",
" m.v = x;",
" return is;",
" }",
" friend void __print(const mi &x) {",
" cerr << x.v;",
" }",
"};",
""
],
"description": ""
},
"seive": {
"prefix": "seive",
"body": [
"vector<bool> Prime;",
"vector<int> spf;",
"void sieve(int s = maxn) {",
" Prime.resize(s + 1, 1);",
" spf.resize(s + 1, s + 1);",
" for(int i = 2 ; i <= s ; i++) if(Prime[i]) {",
" spf[i] = min(spf[i], i);",
" for(int j = i ; (ll)j * i <= s ; j++)",
" Prime[j * i] = 0, spf[j * i] = min(i, spf[j * i]);",
" }",
"}",
""
],
"description": "seive"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment