Skip to content

Instantly share code, notes, and snippets.

Avatar

Izan Beltran izanbf1803

View GitHub Profile
@izanbf1803
izanbf1803 / factorial_info.py
Created Jan 5, 2019
Find last m non-zero digits of n!
View factorial_info.py
# 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 Jan 5, 2019
Solución del problema "A gas station too far"
View A gas station too far.py
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
View gen_2019.cc
#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 Dec 31, 2018
Primes class to factorize integers.
View primes.py
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 Dec 31, 2018
Draw Pascal's triangle with simple format
View pascal_triangle.cc
#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 Dec 5, 2018
Console output color
View colormod.h
#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 Nov 3, 2018
Reduce memory usage in dataframe.
View reduce_mem_usage.py
# 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 Oct 18, 2018
Sample of generating functions implementation using Sympy.
View gen_func_sample.py
"""
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 Jan 15, 2019
Compact recursive implementation of basic Segment Tree.
View SegmentTree.cc
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
}
@izanbf1803
izanbf1803 / IterativeSegmentTree.cc
Last active Sep 6, 2018
Compact and fast Iterative Segment Tree basic implementation.
View IterativeSegmentTree.cc
// Reference: http://codeforces.com/blog/entry/18051
template <typename T>
struct SegmentTree {
int n;
T* t;
T zero_val;
inline T operation(T a, T b)
{
return max(a, b);