Skip to content

Instantly share code, notes, and snippets.

#pragma once
template<class T>
inline void insert_sort(vector<T> &vec, size_t g) {
for (size_t i = g; i < vec.size(); ++i) {
int v = vec[i];
int j = i - g;
while (0 <= j && v < vec[j]) {
vec[j + g] = vec[j];
j -= g;
#pragma once
#include <algorithm>
#include <optional>
enum class Direction {
North,
West,
East,
South,
#pragma once
#include <cmath>
#include <ostream>
const double PI = 3.141592653589793;
struct Quaternion {
double a, b, c, d;
@MikuroXina
MikuroXina / delim-space.cpp
Created May 21, 2023 02:59
Outputting numbers with delimitting spaces in C++ for competitive programming.
#include <algorithm>
#include <iostream>
#include <numeric>
using namespace std;
int main() {
int nums[3];
for (auto &num: nums) {
cin >> num;
@MikuroXina
MikuroXina / compose.rs
Created February 23, 2023 12:12
The composition of functions in Rust.
pub fn compose<'l, X, Y, Z>(
left: &'l dyn Fn(Y) -> Z
) -> Box<dyn
Fn(&'l dyn Fn(X) -> Y) -> Box<dyn
Fn(X) -> Z
+ 'l
>
+ 'l
> {
Box::new(move |right| {
@MikuroXina
MikuroXina / non_nan_f64.rs
Last active May 11, 2024 05:46
NonNanF64, the floating-point number which will not be NaN in Rust.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NonNanF64(f64);
impl NonNanF64 {
/// # Safety
///
/// The value must not be `NaN`.
pub const unsafe fn new_unchecked(f: f64) -> Self {
// SAFETY: this is guaranteed to be safe by the caller.
Self(f)
@MikuroXina
MikuroXina / endless-error-loop.py
Created December 26, 2022 15:50
Lyrics of "Endless Error Loop" by Neko Hacker feat. ななひら.
print('''<header>
ここをもうちょっとシンプルに変えてっと
え、なにこのエラー・・・
</header>''')
try:
uglyCode()
except:
print('よくあるError')
try:
@MikuroXina
MikuroXina / reg_exp.rs
Last active November 30, 2025 07:48
The Brzozowski derivative of Regular Expression with Rust.
/// The regular expression tree.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RegExp {
/// Declares a variable of the match result.
Var(String, Box<RegExp>),
/// Matching pattern of a chatacter.
Let(char),
/// Or pattern of patterns.
Or(Box<RegExp>, Box<RegExp>),
/// Concatenated pattern of patterns.
@MikuroXina
MikuroXina / crt.rs
Last active November 8, 2022 13:15
Rust functions of Chinese Remainder Theorem.
/// Find values where satisify `a * p + b * q = gcd`.
pub fn ext_gcd(a: u64, b: u64) -> ExtGcd {
let mut s = (0i64, 1i64);
let mut t = (1i64, 0i64);
let mut r = (as_i64(b), as_i64(a));
while r.0 != 0 {
let q = r.1 / r.0;
let f = |mut r: (i64, i64)| {
std::mem::swap(&mut r.0, &mut r.1);
@MikuroXina
MikuroXina / mod_int.rs
Last active October 30, 2022 06:11
An integer modulo 998244353 with Montgomery Multiplication and Numeric Theory Transformation.
use num::{traits::Pow, One, Zero};
use serde::{Deserialize, Serialize};
const R: u64 = 1 << 32;
/// Find `modulo_inv` which satisifes `modulo * modulo_inv ≡ -1 (mod R)`.
const fn find_neg_inv(modulo: u32) -> u32 {
let mut inv_mod = 0u32;
let mut t = 0;
let mut i = 1u32;