Skip to content

Instantly share code, notes, and snippets.

View izanbf1803's full-sized avatar

Izan Beltran izanbf1803

View GitHub Profile
@izanbf1803
izanbf1803 / karatsuba.cc
Created April 2, 2019 18:55
Simple C++ Karatsuba implementation.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T> using V = vector<T>;
string bigint_to_str(const V<char>& c)
{
string s = "";
for (int i = c.size()-1; i >= 0; --i) {
s += c[i] + '0';
@izanbf1803
izanbf1803 / factorial_info.py
Created January 5, 2019 13:05
Find last m non-zero digits of n!
# Find last m non-zero digits of n!
n, m = map(int, input().split())
cnt = 0
power = 5
while n // power > 0:
cnt += n // power
power *= 5
print(f"{n}! has {cnt} zeros.")
@izanbf1803
izanbf1803 / A gas station too far.py
Last active January 5, 2019 12:18
Solución del problema "A gas station too far"
from jutge import read # Cosas del input...
def optimize(f, f_extra_args, l, r, y):
# Tiempo: O(t(f) log(r-l)) donde t(f) es el coste de calcular f(x)
#
# Este método maximiza la función f y si hay varios inputs que dan
# el valor y encuentra el mínimo {min x | f(x) = y} y se
# cumple que la función f es (no estrictamente) creciente, es decir,
# que siempre {f(x) >= f(x+1)}.
# Si tienes una función decreciente {f(x) <= f(x+1)} puedes convertirla
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
template<typename T> using V = vector<T>;
using pii = pair<int,int>;
const int N = 2019;
@izanbf1803
izanbf1803 / primes.py
Created December 31, 2018 15:05
Primes class to factorize integers.
import sys
from math import *
def error(err):
print("Error:", err)
sys.exit(0)
def lcm(a, b):
return a//gcd(a,b)*b
@izanbf1803
izanbf1803 / pascal_triangle.cc
Created December 31, 2018 02:44
Draw Pascal's triangle with simple format
#include <bits/stdc++.h>
using namespace std;
void pascals_triangle(int n)
{
n = 1 << (n+1);
vector<vector<bool>> a;
a.resize(n);
a[0].resize(1);
a[0][0] = 1;
@izanbf1803
izanbf1803 / colormod.h
Created December 5, 2018 00:30
Console output color
#include <ostream>
namespace Color {
enum Code {
FG_RED = 31,
FG_GREEN = 32,
FG_BLUE = 34,
FG_DEFAULT = 39,
BG_RED = 41,
BG_GREEN = 42,
@izanbf1803
izanbf1803 / reduce_mem_usage.py
Created November 3, 2018 12:17
Reduce memory usage in dataframe.
# Reference: https://www.kaggle.com/gemartin/load-data-reduce-memory-usage
def reduce_mem_usage(df):
""" iterate through all the columns of a dataframe and modify the data type
to reduce memory usage.
"""
start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
@izanbf1803
izanbf1803 / gen_func_sample.py
Created October 18, 2018 16:53
Sample of generating functions implementation using Sympy.
"""
Problem:
Cody has 4 types of onions.
The number of purple onions can be any non-negative integer.
The number of green onions is a multiple of 2.
The number of red onions is a multiple of 3.
The number of blue onions is a multiple of 5.
If Cody has N onions, how many different distributions of colors can there be?
"""
@izanbf1803
izanbf1803 / SegmentTree.cc
Last active January 15, 2019 15:29
Compact recursive implementation of basic Segment Tree.
template <typename T>
struct SegmentTree {
int n;
T* t;
T zero_val;
inline T merge(T a, T b)
{
return a + b; // MODIFY FOR EACH PROBLEM
}