├── Binary Search and Ternary Search ├── Aplications.cpp ├── BS.cpp ├── LowerBound.cpp ├── STL.cpp ├── TS.cpp ├── UpperBound.cpp ├── parallel_binary_search.cpp └── recursive_parallel_binary_search.cpp ├── Dynamic programming and common problems ├── Digitdp.cpp ├── Knapsack.cpp ├── aliens_trick.cpp ├── bitwise_digit_dp.cpp ├── broken_profile.cpp ├── cht.cpp ├── divideandconquer.cpp ├── dynamic_cht.cpp ├── exchange_arguments.cpp ├── expected_value.cpp ├── inclusion_exclusion.cpp ├── largest-sum-contiguous-subarray.cpp ├── largest_square.cpp ├── lcs.cpp ├── lichao.cpp ├── lis.cpp ├── max_matrix_path.cpp ├── optimization_with_fft.cpp ├── permutations.cpp ├── sos_dp.cpp ├── steiner_tree.cpp ├── subsequences_string.cpp ├── subset_sum.cpp ├── suffix_sum.cpp └── tip.cpp ├── Geometry ├── ConvexHull.cpp ├── LineSweep.cpp ├── convex_hull_point_location.cpp ├── dynamic_ch.cpp ├── halfplane_intersection.cpp ├── kd_tree.cpp ├── line_trick.cpp ├── minkowski.cpp ├── points_and_vectors.cpp ├── polygon_area.cpp ├── polygon_isomorfism.cpp ├── polygons_distance.cpp └── smallest_enclosing_circle.cpp ├── Graph ├── BFS.cpp ├── DFS.cpp ├── Dijkstra.cpp ├── Floyd_Warshall.cpp ├── Ford_Fulkerson.cpp ├── Grafo_Bipartido.cpp ├── Kruskal.cpp ├── LCA.cpp ├── MatrixDijkstra.cpp ├── Prim.cpp ├── Topological_Sort.cpp ├── TreeDiameter.cpp ├── articulation_points.cpp ├── bipartite.cpp ├── block-cut-tree.cpp ├── bridges.cpp ├── caminhoeuleriano.cpp ├── caminhoeuleriano2.cpp ├── centroid_decomposition.cpp ├── centroid_decomposition2.cpp ├── cycle_detection.cpp ├── dinic.cpp ├── dominator_tree.cpp ├── dsu.cpp ├── dsu_rollback.cpp ├── erdos_gallai.cpp ├── eulertour.cpp ├── flow_with_minimum_capacities.cpp ├── hall_theorem.cpp ├── hld.cpp ├── hld_edge.cpp ├── hopcroft_karp.cpp ├── hungarian.cpp ├── kuhn.cpp ├── link_cut_tree_edge.cpp ├── link_cut_tree_vertex.cpp ├── max_matching_without_one_vertex.cpp ├── mincostflow.cpp ├── mo_dsu.cpp ├── mo_trees.cpp ├── mo_trees_edges.cpp ├── push_relabel.cpp ├── reroot.cpp ├── rmq_tree.cpp ├── sack.cpp ├── scc.cpp ├── segtree_graph.cpp ├── stable_matching.cpp ├── strong_orientation.cpp ├── tree_isomorfism.cpp ├── two_sat.cpp └── virtual_tree.cpp ├── Math ├── baby_step_gigant_step.cpp ├── berlekamp_massey.cpp ├── binomial_theorem.cpp ├── catalan.cpp ├── crivo.cpp ├── crt.cpp ├── crt_trick.cpp ├── diophantine.cpp ├── division_trick.cpp ├── divisors.cpp ├── extended_euclidean.cpp ├── fft.cpp ├── fraction.cpp ├── fwht.cpp ├── gaussian_elimination.cpp ├── gaussian_elimination2.cpp ├── lagrange.cpp ├── lucas_theorem.cpp ├── markov.cpp ├── matrix_exponentiation.cpp ├── matrix_exponentiation2.cpp ├── matrix_inverse_and_determinant.cpp ├── max_xor_subsequence.cpp ├── mobius.cpp ├── mobius2.cpp ├── modular_arithmetic.cpp ├── ntt.cpp ├── operadores_binarios.cpp ├── pollard_rho.cpp ├── primefactors.cpp ├── primefactors2.cpp ├── segmentedsieve.cpp ├── simplex.cpp ├── stars_and_bars.cpp ├── totient.cpp └── xor_trie.cpp ├── Miscellaneous ├── bellman_ford_inequalities.cpp ├── bitmasks.cpp ├── coordinate_compression.cpp ├── inversion_count.cpp ├── max_plus_convolution.cpp ├── meetinthemiddle.cpp ├── prefix_sum_2d.cpp ├── rectangle_union.cpp ├── segment_covering.cpp ├── sprague_grundy.cpp ├── stack_trick.cpp ├── sum_hash.cpp ├── tower_of_hanoi.cpp └── two_pointers.cpp ├── Notebook (based on the stanfordacm script) ├── README.md ├── code │ ├── Aplications.cpp │ ├── BFS.cpp │ ├── BS.cpp │ ├── ConvexHull.cpp │ ├── DFS.cpp │ ├── Digitdp.cpp │ ├── Dijkstra.cpp │ ├── Floyd_Warshall.cpp │ ├── Ford_Fulkerson.cpp │ ├── Grafo_Bipartido.cpp │ ├── Knapsack.cpp │ ├── Kruskal.cpp │ ├── LCA.cpp │ ├── LineSweep.cpp │ ├── LowerBound.cpp │ ├── MatrixDijkstra.cpp │ ├── Prim.cpp │ ├── STL.cpp │ ├── STL.txt │ ├── SegTree.cpp │ ├── SegTree_pa.cpp │ ├── Segtree2.cpp │ ├── TS.cpp │ ├── Topological_Sort.cpp │ ├── TreeDiameter.cpp │ ├── UpperBound.cpp │ ├── __int128.cpp │ ├── aho_corasick.cpp │ ├── aliens_trick.cpp │ ├── articulation_points.cpp │ ├── baby_step_gigant_step.cpp │ ├── berlekamp_massey.cpp │ ├── binary_lifting.cpp │ ├── binomial_theorem.cpp │ ├── binomial_theorem.md │ ├── bipartite.cpp │ ├── bit2d.cpp │ ├── bitmasks.cpp │ ├── bitwise_digit_dp.cpp │ ├── block-cut-tree.cpp │ ├── bridges.cpp │ ├── broken_profile.cpp │ ├── caminhoeuleriano.cpp │ ├── caminhoeuleriano2.cpp │ ├── catalan.cpp │ ├── centroid_decomposition.cpp │ ├── centroid_decomposition2.cpp │ ├── chicken_mcnugget.md │ ├── cht.cpp │ ├── color_update.cpp │ ├── convex_hull_point_location.cpp │ ├── coordinate_compression.cpp │ ├── crivo.cpp │ ├── crt.cpp │ ├── cycle_detection.cpp │ ├── de_bruijin.cpp │ ├── dinic.cpp │ ├── diophantine.cpp │ ├── divideandconquer.cpp │ ├── divisors.cpp │ ├── dominator_tree.cpp │ ├── dsu.cpp │ ├── dsu_rollback.cpp │ ├── dynamic_ch.cpp │ ├── dynamic_cht.cpp │ ├── erdos_gallai.cpp │ ├── eulertour.cpp │ ├── exchange_arguments.cpp │ ├── execution_time.cpp │ ├── expected_value.cpp │ ├── extended_euclidean.cpp │ ├── fenwick.cpp │ ├── fenwick2.cpp │ ├── fenwick2D.cpp │ ├── fenwick3.cpp │ ├── fft.cpp │ ├── flow_with_minimum_capacities.cpp │ ├── fraction.cpp │ ├── fwht.cpp │ ├── gaussian_elimination.cpp │ ├── gaussian_elimination2.cpp │ ├── graph_notes.md │ ├── halfplane_intersection.cpp │ ├── hall_theorem.cpp │ ├── hld.cpp │ ├── hld_edge.cpp │ ├── hopcroft_karp.cpp │ ├── hungarian.cpp │ ├── implicit_seg.cpp │ ├── inversion_count.cpp │ ├── kd_tree.cpp │ ├── kmp.cpp │ ├── lagrange.cpp │ ├── largest-sum-contiguous-subarray.cpp │ ├── largest_square.cpp │ ├── lcs.cpp │ ├── lichao.cpp │ ├── link_cut_tree_edge.cpp │ ├── link_cut_tree_vertex.cpp │ ├── lis.cpp │ ├── lower_bound_segtree.cpp │ ├── manacher.cpp │ ├── matrix_exponentiation.cpp │ ├── matrix_exponentiation2.cpp │ ├── matrix_inverse_and_determinant.cpp │ ├── max_matrix_path.cpp │ ├── max_xor_subsequence.cpp │ ├── meetinthemiddle.cpp │ ├── mergesorttree.cpp │ ├── min_queue.cpp │ ├── min_suffix.cpp │ ├── mincostflow.cpp │ ├── minkowski.cpp │ ├── mo.cpp │ ├── mo_dsu.cpp │ ├── mo_trees.cpp │ ├── mo_trees_edges.cpp │ ├── mo_update.cpp │ ├── mobius.cpp │ ├── mobius2.cpp │ ├── modular_arithmetic.cpp │ ├── ntt.cpp │ ├── operadores_binarios.cpp │ ├── ordered_set.cpp │ ├── parallel_binary_search.cpp │ ├── persistent_seg.cpp │ ├── persistent_seg2.cpp │ ├── points_and_vectors.cpp │ ├── pollard_rho.cpp │ ├── polygon_area.cpp │ ├── polygon_isomorfism.cpp │ ├── polygons_distance.cpp │ ├── prefix_sum_2d.cpp │ ├── primefactors.cpp │ ├── primefactors2.cpp │ ├── rabin-karp.cpp │ ├── rand.cpp │ ├── reroot.cpp │ ├── rmq.cpp │ ├── rmq_tree.cpp │ ├── runner.py │ ├── runner2.py │ ├── sack.cpp │ ├── scc.cpp │ ├── segment_covering.cpp │ ├── segmentedsieve.cpp │ ├── segtree2d.cpp │ ├── segtree_lazy.cpp │ ├── segtree_max_seg_sum.cpp │ ├── simplex.cpp │ ├── smallest_enclosing_circle.cpp │ ├── sos_dp.cpp │ ├── sparsetable.cpp │ ├── sprague_grundy.cpp │ ├── stable_matching.cpp │ ├── stack_trick.cpp │ ├── stars_and_bars.cpp │ ├── steiner_tree.cpp │ ├── stringhashing.cpp │ ├── stringhashing2.cpp │ ├── strong_orientation.cpp │ ├── subsequences_string.cpp │ ├── subset_sum.cpp │ ├── substring_fft.cpp │ ├── suffix_array.cpp │ ├── suffix_automaton.cpp │ ├── suffix_sum.cpp │ ├── sum_hash.cpp │ ├── tip.cpp │ ├── totient.cpp │ ├── tower_of_hanoi.cpp │ ├── treap.cpp │ ├── treap2.cpp │ ├── tree_isomorfism.cpp │ ├── two_pointers.cpp │ ├── two_sat.cpp │ ├── virtual_tree.cpp │ ├── xor_trie.cpp │ └── z-function.cpp ├── contents.tex ├── contents.txt ├── generate_pdf.py ├── make_contents.py ├── notebook.aux ├── notebook.fdb_latexmk ├── notebook.fls ├── notebook.log ├── notebook.out ├── notebook.pdf ├── notebook.tex └── notebook.toc ├── Problem Solving ├── Fase zero 2025 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ └── m.cpp ├── Final 2017 │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ └── m.cpp ├── Final 2018 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── f.cpp │ ├── h.cpp │ ├── i.cpp │ ├── l.cpp │ └── m.cpp ├── Final 2019 │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ └── m.cpp ├── Final 2020 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ ├── m.cpp │ └── n.cpp ├── Final 2021 │ ├── b.cpp │ ├── c.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ └── m.cpp ├── Final 2022 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── h.cpp │ ├── i.cpp │ ├── k.cpp │ ├── l.cpp │ └── m.cpp ├── Final 2023 │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ └── m.cpp ├── Final 2024 │ ├── a.cpp │ ├── b.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ └── l.cpp ├── GEMA PC 1.0 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ └── e.cpp ├── GEMA PC 2.0 │ ├── a.c │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ └── f.cpp ├── GEMA PC 3.0 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ └── e.cpp ├── Latam 2023 │ ├── a.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── j.cpp │ ├── k.cpp │ └── l.cpp ├── Latam 2024 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── k.cpp │ └── l.cpp ├── Primeira Seletiva GEMA │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ ├── m.cpp │ └── n.cpp ├── Subregional 2013 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ └── j.cpp ├── Subregional 2017 │ ├── a.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── i.cpp │ ├── j.cpp │ ├── l.cpp │ └── m.cpp ├── Subregional 2018 │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── i.cpp │ ├── j.cpp │ └── l.cpp ├── Subregional 2019 │ ├── a.cpp │ ├── b.cpp │ ├── d.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ └── m.cpp ├── Subregional 2020 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── k.cpp │ ├── l.cpp │ ├── m.cpp │ ├── n.cpp │ └── o.cpp ├── Subregional 2021 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ ├── m.cpp │ └── n.cpp ├── Subregional 2022 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ ├── m.cpp │ └── n.cpp ├── Subregional 2023 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── k.cpp │ ├── l.cpp │ └── m.cpp ├── Subregional 2024 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ └── l.cpp ├── TAP 2023 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ ├── k.cpp │ ├── l.cpp │ ├── m.cpp │ └── n.cpp ├── UFPE try-outs 2020 │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ ├── f.cpp │ ├── g.cpp │ ├── h.cpp │ ├── i.cpp │ ├── j.cpp │ └── k.cpp ├── UFPE try-outs 2021 │ ├── b.cpp │ ├── c.cpp │ ├── d.cpp │ ├── e.cpp │ └── f.cpp └── editorial_secomp_2022.md ├── README.md ├── STL ├── STL.txt └── ordered_set.cpp ├── Strings ├── aho_corasick.cpp ├── de_bruijin.cpp ├── kmp.cpp ├── manacher.cpp ├── min_suffix.cpp ├── rabin-karp.cpp ├── stringhashing.cpp ├── stringhashing2.cpp ├── substring_fft.cpp ├── suffix_array.cpp ├── suffix_automaton.cpp └── z-function.cpp ├── Structures ├── SegTree.cpp ├── SegTree_pa.cpp ├── Segtree2.cpp ├── binary_lifting.cpp ├── bit2d.cpp ├── color_update.cpp ├── fenwick.cpp ├── fenwick2.cpp ├── fenwick2D.cpp ├── fenwick3.cpp ├── implicit_seg.cpp ├── lower_bound_segtree.cpp ├── mergesorttree.cpp ├── min_queue.cpp ├── mo.cpp ├── mo_update.cpp ├── persistent_seg.cpp ├── persistent_seg2.cpp ├── rmq.cpp ├── segtree2d.cpp ├── segtree_lazy.cpp ├── segtree_max_seg_sum.cpp ├── sparsetable.cpp ├── treap.cpp └── treap2.cpp ├── Theorems and Formulas ├── binomial_theorem.md ├── chicken_mcnugget.md ├── graph_notes.md └── manhattan_and_chebyshev.md └── Utils ├── __int128.cpp ├── execution_time.cpp ├── rand.cpp ├── runner.py └── runner2.py /Binary Search and Ternary Search/BS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | 7 | vector v; 8 | int binarysearch (int n , int x) 9 | { 10 | int i = 0 ; 11 | int f = n - 1 ; 12 | int m ; 13 | 14 | while(i <= f) 15 | { 16 | m = (i + f) / 2 ; 17 | 18 | if(v[m] == x) return m + 1 ; 19 | if(v[m] < x) i = m + 1 ; 20 | if(v[m] > x) f = m - 1 ; 21 | } 22 | 23 | return 0 ; 24 | } 25 | int main () 26 | { 27 | int n , aux , m ; 28 | 29 | cin >> n ; 30 | 31 | for (int i = 0 ; i < n ; i++) 32 | { 33 | cin >> aux ; 34 | v.pb(aux); 35 | } 36 | 37 | sort(v.begin() , v.end()); 38 | 39 | cin >> m ; 40 | cout << binarysearch(n , m) << endl ; 41 | 42 | return 0 ; 43 | } -------------------------------------------------------------------------------- /Binary Search and Ternary Search/LowerBound.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define DEBUG 0 17 | #define MAXN 1000001 18 | #define mod 1000000007 19 | // first element >= x 20 | vector k(MAXN); 21 | int lower(int l, int r, int x) // first element >= x 22 | { 23 | while (l < r) 24 | { 25 | // int mid = l + (r - l) / 2; se tiver numero negativo 26 | int mid = (l + r) >> 1; 27 | (x <= k[mid]) ? r = mid : l = mid + 1; 28 | } 29 | return k[l]; 30 | } 31 | -------------------------------------------------------------------------------- /Binary Search and Ternary Search/STL.cpp: -------------------------------------------------------------------------------- 1 | // lower - primeiro maior ou igual a x 2 | // upper - ultimo menor ou igual a x 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define lli long long int 8 | #define pb push_back 9 | 10 | vector v ; 11 | int main() 12 | { 13 | int n , aux ; 14 | cin >> n ; 15 | 16 | for (int i = 0 ; i < n ; i++) 17 | { 18 | cin >> aux ; 19 | v.pb(aux); 20 | } 21 | 22 | sort(v.begin() , v.end()); 23 | 24 | int q ; 25 | cin >> q ; 26 | 27 | while (q--) 28 | { 29 | cin >> aux ; 30 | vector :: iterator low = lower_bound (v.begin() , v.end() , aux) ; 31 | vector :: iterator up = upper_bound (v.begin() , v.end() , aux) ; 32 | 33 | cout << (low - v.begin()) << " " << (up - v.begin()) - 1 << endl ; 34 | } 35 | 36 | return 0 ; 37 | } 38 | -------------------------------------------------------------------------------- /Binary Search and Ternary Search/TS.cpp: -------------------------------------------------------------------------------- 1 | // achar valor maximo da funcao 2 | for (int i = 0; i < 400; i++) 3 | { 4 | double m1 = l + (r - l) / 3; 5 | double m2 = r - (r - l) / 3; 6 | double f1 = f(m1); 7 | double f2 = f(m2); 8 | if (f1 < f2) 9 | l = m1; 10 | else 11 | r = m2; 12 | } -------------------------------------------------------------------------------- /Binary Search and Ternary Search/UpperBound.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define DEBUG 0 17 | #define MAXN 1000001 18 | #define mod 1000000007 19 | // last element <= x 20 | vector k(MAXN); 21 | int upper(int l, int r, int x) 22 | { 23 | while (l < r) 24 | { 25 | int mid = (l + r + 1) >> 1; 26 | (k[mid] <= x) ? l = mid : r = mid - 1; 27 | } 28 | return k[l]; 29 | } 30 | -------------------------------------------------------------------------------- /Dynamic programming and common problems/Digitdp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pb push_back 6 | #define pi pair 7 | #define fir first 8 | #define sec second 9 | #define MAXN 2001 10 | #define mod 1000000007 11 | 12 | int dp[20][20 * 9][2]; // a,b <= 10^18 13 | vector dig; 14 | 15 | int solve(int i, int j, int k) 16 | { 17 | if (i == dig.size()) 18 | return (k) ? dp[i][j][k] = j : dp[i][j][k] = 0; 19 | if (dp[i][j][k] != -1) 20 | return dp[i][j][k]; 21 | int sum = 0; 22 | if (k) 23 | for (int f = 0; f <= 9; f++) 24 | sum += solve(i + 1, j + f, k); 25 | if (!k) 26 | for (int f = 0; f <= dig[i]; f++) 27 | sum += solve(i + 1, j + f, (dig[i] != f) ? 1 : 0); 28 | return dp[i][j][k] = sum; 29 | } 30 | void get_digits(int n) 31 | { 32 | dig.clear(); 33 | while (n) 34 | { 35 | dig.pb(n % 10); 36 | n = n / 10; 37 | } 38 | reverse(dig.begin(), dig.end()); 39 | } 40 | signed main() 41 | { 42 | ios_base::sync_with_stdio(false); 43 | cin.tie(NULL); 44 | int a, b; 45 | cin >> a >> b; 46 | get_digits(a); 47 | memset(dp, -1, sizeof(dp)); 48 | int aa = solve(0, 0, 0); 49 | get_digits(b + 1); 50 | memset(dp, -1, sizeof(dp)); 51 | int bb = solve(0, 0, 0); 52 | cout << bb - aa << endl; 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Dynamic programming and common problems/largest_square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define pb push_back 6 | #define int long long int 7 | #define double long double 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 1001 13 | #define mod 1000000007 14 | 15 | signed main() 16 | { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(NULL); 19 | int n; 20 | cin >> n; 21 | int v[n][n]; 22 | int dp[n][n]; 23 | for (int i = 0; i < n; i++) 24 | for (int j = 0; j < n; j++) 25 | cin >> v[i][j]; 26 | int ans = 0; 27 | for (int i = 0; i < n; i++) 28 | { 29 | for (int j = 0; j < n; j++) 30 | { 31 | dp[i][j] = v[i][j]; 32 | if (i && j && dp[i][j]) 33 | dp[i][j] = min({dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]}) + 1; 34 | ans = max(ans, dp[i][j]); 35 | } 36 | } 37 | cout << ans * ans << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Dynamic programming and common problems/lcs.cpp: -------------------------------------------------------------------------------- 1 | //Dadas duas sequências s1 e s2, uma de tamanho n e outra de tamanho m, qual a maior subsequência comum às duas? 2 | 3 | // uma subsequência de s é um subconjunto dos elementos de s na mesma ordem em que apareciam antes. 4 | // isto significa que {1, 3, 5} é uma subsequência de {1, 2, 3, 4, 5}, mesmo 1 não estando do lado do 3. 5 | #include 6 | using namespace std; 7 | 8 | #define lli long long int 9 | #define pb push_back 10 | #define in insert 11 | #define pi pair 12 | #define pii pair 13 | #define mp make_pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1001 17 | #define INF 1000000000 18 | 19 | int v1[MAXN]; 20 | int v2[MAXN]; 21 | int dp[MAXN][MAXN]; 22 | 23 | void lcs(int m, int n) 24 | { 25 | for (int i = 0; i <= m; i++) 26 | { 27 | for (int j = 0; j <= n; j++) 28 | { 29 | if (i == 0 || j == 0) //se uma das sequências for vazia 30 | dp[i][j] = 0; 31 | else if (v1[i - 1] == v2[j - 1]) // se eh igual, adiciono a lcs e subtraio dos dois 32 | dp[i][j] = dp[i - 1][j - 1] + 1; 33 | else 34 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // se nao retorno o maximo entre tirar um dos dois caras 35 | } 36 | } 37 | cout << dp[m][n] << endl; 38 | } 39 | signed main() 40 | { 41 | int n, m; 42 | cin >> n >> m; 43 | for (int i = 0; i < n; i++) 44 | cin >> v1[i]; 45 | for (int i = 0; i < m; i++) 46 | cin >> v2[i]; 47 | lcs(n, m); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Dynamic programming and common problems/lis.cpp: -------------------------------------------------------------------------------- 1 | // dada uma sequência s qualquer, descobrir o tamanho da maior subsequência crescente de s 2 | // uma subsequência de s é qualquer subconjunto de elementos de s. 3 | // Para cada novo número, você tem duas operações possíveis: 4 | // 1 - Colocar o novo número no topo de uma pilha se ele não superar o que já está em seu topo; 5 | // ou 6 | // 2 - Criar uma nova pilha à direita de todas as outras e colocar o novo número lá. 7 | // ao final do processo a nossa pilha terá os elementos da lis. 8 | #include 9 | using namespace std; 10 | 11 | #define lli long long int 12 | #define pb push_back 13 | #define in insert 14 | #define pi pair 15 | #define pd pair 16 | #define pib pair 17 | #define mp make_pair 18 | #define fir first 19 | #define sec second 20 | #define MAXN 200001 21 | #define MAXL 1000001 22 | #define mod 1000000007 23 | 24 | vector v; 25 | 26 | int lis() 27 | { 28 | vector q; 29 | for (int i = 0; i < v.size(); i++) 30 | { 31 | vector::iterator it = lower_bound(q.begin(), q.end(), v[i]); 32 | if (it == q.end()) 33 | q.pb(v[i]); 34 | else 35 | *it = v[i]; 36 | } 37 | for (int i = 0; i < q.size(); i++) 38 | cout << q[i] << " "; 39 | cout << endl; 40 | return q.size(); 41 | } 42 | signed main() 43 | { 44 | int n; 45 | cin >> n; 46 | v.resize(n); 47 | for (int i = 0; i < n; i++) 48 | cin >> v[i]; 49 | cout << lis() << endl; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Dynamic programming and common problems/max_matrix_path.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define pb push_back 6 | #define int long long int 7 | #define mp make_pair 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 301 13 | #define MAXL 20 14 | #define mod 1000000007 15 | #define INF 1000000001 16 | 17 | int n; 18 | int grid[MAXN][MAXN]; 19 | int dp[MAXN][MAXN]; 20 | 21 | int solve(int i, int j) 22 | { 23 | if (i == n - 1 && j == n - 1) 24 | return grid[i][j]; 25 | if (dp[i][j] != -1) 26 | return dp[i][j]; 27 | if (i + 1 < n && j + 1 < n) 28 | return dp[i][j] = grid[i][j] + max(solve(i + 1, j), solve(i, j + 1)); 29 | if (i + 1 < n) 30 | return dp[i][j] = grid[i][j] + solve(i + 1, j); 31 | if (j + 1 < n) 32 | return dp[i][j] = grid[i][j] + solve(i, j + 1); 33 | } 34 | signed main() 35 | { 36 | cin >> n; 37 | for (int i = 0; i < n; i++) 38 | for (int j = 0; j < n; j++) 39 | cin >> grid[i][j]; 40 | memset(dp, -1, sizeof(dp)); 41 | cout << solve(0, 0) << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Dynamic programming and common problems/subsequences_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define mp make_pair 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 100 13 | #define MAXL 20 14 | #define mod 998244353 15 | 16 | void count(string a, string b) 17 | { 18 | int m = a.size(); 19 | int n = b.size(); 20 | int dp[m + 1][n + 1] = {{0}}; 21 | for (int i = 0; i <= n; ++i) 22 | dp[0][i] = 0; 23 | for (int i = 0; i <= m; ++i) 24 | dp[i][0] = 1; 25 | for (int i = 1; i <= m; i++) 26 | { 27 | for (int j = 1; j <= n; j++) 28 | { 29 | if (a[i - 1] == b[j - 1]) 30 | dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 31 | else 32 | dp[i][j] = dp[i - 1][j]; 33 | } 34 | } 35 | cout << dp[m][n] << endl; 36 | } 37 | signed main() 38 | { 39 | string a, b; 40 | cin >> a >> b; 41 | count(a, b); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Dynamic programming and common problems/tip.cpp: -------------------------------------------------------------------------------- 1 | // dados os valores de moedas v1, v2, ... vn é possivel formar um valor m como combinação de moedas 2 | // para isso basta montar uma dp inicializada com -1 3 | // nesse caso a dp só precisa de um parametro q é = valor restante ate o limite 4 | // mas podem existir variações do problema q precise de mais coisas 5 | // se em achar alguma combinação válida retorna 1, se não retorna 0 6 | #include 7 | using namespace std; 8 | 9 | #define lli long long int 10 | #define pb push_back 11 | #define in insert 12 | #define pi pair 13 | #define pd pair 14 | #define pib pair 15 | #define mp make_pair 16 | #define fir first 17 | #define sec second 18 | #define MAXN 200001 19 | #define MAXL 10001 20 | #define mod 1000000007 21 | 22 | int dp[MAXN]; 23 | vector v; 24 | 25 | int solve(int rem) 26 | { 27 | if (rem == 0) 28 | return 1; 29 | if (rem < 0) 30 | return 0; 31 | if (dp[rem] >= 0) 32 | return dp[rem]; 33 | for (int i = 0; i < v.size(); i++) 34 | if (solve(rem - v[i])) 35 | return dp[rem - v[i]] = 1; 36 | return dp[rem] = 0; 37 | } 38 | signed main() 39 | { 40 | int n, m; 41 | cin >> n >> m; 42 | v.resize(n); 43 | for (int i = 0; i < n; i++) 44 | cin >> v[i]; 45 | memset(dp, -1, sizeof(dp)); 46 | (solve(m)) ? cout << "Yes\n" : cout << "No\n"; 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Geometry/polygon_area.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define endl '\n' 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 200005 12 | #define mod 998244353 13 | 14 | double area(vector fig) 15 | { 16 | double res = 0; 17 | for (unsigned i = 0; i < fig.size(); i++) 18 | { 19 | pi p = i ? fig[i - 1] : fig.back(); 20 | pi q = fig[i]; 21 | res += (p.fir - q.fir) * (p.sec + q.sec); 22 | } 23 | return fabs(res) / 2; 24 | } 25 | int cross(pi a, pi b) 26 | { 27 | return a.fir * b.sec - a.sec * b.fir; 28 | } 29 | double area2(vector fig) 30 | { 31 | double res = 0; 32 | for (unsigned i = 0; i < fig.size(); i++) 33 | { 34 | pi p = i ? fig[i - 1] : fig.back(); 35 | pi q = fig[i]; 36 | res += cross(p, q); 37 | } 38 | return fabs(res) / 2; 39 | } 40 | signed main() 41 | { 42 | ios_base::sync_with_stdio(false); 43 | cin.tie(NULL); 44 | return 0; 45 | } 46 | // achar area de um poligono 47 | // tomar cuiddado com a ordem 48 | // percorrer os vertices em sentido horário ou anti-horário 49 | -------------------------------------------------------------------------------- /Graph/BFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define DEBUG 1 17 | #define MAXN 1001 18 | #define mod 1000000007 19 | 20 | int n, m; 21 | vector adj[MAXN]; 22 | bool visited[MAXN]; 23 | 24 | void bfs(int s) 25 | { 26 | queue q; 27 | q.push(s); 28 | while (!q.empty()) 29 | { 30 | int v = q.front(); 31 | q.pop(); 32 | if (visited[v]) 33 | continue; 34 | visited[v] = true; 35 | for (auto const &u : adj[v]) 36 | if (!visited[u]) 37 | q.push(u); 38 | } 39 | } 40 | signed main() 41 | { 42 | ios_base::sync_with_stdio(false); 43 | cin.tie(NULL); 44 | cin >> n >> m; 45 | for (int i = 0; i < m; i++) 46 | { 47 | int a, b, c; 48 | cin >> a >> b >> c; 49 | a--, b--; 50 | adj[a].pb(b); 51 | adj[b].pb(a); 52 | } 53 | bfs(0); 54 | } 55 | -------------------------------------------------------------------------------- /Graph/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXN 500000 5 | 6 | int n , m ; 7 | int visited [MAXN] ; 8 | vector adj_list [MAXN] ; 9 | 10 | void dfs (int x) 11 | { 12 | for (int i = 0 ; i < adj_list[x].size() ; i++) 13 | { 14 | int v = adj_list[x][i] ; 15 | 16 | if(visited[v] == -1) 17 | { 18 | visited[v] = visited[x] ; 19 | dfs(v) ; 20 | } 21 | } 22 | } 23 | void initialize () 24 | { 25 | for (int i = 1 ; i <= n ; i++) 26 | { 27 | visited[i] = -1 ; 28 | } 29 | } 30 | int main () 31 | { 32 | int a , b ; 33 | 34 | cin >> n >> m ; 35 | 36 | initialize(); 37 | 38 | for (int i = 1 ; i <= m ; i++) 39 | { 40 | cin >> a >> b ; 41 | 42 | adj_list[a].push_back(b) ; 43 | adj_list[b].push_back(a) ; 44 | } 45 | 46 | dfs(1) ; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Graph/Floyd_Warshall.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define lli long long int 6 | #define MAXN 10000 7 | #define INF 999999 8 | 9 | int n , m , a , b , c ; 10 | int dist [MAXN][MAXN] ; 11 | 12 | void floyd_warshall () 13 | { 14 | for (int k = 0 ; k < n ; k++) 15 | { 16 | for (int i = 0 ; i < n ; i++) 17 | { 18 | for (int j = 0 ; j < n ; j++) 19 | { 20 | dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]) ; 21 | } 22 | } 23 | } 24 | } 25 | void initialize () 26 | { 27 | for (int i = 0 ; i < n ; i++) 28 | { 29 | for (int j = 0 ; j < n ; j++) 30 | { 31 | if (i == j) 32 | { 33 | dist[i][j] = 0 ; 34 | } 35 | else 36 | { 37 | dist[i][j] = INF ; 38 | } 39 | 40 | } 41 | } 42 | } 43 | int main() 44 | { 45 | cin >> n >> m ; 46 | 47 | initialize () ; 48 | 49 | for (int i = 0 ; i < m ; i++) 50 | { 51 | cin >> a >> b >> c ; 52 | dist [a][b] = min (dist[a][b] , c) ; 53 | dist [b][a] = min (dist[b][a] , c) ; 54 | } 55 | 56 | floyd_warshall () ; 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Graph/Topological_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | #define MAXN 10000 7 | 8 | int n , m , a , b ; 9 | vector adj [MAXN] ; 10 | int grau [MAXN]; 11 | vector order ; 12 | 13 | bool topological_sort () 14 | { 15 | int ini = 0 ; 16 | 17 | while (ini < order.size()) 18 | { 19 | int atual = order[ini] ; 20 | ini++ ; 21 | 22 | for (int i = 0 ; i < adj[atual].size() ; i++) 23 | { 24 | int v = adj[atual][i] ; 25 | grau[v]-- ; 26 | 27 | if (grau[v] == 0) 28 | { 29 | order.pb(v) ; 30 | } 31 | } 32 | } 33 | 34 | return (order.size() == n) ? true : false ; 35 | } 36 | int main () 37 | { 38 | ios_base::sync_with_stdio(false) ; 39 | cin.tie(NULL) ; 40 | 41 | cin >> n >> m ; 42 | 43 | for (int i = 1 ; i <= m ; i++) 44 | { 45 | cin >> a >> b ; 46 | grau[a]++ ; 47 | adj[b].pb(a) ; 48 | } 49 | 50 | for (int i = 1 ; i <= n ; i++) 51 | { 52 | if (grau[i] == 0) 53 | { 54 | order.pb(i) ; 55 | } 56 | } 57 | 58 | if (topological_sort()) 59 | { 60 | for (int i = 0 ; i < order.size() ; i++) 61 | { 62 | cout << order[i] << " " ; 63 | } 64 | 65 | cout << endl ; 66 | } 67 | else 68 | { 69 | cout << "Impossible\n" ; 70 | } 71 | 72 | return 0 ; 73 | } 74 | -------------------------------------------------------------------------------- /Graph/TreeDiameter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 100001 12 | #define mod 1000000007 13 | 14 | int diameter, best; 15 | vector adj[MAXN]; 16 | bool visited[MAXN]; 17 | 18 | void dfs(int s, int c) 19 | { 20 | if (c > diameter) 21 | { 22 | diameter = c; 23 | best = s; 24 | } 25 | visited[s] = true; 26 | for (auto const &i : adj[s]) 27 | if (!visited[i]) 28 | dfs2(i, c + 1); 29 | } 30 | signed main() 31 | { 32 | ios_base::sync_with_stdio(false); 33 | cin.tie(NULL); 34 | int q; 35 | cin >> q; 36 | while (q--) 37 | { 38 | int n; 39 | cin >> n; 40 | for (int i = 0; i < n; i++) 41 | adj[i].clear(); 42 | for (int i = 0; i < n - 1; i++) 43 | { 44 | int a, b; 45 | cin >> a >> b; 46 | a--, b--; 47 | adj[b].pb(a); 48 | adj[a].pb(b); 49 | } 50 | diameter = 0, best = 0; 51 | memset(visited, false, sizeof(visited)); 52 | dfs(1, 0); // achar o vertice mais distante a partir do vertice 0 53 | memset(visited, false, sizeof(visited)); 54 | dfs(best, 0); // achar o mais distante a partir do primeiro vertice que achamos 55 | cout << diameter << endl; 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Graph/bipartite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 500001 18 | #define mod 1000000007 19 | 20 | int n, m; 21 | vector adj[MAXN]; 22 | 23 | bool is() 24 | { 25 | vector c(n, -1); 26 | bool is = 1; 27 | queue q; 28 | for (int st = 0; st < n; st++) 29 | { 30 | if (c[st] == -1) 31 | { 32 | q.push(st); 33 | c[st] = 0; 34 | while (!q.empty()) 35 | { 36 | int v = q.front(); 37 | q.pop(); 38 | for (int u : adj[v]) 39 | { 40 | if (c[u] == -1) 41 | { 42 | c[u] = c[v] ^ 1; 43 | q.push(u); 44 | } 45 | else 46 | { 47 | is &= (c[u] != c[v]); 48 | } 49 | } 50 | } 51 | } 52 | } 53 | return is; 54 | } 55 | signed main() 56 | { 57 | ios_base::sync_with_stdio(false); 58 | cin.tie(NULL); 59 | cin >> n >> m; 60 | for (int i = 0; i < m; i++) 61 | { 62 | int a, b; 63 | cin >> a >> b; 64 | a--, b--; 65 | adj[a].pb(b); 66 | adj[b].pb(a); 67 | } 68 | cout << is() << endl; 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Graph/dsu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 2001 18 | #define mod 1000000007 19 | 20 | struct dsu 21 | { 22 | int tot; 23 | vector parent; 24 | vector sz; 25 | 26 | dsu(int n) 27 | { 28 | parent.resize(n); 29 | sz.resize(n); 30 | tot = n; 31 | for (int i = 0; i < n; i++) 32 | { 33 | parent[i] = i; 34 | sz[i] = 1; 35 | } 36 | } 37 | int find_set(int i) 38 | { 39 | return parent[i] = (parent[i] == i) ? i : find_set(parent[i]); 40 | } 41 | void make_set(int x, int y) 42 | { 43 | x = find_set(x), y = find_set(y); 44 | if (x != y) 45 | { 46 | if (sz[x] > sz[y]) 47 | swap(x, y); 48 | parent[x] = y; 49 | sz[y] += sz[x]; 50 | tot--; 51 | } 52 | } 53 | }; 54 | signed main() 55 | { 56 | ios_base::sync_with_stdio(false); 57 | cin.tie(NULL); 58 | int n; 59 | cin >> n; 60 | dsu d(n); 61 | int a, b; 62 | cin >> a >> b; 63 | d.make_set(a, b); 64 | d.find_set(a); 65 | } 66 | -------------------------------------------------------------------------------- /Math/crivo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | #define in insert 7 | #define pi pair 8 | #define pd pair 9 | #define pii pair 10 | #define mp make_pair 11 | #define fir first 12 | #define sec second 13 | #define MAXN 100001 14 | #define mod 1000000007 15 | 16 | bitset prime; 17 | 18 | void crivo () 19 | { 20 | prime.set(); 21 | prime[0] = false; 22 | prime[1] = false; 23 | for (int i = 2 ; i < MAXN ; i++) 24 | if(prime[i]) 25 | for(int j = 2 ; j * i < MAXN ; j++) 26 | prime[j * i] = false; 27 | } 28 | signed main() 29 | { 30 | crivo(); 31 | int q; 32 | cin >> q; 33 | while(q--) 34 | { 35 | int n; 36 | cin >> n; 37 | (prime[n]) ? cout << "YES\n" : cout << "NO\n" ; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Math/divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 5001 11 | #define mod 1000000007 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | int n; 18 | cin >> n; 19 | int ans = 0; 20 | for (int i = 1; i <= sqrt(n); i++) 21 | { 22 | if (!(n % i)) 23 | { 24 | ans++; 25 | if (n / i != i) 26 | ans++; 27 | } 28 | } 29 | cout << ans << endl; 30 | } 31 | -------------------------------------------------------------------------------- /Math/extended_euclidean.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pb push_back 6 | #define pi pair 7 | #define pii pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 200001 11 | #define mod 998244353 12 | 13 | int gcd(int a, int b, int &x, int &y) 14 | { 15 | if (b == 0) 16 | { 17 | x = 1; 18 | y = 0; 19 | return a; 20 | } 21 | int x1, y1; 22 | int d = gcd(b, a % b, x1, y1); 23 | x = y1; 24 | y = x1 - y1 * (a / b); 25 | return d; 26 | } 27 | signed main() 28 | { 29 | int n; 30 | cin >> n; 31 | int k = 2; 32 | while (1) 33 | { 34 | int x, y; 35 | if (gcd(k, n, x, y) == 1) 36 | { 37 | x = ((x % n) + n) % n; 38 | cout << x << endl; 39 | return 0; 40 | } 41 | k++; 42 | } 43 | return 0; 44 | } 45 | // achar os numeros x e y tal que: 46 | // a * x + b * y = gcd(a, b) 47 | 48 | // problema exemplo: 49 | // https://codeforces.com/group/btcK4I5D5f/contest/451372/problem/J 50 | // dado um numero k 51 | // quero achar um numero x, se possivel, tal que: 52 | // (k * x) % n = 1 53 | 54 | // k * x + n * y = 1 55 | // se gcd(k, n) = 1, então: 56 | // k * x + n * y = gcd(k, n) 57 | // note que, se gcd(k, n) > 1, logo nao existe solução -------------------------------------------------------------------------------- /Math/lucas_theorem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1000006 17 | #define mod 2 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int q; 24 | cin >> q; 25 | while (q--) 26 | { 27 | int n, k; 28 | cin >> n >> k; 29 | for (int i = 0; i < n; i++) 30 | { 31 | if (((n - 1) & i) == i) 32 | cout << k << " "; 33 | else 34 | cout << 0 << " "; 35 | } 36 | cout << endl; 37 | } 38 | } 39 | // https://codeforces.com/contest/2072/problem/F 40 | // problema interessante de um div3, calcular ncr(n, k) mod 2 41 | // ncr(n - 1, i) mod 2 = (((n - 1) & i) == i) 42 | 43 | // sejam m e n numeros inteiros nao negativos e p um numero primo 44 | // desenvolver n e m na base p 45 | // ou seja: 46 | // m = m[k]*p^(k) + m[k - 1]*p^(k - 1) + ... + m[0]*p^(0) 47 | // n = n[k]*p^(k) + n[k - 1]*p^(k - 1) + ... + n[0]*p^(0) 48 | 49 | // então: 50 | // ncr(m, n) mod p = produtorio de (ncr(m[i], n[i]) mod p) 51 | 52 | // dai pra generalizar pro mod 2 eh deboas, pq se tiver um bit setado em n[i] que nao ta setado em m[i], entao miou, vai dar zero -------------------------------------------------------------------------------- /Math/primefactors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define mp make_pair 8 | #define pi pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 501 12 | #define MAXL 20 13 | #define mod 1000000007 14 | 15 | vector facts; 16 | void primefactors(int n) 17 | { 18 | while (n % 2 == 0) 19 | { 20 | facts.pb(2); 21 | n = n / 2; 22 | } 23 | for (int i = 3; i <= sqrt(n); i += 2) 24 | { 25 | while (n % i == 0) 26 | { 27 | facts.pb(i); 28 | n = n / i; 29 | } 30 | } 31 | if (n > 2) 32 | facts.pb(n); 33 | } 34 | signed main() 35 | { 36 | int n; 37 | cin >> n; 38 | primefactors(n); 39 | sort(facts.begin(), facts.end()); 40 | for (auto const &i : facts) 41 | cout << i << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Math/primefactors2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define PI acos(-1) 11 | #define pb push_back 12 | #define int long long int 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define DEBUG 0 18 | #define MAXN 1000001 19 | #define mod 1000000007 20 | 21 | namespace primefactors 22 | { 23 | bitset prime; 24 | vector nxt(MAXN); 25 | vector factors; 26 | 27 | void crivo() 28 | { 29 | prime.set(); 30 | prime[0] = false, prime[1] = false; 31 | for (int i = 2; i < MAXN; i++) 32 | { 33 | if (prime[i]) 34 | { 35 | nxt[i] = i; 36 | for (int j = 2; j * i < MAXN; j++) 37 | { 38 | prime[j * i] = false; 39 | nxt[j * i] = i; 40 | } 41 | } 42 | } 43 | } 44 | void fact(int n) 45 | { 46 | factors.clear(); 47 | while (n > 1) 48 | { 49 | factors.pb(nxt[n]); 50 | n = n / nxt[n]; 51 | } 52 | } 53 | } 54 | signed main() 55 | { 56 | ios_base::sync_with_stdio(false); 57 | cin.tie(NULL); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Math/segmentedsieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define PI acos(-1) 11 | #define pb push_back 12 | #define int long long int 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define DEBUG 0 18 | #define MAXN 1000003 19 | #define mod 1000000007 20 | 21 | vector prime; 22 | 23 | void segmentedsieve(int l, int r) 24 | { 25 | int lim = sqrt(r); 26 | vector mark(lim + 1, false); 27 | vector primes; 28 | for (int i = 2; i <= lim; ++i) 29 | { 30 | if (!mark[i]) 31 | { 32 | primes.pb(i); 33 | for (int j = i * i; j <= lim; j += i) 34 | mark[j] = true; 35 | } 36 | } 37 | vector isprime(r - l + 1, true); 38 | for (int i : primes) 39 | for (int j = max(i * i, (l + i - 1) / i * i); j <= r; j += i) 40 | isprime[j - l] = false; 41 | if (l == 1) 42 | isprime[0] = false; 43 | for (int i = 0; i < isprime.size(); i++) 44 | if (isprime[i]) 45 | prime.pb(i + l); 46 | } 47 | signed main() 48 | { 49 | ios_base::sync_with_stdio(false); 50 | cin.tie(NULL); 51 | int l, r; 52 | cin >> l >> r; 53 | segmentedsieve(l, r); 54 | for (auto const &i : prime) 55 | cout << i << " "; 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Math/totient.cpp: -------------------------------------------------------------------------------- 1 | #define MAXN 100000 2 | 3 | int phi[MAXN]; 4 | 5 | void calc() 6 | { 7 | for (int i = 0; i < MAXN; i++) 8 | phi[i] = i; 9 | for (int i = 2; i < MAXN; i++) 10 | { 11 | if (phi[i] == i) 12 | { 13 | for (int j = i; j < MAXN; j += i) 14 | phi[j] -= phi[j] / i; 15 | } 16 | } 17 | } 18 | int calc_phi(int n) 19 | { 20 | int ans = n; 21 | for (int i = 2; i * i <= n; i++) 22 | { 23 | if (n % i == 0) 24 | { 25 | while (n % i == 0) 26 | n /= i; 27 | ans -= ans / i; 28 | } 29 | } 30 | if (n > 1) 31 | ans -= ans / n; 32 | return ans; 33 | } -------------------------------------------------------------------------------- /Miscellaneous/coordinate_compression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 500005 17 | #define mod 1000000007 18 | 19 | void compress(vector &v) 20 | { 21 | vector val; 22 | for (auto const &i : v) 23 | val.pb(i); 24 | sort(val.begin(), val.end()); 25 | val.erase(unique(val.begin(), val.end()), val.end()); 26 | for (auto &i : v) 27 | i = lower_bound(val.begin(), val.end(), i) - val.begin(); 28 | } -------------------------------------------------------------------------------- /Miscellaneous/prefix_sum_2d.cpp: -------------------------------------------------------------------------------- 1 | // https://cses.fi/problemset/task/1652 2 | #include 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define endl '\n' 7 | #define pb push_back 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 200005 13 | #define mod 1000000007 14 | 15 | int v[1001][1001]; 16 | int p[1001][1001]; 17 | 18 | int qry(int x1, int y1, int x2, int y2) 19 | { 20 | return p[x2 + 1][y2 + 1] - p[x2 + 1][y1] - p[x1][y2 + 1] + p[x1][y1]; 21 | } 22 | signed main() 23 | { 24 | int n, q; 25 | cin >> n >> q; 26 | for (int i = 0; i < n; i++) 27 | { 28 | for (int j = 0; j < n; j++) 29 | { 30 | char c; 31 | cin >> c; 32 | v[i][j] = (c == '*'); 33 | } 34 | } 35 | for (int i = 0; i < n; i++) 36 | { 37 | for (int j = 0; j < n; j++) 38 | { 39 | p[i + 1][j + 1] = p[i][j + 1] + p[i + 1][j] - p[i][j]; 40 | p[i + 1][j + 1] += v[i][j]; 41 | } 42 | } 43 | while (q--) 44 | { 45 | int a, b, c, d; 46 | cin >> a >> b >> c >> d; 47 | a--, b--, c--, d--; 48 | cout << qry(a, b, c, d) << endl; 49 | } 50 | return 0; 51 | } 52 | // prefix sum 2d 53 | // me enrolo pra codar toda vez, é bom deixar na lib -------------------------------------------------------------------------------- /Miscellaneous/stack_trick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 300001 11 | #define mod 1000000007 12 | 13 | int n; 14 | vector v; 15 | vector ans; 16 | 17 | void solve() 18 | { 19 | stack s; 20 | for (int i = n - 1; i >= 0; i--) 21 | { 22 | while (!s.empty() && s.top().fir <= v[i]) 23 | s.pop(); 24 | (!s.empty()) ? ans[i] = s.top().sec : ans[i] = -1; 25 | s.push({v[i], i}); 26 | } 27 | } 28 | signed main() 29 | { 30 | ios_base::sync_with_stdio(false); 31 | cin.tie(NULL); 32 | cin >> n; 33 | v.resize(n); 34 | ans.resize(n); 35 | for (int i = 0; i < n; i++) 36 | cin >> v[i]; 37 | solve(); 38 | for (auto const &i : ans) 39 | cout << i << " "; 40 | cout << endl; 41 | } 42 | // WITHOUT SEGMENT TREE 43 | // for each index (0 <= i < n), find another index (0 <= j < n) 44 | // which v[j] > v[i] and j > i and j is as close as possible to i. 45 | // if this index does not exist, print -1 46 | 47 | /* 48 | 5 49 | 1 3 3 4 5 50 | */ 51 | /* 52 | 1 3 3 4 -1 53 | */ 54 | -------------------------------------------------------------------------------- /Miscellaneous/sum_hash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 300005 17 | #define mod 998244353 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); 24 | int n, k; 25 | cin >> n >> k; 26 | vector h(k + 1, 0); 27 | for (int i = 1; i < k; i++) 28 | { 29 | h[i] = rng(); 30 | h[k] -= h[i]; 31 | } 32 | vector v(n); 33 | int sum = 0, ans = 0; 34 | map mp; 35 | mp[0] = 0; 36 | for (int i = 0; i < n; i++) 37 | { 38 | cin >> v[i]; 39 | sum += h[v[i]]; 40 | if (mp.find(sum) != mp.end()) 41 | ans = max(ans, i - mp[sum] + 1); 42 | else 43 | mp[sum] = i + 1; 44 | } 45 | cout << ans << endl; 46 | } 47 | // solução pra C da final brasileira da maratona de 2023 48 | // dado um array com n inteiros, cada a[i] tá entre 1 e k 49 | // qual o maior tamanho de um subarray no qual todos os numeros de 1 ate k 50 | // tem a mesma frequencia nesse subarray -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/README.md: -------------------------------------------------------------------------------- 1 | # Notebook 2 | 3 | 1 - Install LaTeX and latexmk. 4 | 5 | ```c 6 | sudo apt-get install texlive-full 7 | sudo apt-get install latexmk 8 | ``` 9 | 10 | 2 - run make_contents.py to be able to generate the PDF with the codes that are in repository. 11 | 12 | ```c 13 | python3 make_contents.py 14 | ``` 15 | 16 | 3 - run generate_pdf.py to create PDF. 17 | 18 | ```c 19 | python3 generate_pdf.py 20 | ``` 21 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/BFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define DEBUG 1 17 | #define MAXN 1001 18 | #define mod 1000000007 19 | 20 | int n, m; 21 | vector adj[MAXN]; 22 | bool visited[MAXN]; 23 | 24 | void bfs(int s) 25 | { 26 | queue q; 27 | q.push(s); 28 | while (!q.empty()) 29 | { 30 | int v = q.front(); 31 | q.pop(); 32 | if (visited[v]) 33 | continue; 34 | visited[v] = true; 35 | for (auto const &u : adj[v]) 36 | if (!visited[u]) 37 | q.push(u); 38 | } 39 | } 40 | signed main() 41 | { 42 | ios_base::sync_with_stdio(false); 43 | cin.tie(NULL); 44 | cin >> n >> m; 45 | for (int i = 0; i < m; i++) 46 | { 47 | int a, b, c; 48 | cin >> a >> b >> c; 49 | a--, b--; 50 | adj[a].pb(b); 51 | adj[b].pb(a); 52 | } 53 | bfs(0); 54 | } 55 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/BS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | 7 | vector v; 8 | int binarysearch (int n , int x) 9 | { 10 | int i = 0 ; 11 | int f = n - 1 ; 12 | int m ; 13 | 14 | while(i <= f) 15 | { 16 | m = (i + f) / 2 ; 17 | 18 | if(v[m] == x) return m + 1 ; 19 | if(v[m] < x) i = m + 1 ; 20 | if(v[m] > x) f = m - 1 ; 21 | } 22 | 23 | return 0 ; 24 | } 25 | int main () 26 | { 27 | int n , aux , m ; 28 | 29 | cin >> n ; 30 | 31 | for (int i = 0 ; i < n ; i++) 32 | { 33 | cin >> aux ; 34 | v.pb(aux); 35 | } 36 | 37 | sort(v.begin() , v.end()); 38 | 39 | cin >> m ; 40 | cout << binarysearch(n , m) << endl ; 41 | 42 | return 0 ; 43 | } -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXN 500000 5 | 6 | int n , m ; 7 | int visited [MAXN] ; 8 | vector adj_list [MAXN] ; 9 | 10 | void dfs (int x) 11 | { 12 | for (int i = 0 ; i < adj_list[x].size() ; i++) 13 | { 14 | int v = adj_list[x][i] ; 15 | 16 | if(visited[v] == -1) 17 | { 18 | visited[v] = visited[x] ; 19 | dfs(v) ; 20 | } 21 | } 22 | } 23 | void initialize () 24 | { 25 | for (int i = 1 ; i <= n ; i++) 26 | { 27 | visited[i] = -1 ; 28 | } 29 | } 30 | int main () 31 | { 32 | int a , b ; 33 | 34 | cin >> n >> m ; 35 | 36 | initialize(); 37 | 38 | for (int i = 1 ; i <= m ; i++) 39 | { 40 | cin >> a >> b ; 41 | 42 | adj_list[a].push_back(b) ; 43 | adj_list[b].push_back(a) ; 44 | } 45 | 46 | dfs(1) ; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/Digitdp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pb push_back 6 | #define pi pair 7 | #define fir first 8 | #define sec second 9 | #define MAXN 2001 10 | #define mod 1000000007 11 | 12 | int dp[20][20 * 9][2]; // a,b <= 10^18 13 | vector dig; 14 | 15 | int solve(int i, int j, int k) 16 | { 17 | if (i == dig.size()) 18 | return (k) ? dp[i][j][k] = j : dp[i][j][k] = 0; 19 | if (dp[i][j][k] != -1) 20 | return dp[i][j][k]; 21 | int sum = 0; 22 | if (k) 23 | for (int f = 0; f <= 9; f++) 24 | sum += solve(i + 1, j + f, k); 25 | if (!k) 26 | for (int f = 0; f <= dig[i]; f++) 27 | sum += solve(i + 1, j + f, (dig[i] != f) ? 1 : 0); 28 | return dp[i][j][k] = sum; 29 | } 30 | void get_digits(int n) 31 | { 32 | dig.clear(); 33 | while (n) 34 | { 35 | dig.pb(n % 10); 36 | n = n / 10; 37 | } 38 | reverse(dig.begin(), dig.end()); 39 | } 40 | signed main() 41 | { 42 | ios_base::sync_with_stdio(false); 43 | cin.tie(NULL); 44 | int a, b; 45 | cin >> a >> b; 46 | get_digits(a); 47 | memset(dp, -1, sizeof(dp)); 48 | int aa = solve(0, 0, 0); 49 | get_digits(b + 1); 50 | memset(dp, -1, sizeof(dp)); 51 | int bb = solve(0, 0, 0); 52 | cout << bb - aa << endl; 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/Floyd_Warshall.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pb push_back 5 | #define lli long long int 6 | #define MAXN 10000 7 | #define INF 999999 8 | 9 | int n , m , a , b , c ; 10 | int dist [MAXN][MAXN] ; 11 | 12 | void floyd_warshall () 13 | { 14 | for (int k = 0 ; k < n ; k++) 15 | { 16 | for (int i = 0 ; i < n ; i++) 17 | { 18 | for (int j = 0 ; j < n ; j++) 19 | { 20 | dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]) ; 21 | } 22 | } 23 | } 24 | } 25 | void initialize () 26 | { 27 | for (int i = 0 ; i < n ; i++) 28 | { 29 | for (int j = 0 ; j < n ; j++) 30 | { 31 | if (i == j) 32 | { 33 | dist[i][j] = 0 ; 34 | } 35 | else 36 | { 37 | dist[i][j] = INF ; 38 | } 39 | 40 | } 41 | } 42 | } 43 | int main() 44 | { 45 | cin >> n >> m ; 46 | 47 | initialize () ; 48 | 49 | for (int i = 0 ; i < m ; i++) 50 | { 51 | cin >> a >> b >> c ; 52 | dist [a][b] = min (dist[a][b] , c) ; 53 | dist [b][a] = min (dist[b][a] , c) ; 54 | } 55 | 56 | floyd_warshall () ; 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/LowerBound.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define DEBUG 0 17 | #define MAXN 1000001 18 | #define mod 1000000007 19 | // first element >= x 20 | vector k(MAXN); 21 | int lower(int l, int r, int x) // first element >= x 22 | { 23 | while (l < r) 24 | { 25 | int mid = (l + r) >> 1; 26 | (x <= k[mid]) ? r = mid : l = mid + 1; 27 | } 28 | return k[l]; 29 | } 30 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/STL.cpp: -------------------------------------------------------------------------------- 1 | // lower - primeiro maior ou igual a x 2 | // upper - ultimo menor ou igual a x 3 | 4 | #include 5 | using namespace std; 6 | 7 | #define lli long long int 8 | #define pb push_back 9 | 10 | vector v ; 11 | int main() 12 | { 13 | int n , aux ; 14 | cin >> n ; 15 | 16 | for (int i = 0 ; i < n ; i++) 17 | { 18 | cin >> aux ; 19 | v.pb(aux); 20 | } 21 | 22 | sort(v.begin() , v.end()); 23 | 24 | int q ; 25 | cin >> q ; 26 | 27 | while (q--) 28 | { 29 | cin >> aux ; 30 | vector :: iterator low = lower_bound (v.begin() , v.end() , aux) ; 31 | vector :: iterator up = upper_bound (v.begin() , v.end() , aux) ; 32 | 33 | cout << (low - v.begin()) << " " << (up - v.begin()) - 1 << endl ; 34 | } 35 | 36 | return 0 ; 37 | } 38 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/TS.cpp: -------------------------------------------------------------------------------- 1 | // busca ternaria 2 | // divide em 3 partes, 2 mids 3 | // mid1 = l + (r-l)/3 4 | // mid2 = r - (r-l)/3 5 | #include 6 | using namespace std; 7 | 8 | #define lli long long int 9 | #define pb push_back 10 | #define in insert 11 | #define pi pair 12 | #define pii pair 13 | #define mp make_pair 14 | #define fir first 15 | #define sec second 16 | #define MAXL 100001 17 | 18 | int n, key; 19 | vector ar; 20 | 21 | int ts() 22 | { 23 | int l = 0, r = n - 1; 24 | while (r >= l) 25 | { 26 | int mid1 = l + (r - l) / 3; 27 | int mid2 = r - (r - l) / 3; 28 | if (ar[mid1] == key) 29 | return mid1; 30 | if (ar[mid2] == key) 31 | return mid2; 32 | if (key < ar[mid1]) 33 | r = mid1 - 1; 34 | else if (key > ar[mid2]) 35 | l = mid2 + 1; 36 | else 37 | { 38 | l = mid1 + 1; 39 | r = mid2 - 1; 40 | } 41 | } 42 | return -1; // nao encontrado 43 | } 44 | signed main() 45 | { 46 | ios_base::sync_with_stdio(false); 47 | cin.tie(NULL); 48 | cin >> n; 49 | ar.resize(n); 50 | for (int i = 0; i < n; i++) 51 | cin >> ar[i]; 52 | sort(ar.begin(), ar.end()); 53 | cin >> key; 54 | cout << ts() << endl; 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/TreeDiameter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 100001 12 | #define mod 1000000007 13 | 14 | int diameter, best; 15 | vector adj[MAXN]; 16 | bool visited[MAXN]; 17 | 18 | void dfs(int s, int c) 19 | { 20 | if (c > diameter) 21 | { 22 | diameter = c; 23 | best = s; 24 | } 25 | visited[s] = true; 26 | for (auto const &i : adj[s]) 27 | if (!visited[i]) 28 | dfs2(i, c + 1); 29 | } 30 | signed main() 31 | { 32 | ios_base::sync_with_stdio(false); 33 | cin.tie(NULL); 34 | int q; 35 | cin >> q; 36 | while (q--) 37 | { 38 | int n; 39 | cin >> n; 40 | for (int i = 0; i < n; i++) 41 | adj[i].clear(); 42 | for (int i = 0; i < n - 1; i++) 43 | { 44 | int a, b; 45 | cin >> a >> b; 46 | a--, b--; 47 | adj[b].pb(a); 48 | adj[a].pb(b); 49 | } 50 | diameter = 0, best = 0; 51 | memset(visited, false, sizeof(visited)); 52 | dfs(1, 0); // achar o vertice mais distante a partir do vertice 0 53 | memset(visited, false, sizeof(visited)); 54 | dfs(best, 0); // achar o mais distante a partir do primeiro vertice que achamos 55 | cout << diameter << endl; 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/UpperBound.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define DEBUG 0 17 | #define MAXN 1000001 18 | #define mod 1000000007 19 | // last element <= x 20 | vector k(MAXN); 21 | int upper(int l, int r, int x) 22 | { 23 | while (l < r) 24 | { 25 | int mid = (l + r + 1) >> 1; 26 | (k[mid] <= x) ? l = mid : r = mid - 1; 27 | } 28 | return k[l]; 29 | } 30 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/__int128.cpp: -------------------------------------------------------------------------------- 1 | // https://codeforces.com/blog/entry/75044 2 | // functions to print and read a __int128 in c++ 3 | __int128 read() 4 | { 5 | __int128 x = 0, f = 1; 6 | char ch = getchar(); 7 | while (ch < '0' || ch > '9') 8 | { 9 | if (ch == '-') 10 | f = -1; 11 | ch = getchar(); 12 | } 13 | while (ch >= '0' && ch <= '9') 14 | { 15 | x = x * 10 + ch - '0'; 16 | ch = getchar(); 17 | } 18 | return x * f; 19 | } 20 | void print(__int128 x) 21 | { 22 | if (x < 0) 23 | { 24 | cout << "-"; 25 | x = -x; 26 | } 27 | if (x > 9) 28 | print(x / 10); 29 | char at = (x % 10) + '0'; 30 | cout << at; 31 | } 32 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/binomial_theorem.md: -------------------------------------------------------------------------------- 1 | ## Binomial Theorem 2 | 3 | ### Theorem 4 | 5 | $$ 6 | (x + y)^n = \sum_{k = 0}^{n} {n \choose k} x^{n - k}y^{k} 7 | $$ 8 | 9 | in addition, we have: 10 | 11 | $$ 12 | (x - y)^n = \sum_{k = 0}^{n} (-1)^k {n \choose k} x^{n - k}y^{k} 13 | $$ 14 | 15 | $$ 16 | (1 + x)^n = \sum_{k = 0}^{n} {n \choose k} x^{k} 17 | $$ 18 | 19 | ### Cool Problem 20 | 21 | [Fibonacci Fever](https://codeforces.com/gym/104412/problem/F) 22 | 23 | Given n and k you're asked to compute (mod $10^9 + 7$): 24 | 25 | $$ 26 | \sum\_{i = 1}^{n}f_i^k 27 | $$ 28 | 29 | where $f_n$ is the n-th fibonacci number. 30 | 31 | Recall that: 32 | 33 | $$ 34 | f_n = \frac{1}{\sqrt{5}} \left(\frac{1+\sqrt{5}}{2}\right)^n - \frac{1}{\sqrt{5}} \left(\frac{1-\sqrt{5}}{2}\right)^n 35 | $$ 36 | 37 | This is something like that: 38 | 39 | $$ 40 | f_n = ca^n - cb^n 41 | $$ 42 | 43 | In the end, we end up with something like: 44 | 45 | $$ 46 | c^k (\sum_{j = 0}^{k} (-1)^{j} {k \choose j} \sum_{i = 1}^{n} (a^{k - j}b^{j})^{i} ) 47 | $$ 48 | 49 | PS: To handle $\sqrt{5}$ mod $10^9 + 7$, we need to store each number in the form $x = a + b\sqrt{5}$ 50 | 51 | [Code](https://github.com/jonh14lk/Competitive_Programming/blob/master/Math/binomial_theorem.cpp) 52 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/chicken_mcnugget.md: -------------------------------------------------------------------------------- 1 | ## Chicken McNugget Theorem 2 | 3 | For any two coprime numbers (n > 0, m > 0), the greatest integer that cannot be written in the form: 4 | 5 | an + bm, (a >= 0, b >= 0) 6 | 7 | is (n \* m) - n - m 8 | 9 | ## Consequence of the theorem 10 | 11 | That there are exactly ((n - 1) \* (m - 1)) / 2 positive integers which cannot be expressed in the form an + bm, (a >= 0, b >= 0) 12 | 13 | ## Generalization 14 | 15 | If n and m are not coprime, so all numbers that are not multiples of gcd(n, m) cannot be expressed in the form an + bm, (a >= 0, b >= 0) 16 | 17 | in addition, you can consider n = (n / gcd(n, m)) and m = (m / gcd(n, m)), to find how many multiples of gcd(n, m) cannot be expressed, or to find the greatest multiple of gcd(n, m) that cannot be expressed 18 | 19 | ## Considering a > 0, b > 0 20 | 21 | Considering (n > 0, m > 0), n and m are coprime: 22 | 23 | let y = ((n \* m) + min(n, m)) - 1 24 | 25 | The number of positive integers which cannot be expressed increases by (y / n) 26 | 27 | The number of positive integers which cannot be expressed increases by (y / m) 28 | 29 | you must not count the multiples of (n \* m) more than once, just decrease number of positive integers which cannot be expressed by (y / (n \* m)) 30 | 31 | ## Problems 32 | 33 | - [Forming Compounds](https://codeforces.com/group/XrhoJtxCjm/contest/422716/problem/I) 34 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/coordinate_compression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 500005 17 | #define mod 1000000007 18 | 19 | void compress(vector &v) 20 | { 21 | vector val; 22 | for (auto const &i : v) 23 | val.pb(i); 24 | sort(val.begin(), val.end()); 25 | val.erase(unique(val.begin(), val.end()), val.end()); 26 | for (auto &i : v) 27 | i = lower_bound(val.begin(), val.end(), i) - val.begin(); 28 | } -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/crivo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | #define in insert 7 | #define pi pair 8 | #define pd pair 9 | #define pii pair 10 | #define mp make_pair 11 | #define fir first 12 | #define sec second 13 | #define MAXN 100001 14 | #define mod 1000000007 15 | 16 | bitset prime; 17 | 18 | void crivo () 19 | { 20 | prime.set(); 21 | prime[0] = false; 22 | prime[1] = false; 23 | for (int i = 2 ; i < MAXN ; i++) 24 | if(prime[i]) 25 | for(int j = 2 ; j * i < MAXN ; j++) 26 | prime[j * i] = false; 27 | } 28 | signed main() 29 | { 30 | crivo(); 31 | int q; 32 | cin >> q; 33 | while(q--) 34 | { 35 | int n; 36 | cin >> n; 37 | (prime[n]) ? cout << "YES\n" : cout << "NO\n" ; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 5001 11 | #define mod 1000000007 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | int n; 18 | cin >> n; 19 | int ans = 0; 20 | for (int i = 1; i <= sqrt(n); i++) 21 | { 22 | if (!(n % i)) 23 | { 24 | ans++; 25 | if (n / i != i) 26 | ans++; 27 | } 28 | } 29 | cout << ans << endl; 30 | } 31 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/dsu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 2001 18 | #define mod 1000000007 19 | 20 | struct dsu 21 | { 22 | int tot; 23 | vector parent; 24 | vector sz; 25 | 26 | dsu(int n) 27 | { 28 | parent.resize(n); 29 | sz.resize(n); 30 | tot = n; 31 | for (int i = 0; i < n; i++) 32 | { 33 | parent[i] = i; 34 | sz[i] = 1; 35 | } 36 | } 37 | int find_set(int i) 38 | { 39 | return parent[i] = (parent[i] == i) ? i : find_set(parent[i]); 40 | } 41 | void make_set(int x, int y) 42 | { 43 | x = find_set(x), y = find_set(y); 44 | if (x != y) 45 | { 46 | if (sz[x] > sz[y]) 47 | swap(x, y); 48 | parent[x] = y; 49 | sz[y] += sz[x]; 50 | tot--; 51 | } 52 | } 53 | }; 54 | signed main() 55 | { 56 | ios_base::sync_with_stdio(false); 57 | cin.tie(NULL); 58 | int n; 59 | cin >> n; 60 | dsu d(n); 61 | int a, b; 62 | cin >> a >> b; 63 | d.make_set(a, b); 64 | d.find_set(a); 65 | } 66 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/execution_time.cpp: -------------------------------------------------------------------------------- 1 | // https://codeforces.com/blog/entry/57647 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | using namespace __gnu_pbds; 7 | 8 | template 9 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 10 | 11 | #define int long long int 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300005 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | // just call clock in the beginning 25 | clock_t time = clock(); 26 | 27 | // ... 28 | // ... 29 | // some code here ... 30 | // ... 31 | // ... 32 | 33 | // execution time: 34 | cout << setprecision(3) << fixed << (double)(clock() - time) / CLOCKS_PER_SEC << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/extended_euclidean.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pb push_back 6 | #define pi pair 7 | #define pii pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 200001 11 | #define mod 998244353 12 | 13 | int gcd(int a, int b, int &x, int &y) 14 | { 15 | if (b == 0) 16 | { 17 | x = 1; 18 | y = 0; 19 | return a; 20 | } 21 | int x1, y1; 22 | int d = gcd(b, a % b, x1, y1); 23 | x = y1; 24 | y = x1 - y1 * (a / b); 25 | return d; 26 | } 27 | signed main() 28 | { 29 | int n; 30 | cin >> n; 31 | int k = 2; 32 | while (1) 33 | { 34 | int x, y; 35 | if (gcd(k, n, x, y) == 1) 36 | { 37 | x = ((x % n) + n) % n; 38 | cout << x << endl; 39 | return 0; 40 | } 41 | k++; 42 | } 43 | return 0; 44 | } 45 | // achar os numeros x e y tal que: 46 | // a * x + b * y = gcd(a, b) 47 | 48 | // problema exemplo: 49 | // https://codeforces.com/group/btcK4I5D5f/contest/451372/problem/J 50 | // dado um numero k 51 | // quero achar um numero x, se possivel, tal que: 52 | // (k * x) % n = 1 53 | 54 | // k * x + n * y = 1 55 | // se gcd(k, n) = 1, entao: 56 | // k * x + n * y = gcd(k, n) 57 | // note que, se gcd(k, n) > 1, logo nao existe solucao -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/fenwick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 200005 18 | #define mod 998244353 19 | 20 | struct fenw 21 | { 22 | int n; 23 | vector bit; 24 | fenw() {} 25 | fenw(int sz) 26 | { 27 | n = sz; 28 | bit.assign(sz + 1, 0); 29 | } 30 | int qry(int r) // query de prefixo a[0] + a[1] + ... a[r] 31 | { 32 | int ret = 0; 33 | for (int i = r + 1; i > 0; i -= i & -i) 34 | ret += bit[i]; 35 | return ret; 36 | } 37 | void upd(int r, int x) // a[r] += x 38 | { 39 | for (int i = r + 1; i <= n; i += i & -i) 40 | bit[i] += x; 41 | } 42 | int bs(int x) // retorna o maior indice i (i < n) tal que: qry(i) < x 43 | { 44 | int i = 0, k = 0; 45 | while (1 << (k + 1) <= n) 46 | k++; 47 | while (k >= 0) 48 | { 49 | int nxt_i = i + (1 << k); 50 | if (nxt_i <= n && bit[nxt_i] < x) 51 | { 52 | i = nxt_i; 53 | x -= bit[i]; 54 | } 55 | k--; 56 | } 57 | return i - 1; 58 | } 59 | }; 60 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/largest_square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define pb push_back 6 | #define int long long int 7 | #define double long double 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 1001 13 | #define mod 1000000007 14 | 15 | signed main() 16 | { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(NULL); 19 | int n; 20 | cin >> n; 21 | int v[n][n]; 22 | int dp[n][n]; 23 | for (int i = 0; i < n; i++) 24 | for (int j = 0; j < n; j++) 25 | cin >> v[i][j]; 26 | int ans = 0; 27 | for (int i = 0; i < n; i++) 28 | { 29 | for (int j = 0; j < n; j++) 30 | { 31 | dp[i][j] = v[i][j]; 32 | if (i && j && dp[i][j]) 33 | dp[i][j] = min({dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]}) + 1; 34 | ans = max(ans, dp[i][j]); 35 | } 36 | } 37 | cout << ans * ans << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/lcs.cpp: -------------------------------------------------------------------------------- 1 | //Dadas duas sequencias s1 e s2, uma de tamanho n e outra de tamanho m, qual a maior subsequencia comum as duas? 2 | 3 | // uma subsequencia de s e um subconjunto dos elementos de s na mesma ordem em que apareciam antes. 4 | // isto significa que {1, 3, 5} e uma subsequencia de {1, 2, 3, 4, 5}, mesmo 1 nao estando do lado do 3. 5 | #include 6 | using namespace std; 7 | 8 | #define lli long long int 9 | #define pb push_back 10 | #define in insert 11 | #define pi pair 12 | #define pii pair 13 | #define mp make_pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1001 17 | #define INF 1000000000 18 | 19 | int v1[MAXN]; 20 | int v2[MAXN]; 21 | int dp[MAXN][MAXN]; 22 | 23 | void lcs(int m, int n) 24 | { 25 | for (int i = 0; i <= m; i++) 26 | { 27 | for (int j = 0; j <= n; j++) 28 | { 29 | if (i == 0 || j == 0) //se uma das sequencias for vazia 30 | dp[i][j] = 0; 31 | else if (v1[i - 1] == v2[j - 1]) // se eh igual, adiciono a lcs e subtraio dos dois 32 | dp[i][j] = dp[i - 1][j - 1] + 1; 33 | else 34 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // se nao retorno o maximo entre tirar um dos dois caras 35 | } 36 | } 37 | cout << dp[m][n] << endl; 38 | } 39 | signed main() 40 | { 41 | int n, m; 42 | cin >> n >> m; 43 | for (int i = 0; i < n; i++) 44 | cin >> v1[i]; 45 | for (int i = 0; i < m; i++) 46 | cin >> v2[i]; 47 | lcs(n, m); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/lis.cpp: -------------------------------------------------------------------------------- 1 | // dada uma sequencia s qualquer, descobrir o tamanho da maior subsequencia crescente de s 2 | // uma subsequencia de s e qualquer subconjunto de elementos de s. 3 | // Para cada novo numero, voce tem duas operacoes possiveis: 4 | // 1 - Colocar o novo numero no topo de uma pilha se ele nao superar o que ja esta em seu topo; 5 | // ou 6 | // 2 - Criar uma nova pilha a direita de todas as outras e colocar o novo numero la. 7 | // ao final do processo a nossa pilha tera os elementos da lis. 8 | #include 9 | using namespace std; 10 | 11 | #define lli long long int 12 | #define pb push_back 13 | #define in insert 14 | #define pi pair 15 | #define pd pair 16 | #define pib pair 17 | #define mp make_pair 18 | #define fir first 19 | #define sec second 20 | #define MAXN 200001 21 | #define MAXL 1000001 22 | #define mod 1000000007 23 | 24 | vector v; 25 | 26 | int lis() 27 | { 28 | vector q; 29 | for (int i = 0; i < v.size(); i++) 30 | { 31 | vector::iterator it = lower_bound(q.begin(), q.end(), v[i]); 32 | if (it == q.end()) 33 | q.pb(v[i]); 34 | else 35 | *it = v[i]; 36 | } 37 | for (int i = 0; i < q.size(); i++) 38 | cout << q[i] << " "; 39 | cout << endl; 40 | return q.size(); 41 | } 42 | signed main() 43 | { 44 | int n; 45 | cin >> n; 46 | v.resize(n); 47 | for (int i = 0; i < n; i++) 48 | cin >> v[i]; 49 | cout << lis() << endl; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/max_matrix_path.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define pb push_back 6 | #define int long long int 7 | #define mp make_pair 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 301 13 | #define MAXL 20 14 | #define mod 1000000007 15 | #define INF 1000000001 16 | 17 | int n; 18 | int grid[MAXN][MAXN]; 19 | int dp[MAXN][MAXN]; 20 | 21 | int solve(int i, int j) 22 | { 23 | if (i == n - 1 && j == n - 1) 24 | return grid[i][j]; 25 | if (dp[i][j] != -1) 26 | return dp[i][j]; 27 | if (i + 1 < n && j + 1 < n) 28 | return dp[i][j] = grid[i][j] + max(solve(i + 1, j), solve(i, j + 1)); 29 | if (i + 1 < n) 30 | return dp[i][j] = grid[i][j] + solve(i + 1, j); 31 | if (j + 1 < n) 32 | return dp[i][j] = grid[i][j] + solve(i, j + 1); 33 | } 34 | signed main() 35 | { 36 | cin >> n; 37 | for (int i = 0; i < n; i++) 38 | for (int j = 0; j < n; j++) 39 | cin >> grid[i][j]; 40 | memset(dp, -1, sizeof(dp)); 41 | cout << solve(0, 0) << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/ordered_set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | #define int long long int 8 | #define pb push_back 9 | #define pi pair 10 | #define pii pair 11 | #define fir first 12 | #define sec second 13 | #define DEBUG false 14 | #define MAXN 200002 15 | 16 | template // template do ordered set 17 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | ordered_set s; // ordered_set 24 | s.insert(1); 25 | s.insert(1); 26 | s.insert(2); 27 | s.insert(4); 28 | s.insert(3); 29 | for (auto const &i : s) // nao adiciona elementos repetidos, que nem o set normal 30 | cout << i << " "; 31 | cout << endl; 32 | cout << *(s.find_by_order(0)) << endl; // iterator do elemento 0 33 | cout << *(s.find_by_order(1)) << endl; // iterator do elemento 1 34 | cout << s.order_of_key(4) << endl; // quantidade de elementos que sao menores do que 4 35 | cout << s.order_of_key(6) << endl; // quantidade de elementos que sao menores do que 4 36 | } 37 | // find_by_order : O(log n), retorna (um iterator) qual o k-esimo elemento do set 38 | // order_of_key: O(log n), retorna qual a quantidade de elementos menores do que x no set 39 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/polygon_area.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define endl '\n' 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 200005 12 | #define mod 998244353 13 | 14 | double area(vector fig) 15 | { 16 | double res = 0; 17 | for (unsigned i = 0; i < fig.size(); i++) 18 | { 19 | pi p = i ? fig[i - 1] : fig.back(); 20 | pi q = fig[i]; 21 | res += (p.fir - q.fir) * (p.sec + q.sec); 22 | } 23 | return fabs(res) / 2; 24 | } 25 | int cross(pi a, pi b) 26 | { 27 | return a.fir * b.sec - a.sec * b.fir; 28 | } 29 | double area2(vector fig) 30 | { 31 | double res = 0; 32 | for (unsigned i = 0; i < fig.size(); i++) 33 | { 34 | pi p = i ? fig[i - 1] : fig.back(); 35 | pi q = fig[i]; 36 | res += cross(p, q); 37 | } 38 | return fabs(res) / 2; 39 | } 40 | signed main() 41 | { 42 | ios_base::sync_with_stdio(false); 43 | cin.tie(NULL); 44 | return 0; 45 | } 46 | // achar area de um poligono 47 | // tomar cuiddado com a ordem 48 | // percorrer os vertices em sentido horario ou anti-horario 49 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/prefix_sum_2d.cpp: -------------------------------------------------------------------------------- 1 | // https://cses.fi/problemset/task/1652 2 | #include 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define endl '\n' 7 | #define pb push_back 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 200005 13 | #define mod 1000000007 14 | 15 | int v[1001][1001]; 16 | int p[1001][1001]; 17 | 18 | int qry(int x1, int y1, int x2, int y2) 19 | { 20 | return p[x2 + 1][y2 + 1] - p[x2 + 1][y1] - p[x1][y2 + 1] + p[x1][y1]; 21 | } 22 | signed main() 23 | { 24 | int n, q; 25 | cin >> n >> q; 26 | for (int i = 0; i < n; i++) 27 | { 28 | for (int j = 0; j < n; j++) 29 | { 30 | char c; 31 | cin >> c; 32 | v[i][j] = (c == '*'); 33 | } 34 | } 35 | for (int i = 0; i < n; i++) 36 | { 37 | for (int j = 0; j < n; j++) 38 | { 39 | p[i + 1][j + 1] = p[i][j + 1] + p[i + 1][j] - p[i][j]; 40 | p[i + 1][j + 1] += v[i][j]; 41 | } 42 | } 43 | while (q--) 44 | { 45 | int a, b, c, d; 46 | cin >> a >> b >> c >> d; 47 | a--, b--, c--, d--; 48 | cout << qry(a, b, c, d) << endl; 49 | } 50 | return 0; 51 | } 52 | // prefix sum 2d 53 | // me enrolo pra codar toda vez, e bom deixar na lib -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/primefactors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define mp make_pair 8 | #define pi pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 501 12 | #define MAXL 20 13 | #define mod 1000000007 14 | 15 | vector facts; 16 | void primefactors(int n) 17 | { 18 | while (n % 2 == 0) 19 | { 20 | facts.pb(2); 21 | n = n / 2; 22 | } 23 | for (int i = 3; i <= sqrt(n); i += 2) 24 | { 25 | while (n % i == 0) 26 | { 27 | facts.pb(i); 28 | n = n / i; 29 | } 30 | } 31 | if (n > 2) 32 | facts.pb(n); 33 | } 34 | signed main() 35 | { 36 | int n; 37 | cin >> n; 38 | primefactors(n); 39 | sort(facts.begin(), facts.end()); 40 | for (auto const &i : facts) 41 | cout << i << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/primefactors2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define PI acos(-1) 11 | #define pb push_back 12 | #define int long long int 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define DEBUG 0 18 | #define MAXN 1000001 19 | #define mod 1000000007 20 | 21 | namespace primefactors 22 | { 23 | bitset prime; 24 | vector nxt(MAXN); 25 | vector factors; 26 | 27 | void crivo() 28 | { 29 | prime.set(); 30 | prime[0] = false, prime[1] = false; 31 | for (int i = 2; i < MAXN; i++) 32 | { 33 | if (prime[i]) 34 | { 35 | nxt[i] = i; 36 | for (int j = 2; j * i < MAXN; j++) 37 | { 38 | prime[j * i] = false; 39 | nxt[j * i] = i; 40 | } 41 | } 42 | } 43 | } 44 | void fact(int n) 45 | { 46 | factors.clear(); 47 | while (n > 1) 48 | { 49 | factors.pb(nxt[n]); 50 | n = n / nxt[n]; 51 | } 52 | } 53 | } 54 | signed main() 55 | { 56 | ios_base::sync_with_stdio(false); 57 | cin.tie(NULL); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/rand.cpp: -------------------------------------------------------------------------------- 1 | // https://codeforces.com/blog/entry/61587 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | using namespace __gnu_pbds; 7 | 8 | template 9 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 10 | 11 | #define int long long int 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300005 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 25 | 26 | int n = 10; 27 | vector v(n); 28 | for (int i = 0; i < n; i++) 29 | v[i] = i; 30 | shuffle(v.begin(), v.end(), rng); // random shuffle 31 | 32 | int x = rng() % 10; // better than rand() 33 | cout << x << endl; 34 | 35 | // random integer values on the closed interval [a, b] 36 | int y = uniform_int_distribution(3, 77)(rng); 37 | cout << y << endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/runner.py: -------------------------------------------------------------------------------- 1 | # This script does the following: 2 | # 1 - Generate a random testcase 3 | # 2 - Run some "naive" code with this input 4 | # 3 - Run your code with this input 5 | # 4 - Compare the outputs 6 | import os 7 | 8 | naive = "brute.cpp" # path to naive code 9 | code = "d.cpp" # path to your code 10 | generator = "g.cpp" # path to test generator 11 | 12 | def compile_codes(): 13 | os.system('g++ ' + generator + ' -o generator -O2') 14 | os.system('g++ ' + naive + ' -o naive -O2') 15 | os.system('g++ ' + code + ' -o code -O2') 16 | 17 | def generate_case(): 18 | os.system('./generator > in'); 19 | 20 | def get_naive_output(): 21 | output = os.popen('./naive 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define PI acos(-1) 11 | #define pb push_back 12 | #define int long long int 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define DEBUG 0 18 | #define MAXN 1000003 19 | #define mod 1000000007 20 | 21 | vector prime; 22 | 23 | void segmentedsieve(int l, int r) 24 | { 25 | int lim = sqrt(r); 26 | vector mark(lim + 1, false); 27 | vector primes; 28 | for (int i = 2; i <= lim; ++i) 29 | { 30 | if (!mark[i]) 31 | { 32 | primes.pb(i); 33 | for (int j = i * i; j <= lim; j += i) 34 | mark[j] = true; 35 | } 36 | } 37 | vector isprime(r - l + 1, true); 38 | for (int i : primes) 39 | for (int j = max(i * i, (l + i - 1) / i * i); j <= r; j += i) 40 | isprime[j - l] = false; 41 | if (l == 1) 42 | isprime[0] = false; 43 | for (int i = 0; i < isprime.size(); i++) 44 | if (isprime[i]) 45 | prime.pb(i + l); 46 | } 47 | signed main() 48 | { 49 | ios_base::sync_with_stdio(false); 50 | cin.tie(NULL); 51 | int l, r; 52 | cin >> l >> r; 53 | segmentedsieve(l, r); 54 | for (auto const &i : prime) 55 | cout << i << " "; 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/stack_trick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 300001 11 | #define mod 1000000007 12 | 13 | int n; 14 | vector v; 15 | vector ans; 16 | 17 | void solve() 18 | { 19 | stack s; 20 | for (int i = n - 1; i >= 0; i--) 21 | { 22 | while (!s.empty() && s.top().fir <= v[i]) 23 | s.pop(); 24 | (!s.empty()) ? ans[i] = s.top().sec : ans[i] = -1; 25 | s.push({v[i], i}); 26 | } 27 | } 28 | signed main() 29 | { 30 | ios_base::sync_with_stdio(false); 31 | cin.tie(NULL); 32 | cin >> n; 33 | v.resize(n); 34 | ans.resize(n); 35 | for (int i = 0; i < n; i++) 36 | cin >> v[i]; 37 | solve(); 38 | for (auto const &i : ans) 39 | cout << i << " "; 40 | cout << endl; 41 | } 42 | // WITHOUT SEGMENT TREE 43 | // for each index (0 <= i < n), find another index (0 <= j < n) 44 | // which v[j] > v[i] and j > i and j is as close as possible to i. 45 | // if this index does not exist, print -1 46 | 47 | /* 48 | 5 49 | 1 3 3 4 5 50 | */ 51 | /* 52 | 1 3 3 4 -1 53 | */ 54 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/subsequences_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define mp make_pair 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 100 13 | #define MAXL 20 14 | #define mod 998244353 15 | 16 | void count(string a, string b) 17 | { 18 | int m = a.size(); 19 | int n = b.size(); 20 | int dp[m + 1][n + 1] = {{0}}; 21 | for (int i = 0; i <= n; ++i) 22 | dp[0][i] = 0; 23 | for (int i = 0; i <= m; ++i) 24 | dp[i][0] = 1; 25 | for (int i = 1; i <= m; i++) 26 | { 27 | for (int j = 1; j <= n; j++) 28 | { 29 | if (a[i - 1] == b[j - 1]) 30 | dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 31 | else 32 | dp[i][j] = dp[i - 1][j]; 33 | } 34 | } 35 | cout << dp[m][n] << endl; 36 | } 37 | signed main() 38 | { 39 | string a, b; 40 | cin >> a >> b; 41 | count(a, b); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/sum_hash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 300005 17 | #define mod 998244353 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); 24 | int n, k; 25 | cin >> n >> k; 26 | vector h(k + 1, 0); 27 | for (int i = 1; i < k; i++) 28 | { 29 | h[i] = rng(); 30 | h[k] -= h[i]; 31 | } 32 | vector v(n); 33 | int sum = 0, ans = 0; 34 | map mp; 35 | mp[0] = 0; 36 | for (int i = 0; i < n; i++) 37 | { 38 | cin >> v[i]; 39 | sum += h[v[i]]; 40 | if (mp.find(sum) != mp.end()) 41 | ans = max(ans, i - mp[sum] + 1); 42 | else 43 | mp[sum] = i + 1; 44 | } 45 | cout << ans << endl; 46 | } 47 | // solucao pra C da final brasileira da maratona de 2023 48 | // dado um array com n inteiros, cada a[i] ta entre 1 e k 49 | // qual o maior tamanho de um subarray no qual todos os numeros de 1 ate k 50 | // tem a mesma frequencia nesse subarray -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/tip.cpp: -------------------------------------------------------------------------------- 1 | // dados os valores de moedas v1, v2, ... vn e possivel formar um valor m como combinacao de moedas 2 | // para isso basta montar uma dp inicializada com -1 3 | // nesse caso a dp so precisa de um parametro q e = valor restante ate o limite 4 | // mas podem existir variacoes do problema q precise de mais coisas 5 | // se em achar alguma combinacao valida retorna 1, se nao retorna 0 6 | #include 7 | using namespace std; 8 | 9 | #define lli long long int 10 | #define pb push_back 11 | #define in insert 12 | #define pi pair 13 | #define pd pair 14 | #define pib pair 15 | #define mp make_pair 16 | #define fir first 17 | #define sec second 18 | #define MAXN 200001 19 | #define MAXL 10001 20 | #define mod 1000000007 21 | 22 | int dp[MAXN]; 23 | vector v; 24 | 25 | int solve(int rem) 26 | { 27 | if (rem == 0) 28 | return 1; 29 | if (rem < 0) 30 | return 0; 31 | if (dp[rem] >= 0) 32 | return dp[rem]; 33 | for (int i = 0; i < v.size(); i++) 34 | if (solve(rem - v[i])) 35 | return dp[rem - v[i]] = 1; 36 | return dp[rem] = 0; 37 | } 38 | signed main() 39 | { 40 | int n, m; 41 | cin >> n >> m; 42 | v.resize(n); 43 | for (int i = 0; i < n; i++) 44 | cin >> v[i]; 45 | memset(dp, -1, sizeof(dp)); 46 | (solve(m)) ? cout << "Yes\n" : cout << "No\n"; 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/totient.cpp: -------------------------------------------------------------------------------- 1 | #define MAXN 100000 2 | 3 | int phi[MAXN]; 4 | 5 | void calc() 6 | { 7 | for (int i = 0; i < MAXN; i++) 8 | phi[i] = i; 9 | for (int i = 2; i < MAXN; i++) 10 | { 11 | if (phi[i] == i) 12 | { 13 | for (int j = i; j < MAXN; j += i) 14 | phi[j] -= phi[j] / i; 15 | } 16 | } 17 | } 18 | int calc_phi(int n) 19 | { 20 | int ans = n; 21 | for (int i = 2; i * i <= n; i++) 22 | { 23 | if (n % i == 0) 24 | { 25 | while (n % i == 0) 26 | n /= i; 27 | ans -= ans / i; 28 | } 29 | } 30 | if (n > 1) 31 | ans -= ans / n; 32 | return ans; 33 | } -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/code/z-function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 2001 18 | #define mod 1000000007 19 | 20 | vector z_function(string &s) 21 | { 22 | int n = s.size(); 23 | vector z(n); 24 | z[0] = n; 25 | for (int i = 1, l = 0, r = 0; i < n; i++) 26 | { 27 | if (i <= r) 28 | z[i] = min(r - i + 1, z[i - l]); 29 | while (i + z[i] < n && s[z[i]] == s[i + z[i]]) 30 | z[i]++; 31 | if (i + z[i] - 1 > r) 32 | l = i, r = i + z[i] - 1; 33 | } 34 | return z; 35 | } 36 | signed main() 37 | { 38 | ios_base::sync_with_stdio(false); 39 | cin.tie(NULL); 40 | string s; 41 | cin >> s; 42 | vector z = z_function(s); 43 | } 44 | // z-function 45 | // calcula para cada i: 46 | // z[i] = o tamanho de lcp(s, s.substr(i, n - i)) 47 | // lcp -> longest comom prefix -------------------------------------------------------------------------------- /Notebook (based on the stanfordacm script)/notebook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonh14lk/Competitive_Programming/7dd840599cfb127ae873e59bf2db29338de55292/Notebook (based on the stanfordacm script)/notebook.pdf -------------------------------------------------------------------------------- /Problem Solving/Fase zero 2025/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 100005 17 | #define mod 998244353 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int c, g; 24 | cin >> c >> g; 25 | if (c == 1) 26 | cout << "vivo e morto\n"; 27 | else if (g == 1) 28 | cout << "vivo\n"; 29 | else 30 | cout << "morto\n"; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Problem Solving/Fase zero 2025/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1005 17 | #define MAXK 8005 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n; 24 | cin >> n; 25 | string s, t; 26 | cin >> s >> t; 27 | int tot = 0, tot2 = 0; 28 | for (int i = 0; i < n; i++) 29 | { 30 | if (s[i] == '*') 31 | { 32 | tot++; 33 | tot2 += (t[i] != '*'); 34 | } 35 | else 36 | { 37 | assert(t[i] == s[i]); 38 | } 39 | } 40 | double ans = (double)tot2 / tot; 41 | cout << fixed << setprecision(2) << ans << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Problem Solving/Fase zero 2025/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pb push_back 6 | #define fir first 7 | #define sec second 8 | #define pi pair 9 | 10 | const int inf = 1e18; 11 | 12 | signed main() 13 | { 14 | ios_base::sync_with_stdio(false); 15 | cin.tie(NULL); 16 | int y, k; 17 | cin >> y >> k; 18 | vector divs; 19 | for (int i = 1; (i * i) <= y; i++) 20 | { 21 | if (y % i == 0) 22 | { 23 | divs.pb(i); 24 | if ((y / i) != i) 25 | divs.pb(y / i); 26 | } 27 | } 28 | sort(divs.begin(), divs.end()); 29 | int x = 1, i = 0; 30 | while (1) 31 | { 32 | pi mn = {inf, inf}; 33 | for (int j = i + 1; j < divs.size(); j++) 34 | { 35 | if (divs[j] % divs[i] == 0) 36 | { 37 | int div = divs[j] / divs[i]; 38 | mn = {div - 1, j}; 39 | break; 40 | } 41 | } 42 | if (mn.fir > k) 43 | { 44 | x += (divs[i] * k); 45 | break; 46 | } 47 | x += (divs[i] * mn.fir); 48 | k -= mn.fir; 49 | i = mn.sec; 50 | } 51 | cout << x << endl; 52 | } 53 | // uma vez que gcd(x, y) = z 54 | // gcd(x + z, y) >= gcd(x, y) ne -------------------------------------------------------------------------------- /Problem Solving/Fase zero 2025/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1005 17 | #define MAXK 8005 18 | 19 | int n; 20 | int v[MAXN]; 21 | int dp[MAXN][MAXK][4]; 22 | 23 | int solve(int i, int j, int k) 24 | { 25 | if (k == 0) 26 | return (j == 0); 27 | if (j < 0 || i == n) 28 | return 0; 29 | if (dp[i][j][k] != -1) 30 | return dp[i][j][k]; 31 | int ans = solve(i + 1, j, k); 32 | ans += solve(i + 1, j - v[i], k - 1); 33 | return dp[i][j][k] = ans; 34 | } 35 | signed main() 36 | { 37 | ios_base::sync_with_stdio(false); 38 | cin.tie(NULL); 39 | cin >> n; 40 | for (int i = 0; i < n; i++) 41 | { 42 | cin >> v[i]; 43 | assert(v[i] <= 1000); 44 | v[i] += 1000; 45 | assert(v[i] >= 0); 46 | } 47 | memset(dp, -1, sizeof(dp)); 48 | int q; 49 | cin >> q; 50 | while (q--) 51 | { 52 | int x; 53 | cin >> x; 54 | x += 4000; 55 | cout << solve(0, x, 4) << endl; 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Problem Solving/Fase zero 2025/h.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | signed main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int x; 11 | cin >> x; 12 | if (x == 1) 13 | { 14 | cout << 1 << endl; 15 | return 0; 16 | } 17 | int msb; 18 | for (int b = 60; b >= 0; b--) 19 | { 20 | if (x & (1ll << b)) 21 | { 22 | msb = b; 23 | break; 24 | } 25 | } 26 | if ((1ll << msb) == x) 27 | { 28 | cout << x - 1 << endl; 29 | return 0; 30 | } 31 | int y = 0, l = msb, r = 0; 32 | while (l >= r) 33 | { 34 | int new_y = y | (1ll << l) | (1ll << r); 35 | if (new_y <= x) 36 | { 37 | y = new_y; 38 | } 39 | l--; 40 | r++; 41 | } 42 | cout << y << endl; 43 | return 0; 44 | } 45 | // para x = 1 a resposta eh 1 46 | // para potencias de dois, a resposta eh x - 1 47 | // caso contrario, o msb de x e y vai ser o mesmo, entao podemos aplicar a ideia greedy, que eh: 48 | // o msb de x e y vao ser o mesmo, entao podemos ir considerando do maior bit pro menor bit, e vendo se podemos setar 49 | // mas se tiver que setar um bit na posicao k, teremos que setar o bit na posicao msb - k tambem -------------------------------------------------------------------------------- /Problem Solving/Fase zero 2025/l.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1005 17 | #define MAXK 8005 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n; 24 | cin >> n; 25 | n *= 8000000; 26 | int pot = 1, ans = 0; 27 | while (pot < n) 28 | { 29 | ans++; 30 | pot *= 2; 31 | } 32 | cout << ans << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Problem Solving/Final 2017/h.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 500001 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | vector v(3); 25 | for (int i = 0; i < 3; i++) 26 | cin >> v[i]; 27 | int ans = 0; 28 | for (int i = 0; i < 3; i++) 29 | { 30 | int x; 31 | cin >> x; 32 | ans += max(0ll, x - v[i]); 33 | } 34 | cout << ans << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Problem Solving/Final 2018/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 100005 18 | #define mod 998244353 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | double x; 25 | cin >> x; 26 | x *= 100; 27 | int k = round(x); 28 | int g = __gcd((int)36000, k); 29 | int ans = 36000 / g; 30 | cout << ans << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2018/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 100005 18 | #define mod 998244353 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | int n; 25 | cin >> n; 26 | vector v(n); 27 | int sum = 0; 28 | for (int i = 0; i < n; i++) 29 | { 30 | cin >> v[i]; 31 | sum += v[i]; 32 | } 33 | if (sum % 2) 34 | { 35 | cout << "N\n"; 36 | return 0; 37 | } 38 | for (int i = 0; i < n; i++) 39 | { 40 | v.pb(v[i]); 41 | } 42 | int j = 0, s = 0, t = 0, ok = sum / 2; 43 | for (int i = 0; i < n; i++) 44 | { 45 | while (j < v.size() && s < ok) 46 | { 47 | s += v[j]; 48 | j++; 49 | } 50 | if (s == ok) 51 | { 52 | t++; 53 | if (t == 4) 54 | { 55 | cout << "Y\n"; 56 | return 0; 57 | } 58 | } 59 | s -= v[i]; 60 | } 61 | cout << "N\n"; 62 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2018/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 100005 18 | #define mod 998244353 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | int n; 25 | cin >> n; 26 | set s; 27 | for (int i = 0; i < n; i++) 28 | { 29 | string t; 30 | cin >> t; 31 | string c; 32 | int flag = 0; 33 | for (auto const &j : t) 34 | { 35 | if (j == '@') 36 | flag = 2; 37 | else if (j == '+' && flag == 0) 38 | flag = 1; 39 | else if (j == '.' && flag == 0) 40 | continue; 41 | else if (flag != 1) 42 | c.pb(j); 43 | } 44 | s.insert(c); 45 | } 46 | cout << s.size() << endl; 47 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2018/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 100005 18 | #define mod 998244353 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | int n; 25 | cin >> n; 26 | int mn = 1e18, ans = 1; 27 | for (int i = 0; i < n; i++) 28 | { 29 | int x; 30 | cin >> x; 31 | if (x > mn) 32 | ans++; 33 | mn = x; 34 | } 35 | cout << ans << endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2019/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define pb push_back 6 | #define int long long int 7 | #define double long double 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 13 | #define mod 1000000007 14 | 15 | string s; 16 | int n, num; 17 | 18 | namespace bit 19 | { 20 | vector bit; 21 | 22 | int sum(int r) 23 | { 24 | int ret = 0; 25 | for (; r >= 0; r = (r & (r + 1)) - 1) 26 | ret += bit[r]; 27 | return ret; 28 | } 29 | void add(int idx, int delta) 30 | { 31 | for (; idx < 2 * n; idx = idx | (idx + 1)) 32 | bit[idx] += delta; 33 | } 34 | } // namespace bit 35 | signed main() 36 | { 37 | ios_base::sync_with_stdio(false); 38 | cin.tie(NULL); 39 | cin >> s >> num; 40 | n = s.size(); 41 | bit::bit.resize(2 * n); 42 | for (int i = 0; i < 2 * n; i++) 43 | { 44 | int curr = (s[i % n] == 'E') ? 1 : 0; 45 | bit::add(i, curr); 46 | } 47 | int ans = 0; 48 | for (int i = 0; i < n; i++) 49 | { 50 | int l = i, r = i + num - 1, kappa = i + num - 1; 51 | while (l < r) 52 | { 53 | int mid = (l + r) >> 1; 54 | (bit::sum(mid) - bit::sum(i - 1)) ? r = mid : l = mid + 1; 55 | } 56 | if ((bit::sum(l) - bit::sum(i - 1))) 57 | ans += (kappa - l + 1); 58 | } 59 | cout << ans << endl; 60 | return 0; 61 | } 62 | // no maximo tamanho = tam 63 | // no minimo um eggfruit -------------------------------------------------------------------------------- /Problem Solving/Final 2019/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 5005 17 | #define mod 1000000007 18 | 19 | int s, b; 20 | int dp[MAXN][MAXN]; 21 | 22 | int solve(int i, int j) 23 | { 24 | if (j <= 0 || i < 0) 25 | return 0; 26 | if (!i) 27 | return 1; 28 | if (j == 1) 29 | return 1; 30 | if (dp[i][j] != -1) 31 | return dp[i][j]; 32 | int ret = 0; 33 | ret = (ret + solve(i - j, j)) % mod; 34 | ret = (ret + (2 * solve(i, j - 1))) % mod; 35 | ret = (ret - solve(i, j - 2) + mod) % mod; 36 | return dp[i][j] = ret; 37 | } 38 | signed main() 39 | { 40 | ios_base::sync_with_stdio(false); 41 | cin.tie(NULL); 42 | cin >> s >> b; 43 | memset(dp, -1, sizeof(dp)); 44 | cout << solve(b - s, s) << endl; 45 | return 0; 46 | } 47 | // dp[s][b] = dp[s - b][b] + (2 * dp[s][b - 1]) - dp[s][b - 2] -------------------------------------------------------------------------------- /Problem Solving/Final 2019/i.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define pb push_back 6 | #define int long long int 7 | #define double long double 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 2001 13 | #define mod 1000000007 14 | 15 | int n, l; 16 | vector adj[MAXN]; 17 | int dp[MAXN]; 18 | bool vis[MAXN]; 19 | 20 | int solve(int i) 21 | { 22 | if (dp[i] != -1) 23 | return dp[i]; 24 | int ret = 0; 25 | for (auto const &v : adj[i]) 26 | { 27 | if (v < l) 28 | { 29 | ret = (ret + solve(v)) % mod; 30 | } 31 | else 32 | { 33 | ret = (ret + 1) % mod; 34 | vis[v] = true; 35 | } 36 | } 37 | return dp[i] = ret; 38 | } 39 | signed main() 40 | { 41 | ios_base::sync_with_stdio(false); 42 | cin.tie(NULL); 43 | cin >> n >> l; 44 | for (int i = 0; i < l; i++) 45 | { 46 | int m; 47 | cin >> m; 48 | for (int j = 0; j < m; j++) 49 | { 50 | int a; 51 | cin >> a; 52 | a--; 53 | adj[i].pb(a); 54 | } 55 | } 56 | memset(dp, -1, sizeof(dp)); 57 | cout << solve(0) << " "; 58 | int ans = 0; 59 | for (int i = 0; i < n; i++) 60 | ans += vis[i]; 61 | cout << ans << endl; 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2019/k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 2405 17 | #define mod 1000000007 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | string s; 24 | cin >> s; 25 | vector ans; 26 | ans.pb(1); 27 | for (int i = 0; i < s.size() - 1; i++) 28 | { 29 | if (s[i] != s[i + 1]) 30 | { 31 | ans.pb(0); 32 | vector prev = ans; 33 | for (int j = ans.size() - 1; j >= 0; j--) 34 | ans[j] = ((2 * i) + 3) * -1 * ans[j]; 35 | for (int j = ans.size() - 1; j > 0; j--) 36 | ans[j] += prev[j - 1]; 37 | } 38 | } 39 | reverse(ans.begin(), ans.end()); 40 | cout << ans.size() - 1 << endl; 41 | for (auto const &i : ans) 42 | (s.back() == 'A') ? cout << i * -1 << " " : cout << i << " "; 43 | cout << endl; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2019/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 2405 17 | #define mod 1000000007 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n, m; 24 | cin >> n >> m; 25 | vector v(n); 26 | for (int i = 0; i < n; i++) 27 | cin >> v[i]; 28 | int ans = 0; 29 | for (int i = 0; i < n; i++) 30 | { 31 | int j = i; 32 | while (j + 1 < n && v[j] + m >= v[j + 1]) 33 | j++; 34 | ans = max(ans, j - i + 1); 35 | } 36 | cout << ans << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2020/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 100005 17 | #define mod 1000000007 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n; 24 | cin >> n; 25 | priority_queue, greater> pq; 26 | for (int i = 0; i < n; i++) 27 | { 28 | int x; 29 | cin >> x; 30 | pq.push(x); 31 | } 32 | if (n == 1) 33 | { 34 | cout << "N\n"; 35 | return 0; 36 | } 37 | bool has = false, can = true; 38 | while (pq.size()) 39 | { 40 | if (pq.size() == 2 && !has) 41 | break; 42 | if (pq.size() <= 1) 43 | break; 44 | int x = pq.top(); 45 | pq.pop(); 46 | int y = pq.top(); 47 | pq.pop(); 48 | if (x == y) 49 | { 50 | pq.push(x + 1); 51 | } 52 | else if (!has) 53 | { 54 | has = true; 55 | pq.push(y); 56 | } 57 | else 58 | { 59 | can = false; 60 | break; 61 | } 62 | } 63 | (can) ? cout << "Y\n" : cout << "N\n"; 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2020/g.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 105 17 | #define mod 1000000007 18 | 19 | int n; 20 | double dp[MAXN][MAXN][MAXN]; 21 | 22 | double solve(int i, int j, int k) 23 | { 24 | if (!i && !j) 25 | return 0; 26 | if (dp[i][j][k] != -1) 27 | return dp[i][j][k]; 28 | double ret = 0; 29 | if (i) 30 | { 31 | double prob = (double)i / (i + j); 32 | if (k) 33 | ret += (solve(i - 1, j, k - 1) * prob); 34 | else 35 | ret += ((i + solve(i - 1, j, k)) * prob); 36 | } 37 | if (j) 38 | { 39 | double prob = (double)j / (i + j); 40 | ret += (solve(i, j - 1, k + 1) * prob); 41 | } 42 | return dp[i][j][k] = ret; 43 | } 44 | signed main() 45 | { 46 | ios_base::sync_with_stdio(false); 47 | cin.tie(NULL); 48 | cin >> n; 49 | for (int i = 0; i <= n; i++) 50 | for (int j = 0; j <= n; j++) 51 | for (int k = 0; k <= n; k++) 52 | dp[i][j][k] = -1; 53 | cout << fixed << setprecision(6) << solve(n, n, 0) << endl; 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2020/l.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 300005 17 | #define mod 1000000007 18 | 19 | bool vis[MAXN]; 20 | 21 | signed main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | int n, x; 26 | cin >> n >> x; 27 | for (int i = 0; i < n; i++) 28 | { 29 | int a, b; 30 | cin >> a >> b; 31 | for (int i = a; i <= a + b; i++) 32 | vis[i] = true; 33 | } 34 | int ans = -1, c = INT_MAX; 35 | for (int i = 0; i <= 480; i++) 36 | { 37 | int t = i, cc = 0; 38 | while (t < MAXN) 39 | { 40 | if (vis[t]) 41 | cc++; 42 | t += x; 43 | } 44 | if (cc < c) 45 | { 46 | c = cc; 47 | ans = i; 48 | } 49 | } 50 | cout << ans << " " << c << endl; 51 | return 0; 52 | } 53 | // tomar a cada x minutos 54 | // t[0] 55 | // 8 -------------------------------------------------------------------------------- /Problem Solving/Final 2020/n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 100005 17 | #define mod 1000000007 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n; 24 | cin >> n; 25 | char c; 26 | double x; 27 | cin >> c >> x; 28 | int sum = x * 100, ans = 0; 29 | for (int i = 0; i < n; i++) 30 | { 31 | cin >> c >> x; 32 | x *= 100; 33 | sum += x; 34 | if (sum % 100) 35 | ans++; 36 | } 37 | cout << ans << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2021/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300001 18 | #define mod 1000000007 19 | 20 | int ans[MAXN]; 21 | bool vis[MAXN]; 22 | vector adj[MAXN]; 23 | 24 | void dfs(int s) 25 | { 26 | ans[s] = 2; 27 | vis[s] = 1; 28 | for (auto const &i : adj[s]) 29 | { 30 | if (!vis[i] && ans[i] != 1) 31 | dfs(i); 32 | } 33 | } 34 | signed main() 35 | { 36 | ios_base::sync_with_stdio(false); 37 | cin.tie(NULL); 38 | int n, m; 39 | cin >> n >> m; 40 | for (int i = 0; i < m; i++) 41 | { 42 | int a, b; 43 | cin >> a >> b; 44 | a--, b--; 45 | adj[a].pb(b); 46 | adj[b].pb(a); 47 | } 48 | ans[n - 1] = 1; 49 | ans[n - 2] = 2; 50 | dfs(n - 2); 51 | for (int i = 0; i < n; i++) 52 | (ans[i] == 2) ? cout << "B" : cout << "A"; 53 | cout << endl; 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2021/k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pbc push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 3000001 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | int n; 25 | cin >> n; 26 | map vis; 27 | vector v(n); 28 | for (int i = 0; i < n; i++) 29 | { 30 | cin >> v[i]; 31 | vis[v[i][0]] = 1; 32 | } 33 | for (auto const &i : v) 34 | { 35 | bool ok = 1; 36 | for (auto const &j : i) 37 | { 38 | if (!vis[j]) 39 | { 40 | ok = 0; 41 | break; 42 | } 43 | } 44 | if (ok) 45 | { 46 | cout << "Y\n"; 47 | return 0; 48 | } 49 | } 50 | cout << "N\n"; 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2022/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | 5 | #define pi pair 6 | #define pii pair 7 | #define fir first 8 | #define sec second 9 | #define pb push_back 10 | 11 | int n, p, h; 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | cin >> n >> p >> h; 18 | int hh = h; 19 | vector ans; // 1 contra, 0 a favor 20 | vector ans2(n); 21 | for (int i = n; i >= 1; i--) 22 | { 23 | int k = 1ll << i; 24 | if (hh > k / 2) 25 | { 26 | ans.pb(1); 27 | hh = k - hh + 1; 28 | } 29 | else 30 | { 31 | ans.pb(0); 32 | } 33 | } 34 | reverse(ans.begin(), ans.end()); 35 | int v = 1ll << n; 36 | for (int i = 0; i < n; i++) 37 | { 38 | if (ans[i]) 39 | { 40 | if (p > v / 2) 41 | { 42 | ans2[i] = 'R'; 43 | p = v - p + 1; 44 | } 45 | else 46 | { 47 | ans2[i] = 'L'; 48 | p = v / 2 - p + 1; 49 | } 50 | v >>= 1; 51 | } 52 | else 53 | { 54 | if (p > v / 2) 55 | { 56 | ans2[i] = 'L'; 57 | p = p - v / 2; 58 | } 59 | else 60 | { 61 | ans2[i] = 'R'; 62 | } 63 | v >>= 1; 64 | } 65 | cout << ans2[i]; 66 | } 67 | cout << endl; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2022/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | signed main() 7 | { 8 | int n, h, w; 9 | cin >> n >> h >> w; 10 | while (n--) 11 | { 12 | char a, b; 13 | cin >> a >> b; 14 | if (a == 'Y') 15 | { 16 | w++; 17 | h--; 18 | cout << "Y "; 19 | } 20 | else if (w == 0) 21 | { 22 | w++; 23 | h--; 24 | cout << "Y "; 25 | } 26 | else 27 | { 28 | cout << "N "; 29 | } 30 | 31 | if (b == 'Y') 32 | { 33 | w--; 34 | h++; 35 | cout << "Y "; 36 | } 37 | else if (h == 0) 38 | { 39 | w--; 40 | h++; 41 | cout << "Y "; 42 | } 43 | else 44 | { 45 | cout << "N "; 46 | } 47 | cout << endl; 48 | } 49 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2022/i.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pi pair 5 | #define pii pair 6 | #define fir first 7 | #define sec second 8 | 9 | int dx[] = {0, 0, 1, -1}; 10 | int dy[] = {1, -1, 0, 0}; 11 | 12 | int r, c; 13 | int v[101][101]; 14 | bool vis[101][101]; 15 | 16 | int solve(int i, int j) 17 | { 18 | for (int i = 0; i < r; i++) 19 | { 20 | for (int j = 0; j < c; j++) 21 | vis[i][j] = 0; 22 | } 23 | priority_queue pq; 24 | pq.push({-v[i][j], {i, j}}); 25 | vis[i][j] = 1; 26 | int ans = 0, mx = -1; 27 | while (!pq.empty()) 28 | { 29 | int x = pq.top().sec.fir; 30 | int y = pq.top().sec.sec; 31 | pq.pop(); 32 | if (v[x][y] <= mx) 33 | continue; 34 | mx = v[x][y]; 35 | ans++; 36 | for (int d = 0; d < 4; d++) 37 | { 38 | int ii = x + dx[d], jj = y + dy[d]; 39 | if (ii >= 0 && jj >= 0 && ii < r && jj < c && !vis[ii][jj]) 40 | { 41 | vis[ii][jj] = 1; 42 | pq.push({-v[ii][jj], {ii, jj}}); 43 | } 44 | } 45 | } 46 | return ans; 47 | } 48 | signed main() 49 | { 50 | ios_base::sync_with_stdio(false); 51 | cin.tie(NULL); 52 | cin >> r >> c; 53 | for (int i = 0; i < r; i++) 54 | { 55 | for (int j = 0; j < c; j++) 56 | cin >> v[i][j]; 57 | } 58 | int ans = 0; 59 | for (int i = 0; i < r; i++) 60 | { 61 | for (int j = 0; j < c; j++) 62 | ans = max(ans, solve(i, j)); 63 | } 64 | cout << ans << endl; 65 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2022/k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 1005 18 | #define mod 1000000007 19 | #define INF 1e9 20 | 21 | int v[102][102]; 22 | 23 | signed main() 24 | { 25 | ios_base::sync_with_stdio(false); 26 | cin.tie(NULL); 27 | int n; 28 | cin >> n; 29 | int ans = 0; 30 | while ((1 << ans) < n) 31 | ans++; 32 | cout << ans << endl; 33 | int cnt = 1, x = 0; 34 | for (int i = 1; i <= 100; i++) 35 | { 36 | for (int j = 1; j <= 100; j++) 37 | { 38 | if (i % 2 || j == 100 || cnt >= n) 39 | v[i][j] = (1 << ans) - 1; 40 | else 41 | v[i][j] = x++, cnt++; 42 | } 43 | } 44 | for (int b = 0; b < ans; b++) 45 | { 46 | vector curr; 47 | for (int i = 1; i <= 100; i++) 48 | { 49 | for (int j = 1; j <= 100; j++) 50 | { 51 | if (v[i][j] & (1 << b)) 52 | curr.pb({i, j}); 53 | } 54 | } 55 | cout << curr.size() << " "; 56 | for (auto const &i : curr) 57 | cout << i.fir << " " << i.sec << " "; 58 | cout << endl; 59 | } 60 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2023/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | signed main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int n; 11 | cin >> n; 12 | int m = 3 * n; 13 | vector v(m); 14 | map cnt; 15 | for (int i = 0; i < m; i++) 16 | { 17 | cin >> v[i]; 18 | cnt[v[i]]++; 19 | } 20 | bool ok = 1; 21 | for (auto const &i : cnt) 22 | { 23 | if (i.second % 3 != 0) 24 | ok = 0; 25 | } 26 | if (!ok) 27 | cout << "Y\n"; 28 | else 29 | cout << "N\n"; 30 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2023/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | signed main() 7 | { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | int n; 11 | cin >> n; 12 | string s; 13 | cin >> s; 14 | map mp; 15 | mp["*****"]++; 16 | for (int i = 1; i < n; i++) 17 | { 18 | string t; 19 | cin >> t; 20 | string tt; 21 | for (int j = 0; j < 5; j++) 22 | { 23 | if (s[j] == t[j]) 24 | tt.push_back('*'); 25 | else if (s.find(t[j]) != string::npos) 26 | tt.push_back('!'); 27 | else 28 | tt.push_back('X'); 29 | } 30 | mp[tt]++; 31 | } 32 | int q; 33 | cin >> q; 34 | while (q--) 35 | { 36 | string s; 37 | cin >> s; 38 | cout << mp[s] << endl; 39 | } 40 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2023/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define mod 1000000007 7 | 8 | bool check(int n, int b) 9 | { 10 | vector v; 11 | while (n > 0) 12 | { 13 | v.push_back(n % b); 14 | n /= b; 15 | } 16 | vector v2 = v; 17 | reverse(v.begin(), v.end()); 18 | return (v == v2); 19 | } 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | int n; 25 | cin >> n; 26 | set s; 27 | for (int i = 1; i * i <= n; i++) 28 | { 29 | if (n % i == 0) 30 | { 31 | s.insert(i); 32 | s.insert(n / i); 33 | } 34 | } 35 | set ans; 36 | for (auto const &i : s) 37 | { 38 | if (i >= 3 && check(n, i - 1)) 39 | ans.insert(i - 1); 40 | } 41 | for (int i = 2; i <= min(n, 1000005ll); i++) 42 | { 43 | if (check(n, i)) 44 | ans.insert(i); 45 | } 46 | if (!ans.size()) 47 | cout << "*"; 48 | for (auto const &i : ans) 49 | cout << i << " "; 50 | cout << endl; 51 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2023/i.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define mod 1000000007 7 | 8 | int solve(string &s) 9 | { 10 | vector mp(27, 0); 11 | int ans = 0; 12 | for (auto const &i : s) 13 | { 14 | int pos = i - 'a'; 15 | for (int j = pos + 1; j < 27; j++) 16 | ans += mp[j]; 17 | mp[i - 'a']++; 18 | } 19 | return ans; 20 | } 21 | signed main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | string s; 26 | int n; 27 | cin >> s >> n; 28 | map mp; 29 | int sum = 0; 30 | for (auto const &i : s) 31 | { 32 | mp[i]++; 33 | sum++; 34 | } 35 | int ans = 0; 36 | for (auto const &i : mp) 37 | { 38 | sum -= i.second; 39 | __int128 qt = ((__int128)i.second * sum) % mod; 40 | __int128 pa = ((__int128)n * (n - 1)) / 2; 41 | pa %= mod; 42 | qt *= pa; 43 | qt %= mod; 44 | ans += qt; 45 | ans %= mod; 46 | } 47 | __int128 inv = solve(s); 48 | inv %= mod; 49 | inv *= n; 50 | inv %= mod; 51 | ans += inv; 52 | ans %= mod; 53 | cout << ans << endl; 54 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2024/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | signed main() 5 | { 6 | ios::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | string s; 9 | cin >> s; 10 | int n = s.size(); 11 | for (int i = n - 1; i >= 1; i--) 12 | { 13 | string t = s.substr(0, i); 14 | string t2 = s.substr(i, n - i); 15 | sort(t.begin(), t.end()); 16 | t.erase(unique(t.begin(), t.end()), t.end()); 17 | if (t == t2) 18 | { 19 | cout << i << endl; 20 | return 0; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Problem Solving/Final 2024/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check(string &s) 5 | { 6 | int n = s.size(); 7 | for (int i = 0; i < n; i++) 8 | { 9 | bool ok = (s[i] == 'X'); 10 | if (i - 1 >= 0 && s[i - 1] == 'X') 11 | ok = 1; 12 | if (i + 1 < n && s[i + 1] == 'X') 13 | ok = 1; 14 | if (!ok) 15 | return 0; 16 | } 17 | return 1; 18 | } 19 | signed main() 20 | { 21 | ios::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int a, b; 24 | cin >> a >> b; 25 | int qt = (b + 2) / 3; 26 | for (int ini = 0; ini <= 1; ini++) 27 | { 28 | if (b == 1 && ini == 1) 29 | { 30 | break; 31 | } 32 | string s(b, '-'); 33 | int ptr = ini; 34 | bool ok = 1; 35 | for (int i = 0; i < a; i++) 36 | { 37 | if (ptr >= b) 38 | { 39 | ok = 0; 40 | break; 41 | } 42 | s[ptr] = 'X'; 43 | int rem = a - (i + 1); 44 | int len = b - (ptr + 3); 45 | int to_put = (len + 2) / 3; 46 | if (to_put >= rem) 47 | { 48 | ptr += 3; 49 | } 50 | else 51 | { 52 | ptr += 2; 53 | } 54 | } 55 | if (ok && check(s)) 56 | { 57 | cout << s << endl; 58 | return 0; 59 | } 60 | } 61 | cout << "*\n"; 62 | } 63 | // 3 em 3 64 | // mas pode ser de 2 em 2 -------------------------------------------------------------------------------- /Problem Solving/Final 2024/j.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pi pair 6 | #define fir first 7 | #define sec second 8 | #define pb push_back; 9 | #define PI acos(-1) 10 | 11 | signed main() 12 | { 13 | ios::sync_with_stdio(false); 14 | cin.tie(NULL); 15 | int ang, n; 16 | cin >> ang >> n; 17 | double a = ang; 18 | double rad = (a * PI) / 180; 19 | double tg = tan(rad); 20 | vector v(n); 21 | for (int i = 0; i < n; i++) 22 | cin >> v[i].fir >> v[i].sec; 23 | sort(v.rbegin(), v.rend()); 24 | double ans = 0.0; 25 | vector> vec(n); 26 | for (int i = 0; i < n; i++) 27 | { 28 | double x = (double)v[i].sec / tg; 29 | vec[i] = {v[i].fir, (double)v[i].fir + x}; 30 | } 31 | sort(vec.begin(), vec.end()); 32 | double ll, rr; 33 | for (int i = 0; i < n; i++) 34 | { 35 | if (i == 0) 36 | { 37 | ll = vec[i].fir; 38 | rr = vec[i].sec; 39 | } 40 | else 41 | { 42 | if (vec[i].fir > rr) 43 | { 44 | ans += (rr - ll); 45 | ll = vec[i].fir; 46 | rr = vec[i].sec; 47 | } 48 | else 49 | { 50 | ll = min(ll, vec[i].fir); 51 | rr = max(rr, vec[i].sec); 52 | } 53 | } 54 | } 55 | ans += (rr - ll); 56 | cout << fixed << setprecision(15) << ans << endl; 57 | } -------------------------------------------------------------------------------- /Problem Solving/GEMA PC 1.0/b.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonh14lk/Competitive_Programming/7dd840599cfb127ae873e59bf2db29338de55292/Problem Solving/GEMA PC 1.0/b.cpp -------------------------------------------------------------------------------- /Problem Solving/GEMA PC 1.0/c.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonh14lk/Competitive_Programming/7dd840599cfb127ae873e59bf2db29338de55292/Problem Solving/GEMA PC 1.0/c.cpp -------------------------------------------------------------------------------- /Problem Solving/GEMA PC 1.0/d.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonh14lk/Competitive_Programming/7dd840599cfb127ae873e59bf2db29338de55292/Problem Solving/GEMA PC 1.0/d.cpp -------------------------------------------------------------------------------- /Problem Solving/GEMA PC 1.0/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | #define in insert 7 | #define pi pair 8 | #define pii pair 9 | #define mp make_pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 1001 13 | 14 | vector v; 15 | vector v2; 16 | lli n, m; 17 | 18 | bool possible(lli x) 19 | { 20 | int j = 0; 21 | for (int i = 0; i < n; i++) 22 | { 23 | while (1) 24 | { 25 | if (v2[j] - x <= v[i] && v2[j] >= v[i]) 26 | break; 27 | else if (v2[j] + x >= v[i] && v2[j] <= v[i]) 28 | break; 29 | else if (j == m - 1) 30 | return false; 31 | else 32 | j++; 33 | } 34 | } 35 | return true; 36 | } 37 | lli bb() 38 | { 39 | lli i = 0, f = INT_MAX, m; 40 | while (i < f) 41 | { 42 | m = (i + f) / 2; 43 | if (possible(m)) 44 | { 45 | f = m; 46 | } 47 | else 48 | { 49 | i = m + 1; 50 | } 51 | } 52 | return i; 53 | } 54 | signed main() 55 | { 56 | ios_base::sync_with_stdio(false); 57 | cin.tie(NULL); 58 | cin >> n >> m; 59 | v.resize(n); 60 | v2.resize(m); 61 | for (int i = 0; i < n; i++) 62 | cin >> v[i]; 63 | for (int i = 0; i < m; i++) 64 | cin >> v2[i]; 65 | sort(v.begin(), v.end()); 66 | sort(v2.begin(), v2.end()); 67 | cout << bb() << endl; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Problem Solving/GEMA PC 2.0/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define DEBUG(X) if (X) 5 | #define pb push_back 6 | typedef long long int lli; 7 | typedef vector vi; 8 | typedef vector vvi; 9 | typedef pair ii; 10 | typedef vector> vvii; 11 | const int maxN = 2e3; 12 | 13 | lli pd[maxN][maxN]; 14 | 15 | int main() 16 | { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(NULL); 19 | 20 | lli n, m; 21 | vi arr; 22 | cin >> n >> m; 23 | arr.resize(maxN); 24 | 25 | for (int i = 0; i < n; i++) 26 | { 27 | cin >> arr[i]; 28 | } 29 | 30 | for (int i = 0; i < m; i++) 31 | { 32 | for (int j = 0; j < n + 1; j++) 33 | { 34 | pd[i][j + 1] = (pd[i][j] + ((arr[j] % m) == i)); 35 | } 36 | } 37 | 38 | lli q; 39 | cin >> q; 40 | 41 | while (q--) 42 | { 43 | int l, r, d, sum = 0; 44 | cin >> l >> r >> d; 45 | l--; 46 | cout << (pd[d][r] - pd[d][l]) << endl; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Problem Solving/GEMA PC 2.0/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define DEBUG(X) if (X) 5 | #define pb push_back 6 | typedef long long int lli; 7 | typedef vector vi; 8 | typedef vector vvi; 9 | typedef pair ii; 10 | typedef vector> vvii; 11 | 12 | int mat[50][50]; 13 | int second[50][50]; 14 | 15 | void go(int n, int m, int r, int s) 16 | { 17 | int qnt = 0; 18 | for (int i = 0; i < n; i++) 19 | { 20 | for (int j = 0; j < m; j++) 21 | { 22 | if (i + r <= n && j + s <= m) 23 | { 24 | int count = 1; 25 | for (int k = 0; k < r; k++) 26 | { 27 | for (int w = 0; w < s; w++) 28 | { 29 | // printf("i = %d, j = %d, k = %d, w = %d\n", i, j, k, w); 30 | if (mat[i + k][j + w] != second[k][w]) 31 | { 32 | count = 0; 33 | break; 34 | } 35 | } 36 | } 37 | qnt += count; 38 | } 39 | } 40 | } 41 | cout << qnt << endl; 42 | } 43 | 44 | int main() 45 | { 46 | ios_base::sync_with_stdio(false); 47 | cin.tie(NULL); 48 | int n, m; 49 | cin >> n >> m; 50 | 51 | for (int i = 0; i < n; i++) 52 | for (int j = 0; j < m; j++) 53 | cin >> mat[i][j]; 54 | int l, r; 55 | cin >> l >> r; 56 | 57 | for (int i = 0; i < l; i++) 58 | for (int j = 0; j < r; j++) 59 | cin >> second[i][j]; 60 | go(n, m, l, r); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /Problem Solving/GEMA PC 3.0/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 10001 11 | #define mod 1000000007 12 | 13 | int n, r; 14 | vector adj[MAXN]; 15 | int t[MAXN]; 16 | 17 | int dfs(int s, int f) 18 | { 19 | vector vec; 20 | for (auto const &v : adj[s]) 21 | { 22 | if (v != f) 23 | { 24 | int at = 1 + dfs(v, s); 25 | vec.pb(at); 26 | } 27 | } 28 | sort(vec.rbegin(), vec.rend()); 29 | int ans = 0, inc = 0; 30 | for (auto const &i : vec) 31 | { 32 | int curr = i + inc; 33 | ans = max(ans, curr); 34 | inc++; 35 | } 36 | return ans; 37 | } 38 | signed main() 39 | { 40 | ios_base::sync_with_stdio(false); 41 | cin.tie(NULL); 42 | int q; 43 | cin >> q; 44 | while (q--) 45 | { 46 | cin >> n >> r; 47 | r--; 48 | for (int i = 0; i < MAXN; i++) 49 | { 50 | adj[i].clear(); 51 | t[i] = 0; 52 | } 53 | for (int i = 0; i < n - 1; i++) 54 | { 55 | int a, b; 56 | cin >> a >> b; 57 | a--, b--; 58 | adj[a].pb(b); 59 | adj[b].pb(a); 60 | } 61 | cout << dfs(r, -1) << endl; 62 | } 63 | } -------------------------------------------------------------------------------- /Problem Solving/GEMA PC 3.0/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 101 11 | #define mod 1000000007 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | int q; 18 | cin >> q; 19 | while (q--) 20 | { 21 | string s; 22 | cin >> s; 23 | int best = -1; 24 | for (int i = 0; i < s.size(); i++) 25 | { 26 | string t = s; 27 | s.erase(s.begin() + i); 28 | int curr = stoll(s); 29 | best = max(best, curr); 30 | s = t; 31 | } 32 | cout << best << endl; 33 | } 34 | } -------------------------------------------------------------------------------- /Problem Solving/Latam 2023/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1005 17 | #define mod 1000000007 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n; 24 | cin >> n; 25 | if (n == 1) 26 | { 27 | cout << "N\n"; 28 | return 0; 29 | } 30 | vector p; 31 | while (n % 2 == 0) 32 | { 33 | p.pb(2); 34 | n = n / 2; 35 | } 36 | for (int i = 3; i <= sqrt(n); i += 2) 37 | { 38 | while (n % i == 0) 39 | { 40 | p.pb(i); 41 | n = n / i; 42 | } 43 | } 44 | if (n > 2) 45 | { 46 | p.pb(n); 47 | } 48 | sort(p.begin(), p.end()); 49 | if (p.size() == 2 && p[0] != p[1]) 50 | cout << "Y\n"; 51 | else if (p[0] == p.back() && (p.size() % 2) == 1) 52 | cout << "Y\n"; 53 | else 54 | cout << "N\n"; 55 | } -------------------------------------------------------------------------------- /Problem Solving/Latam 2023/k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | // #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1000005 17 | #define mod 1000000007 18 | 19 | const int inf = 1e9; 20 | 21 | int n; 22 | string s[MAXN]; 23 | int dp[MAXN][3]; 24 | 25 | bool is(char c) 26 | { 27 | return (c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U' && c != 'Y'); 28 | } 29 | int solve(int i, int j) 30 | { 31 | if (i == n) 32 | return 0; 33 | if (dp[i][j] != -1) 34 | return dp[i][j]; 35 | int ans = inf, cons = j; 36 | for (int x = 1; x <= min((int)s[i].size(), 3); x++) 37 | { 38 | if (is(s[i][x - 1])) 39 | cons++; 40 | else 41 | cons = 0; 42 | if (cons <= 2) 43 | ans = min(ans, x + solve(i + 1, cons)); 44 | else 45 | break; 46 | } 47 | return dp[i][j] = ans; 48 | } 49 | signed main() 50 | { 51 | ios_base::sync_with_stdio(false); 52 | cin.tie(NULL); 53 | cin >> n; 54 | for (int i = 0; i < n; i++) 55 | cin >> s[i]; 56 | memset(dp, -1, sizeof(dp)); 57 | int ans = solve(0, 0); 58 | (ans >= inf) ? cout << "*\n" : cout << ans << endl; 59 | } -------------------------------------------------------------------------------- /Problem Solving/Latam 2023/l.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1005 17 | #define mod 1000000007 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int r, c, k; 24 | cin >> r >> c >> k; 25 | for (int i = 0; i < r; i++) 26 | { 27 | string s, t; 28 | cin >> s >> t; 29 | bool has = 0; 30 | for (int j = 0; j < k; j++) 31 | { 32 | if (t[j] == '*') 33 | has = 1; 34 | } 35 | if (has) 36 | { 37 | for (int j = 0; j < c; j++) 38 | { 39 | if (s[j] != '*') 40 | { 41 | cout << "N\n"; 42 | return 0; 43 | } 44 | } 45 | } 46 | } 47 | cout << "Y\n"; 48 | } -------------------------------------------------------------------------------- /Problem Solving/Latam 2024/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pb push_back 6 | 7 | signed main() 8 | { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(NULL); 11 | int c, n; 12 | cin >> c >> n; 13 | vector v(n); 14 | vector vals; 15 | for (int i = 0; i < n; i++) 16 | { 17 | cin >> v[i]; 18 | vals.pb(v[i]); 19 | } 20 | sort(vals.begin(), vals.end()); 21 | vals.erase(unique(vals.begin(), vals.end()), vals.end()); 22 | auto can = [&](int x) 23 | { 24 | int sq = 0; 25 | for (auto const &i : v) 26 | { 27 | if (i >= x) 28 | sq = 0; 29 | else 30 | sq++; 31 | if (sq > c) 32 | return 0; 33 | } 34 | return 1; 35 | }; 36 | int l = 0, r = vals.size() - 1; 37 | while (l < r) 38 | { 39 | int mid = (l + r + 1) >> 1; 40 | (can(vals[mid])) ? l = mid : r = mid - 1; 41 | } 42 | cout << vals[l] << endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Problem Solving/Latam 2024/l.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | string hue[10] = { 7 | "GGGgGGG", 8 | "gggggGG", 9 | "gGGGGGg", 10 | "ggGGGGG", 11 | "GggGgGG", 12 | "GgGGGgG", 13 | "GGGGGgG", 14 | "ggGggGG", 15 | "GGGGGGG", 16 | "GgGGGGG"}; 17 | 18 | signed main() 19 | { 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(NULL); 22 | int n; 23 | cin >> n; 24 | for (int i = 0; i < n; i++) 25 | { 26 | string t; 27 | cin >> t; 28 | int ans = -1, cnt = 0; 29 | for (int i = 0; i <= 9; i++) 30 | { 31 | bool ok = 1; 32 | for (int j = 0; j < 7; j++) 33 | { 34 | if (t[j] != '+' && t[j] != '-') 35 | ok &= (t[j] == hue[i][j]); 36 | } 37 | if (ok) 38 | { 39 | ans = i; 40 | cnt++; 41 | } 42 | } 43 | if (cnt == 1) 44 | { 45 | cout << ans; 46 | } 47 | else 48 | { 49 | cout << "*"; 50 | } 51 | } 52 | cout << endl; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b, c; 7 | cin >> a >> b >> c; 8 | vector v = {a, b, c}; 9 | sort(v.begin(), v.end()); 10 | if (v[0] == v[1] && v[1] != v[2]) 11 | cout << "Yes\n"; 12 | else if (v[0] != v[1] && v[1] == v[2]) 13 | cout << "Yes\n"; 14 | else 15 | cout << "No\n"; 16 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int q; 7 | cin >> q; 8 | while (q--) 9 | { 10 | int n; 11 | cin >> n; 12 | vector v(n); 13 | for (int i = 0; i < n; i++) 14 | cin >> v[i]; 15 | bool ok = true; 16 | for (char ch = 'a'; ch <= 'z'; ch++) 17 | { 18 | int tot = 0; 19 | for (int i = 0; i < n; i++) 20 | tot += count(v[i].begin(), v[i].end(), ch); 21 | if (tot % n != 0) 22 | ok = false; 23 | } 24 | (ok) ? cout << "YES\n" : cout << "NO\n"; 25 | } 26 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int r, c, k; 7 | cin >> r >> c >> k; 8 | vector cnt(r + 1, 0); 9 | for (int i = 0; i < c; i++) 10 | { 11 | int x; 12 | cin >> x; 13 | cnt[x]++; 14 | } 15 | sort(cnt.begin(), cnt.end()); 16 | reverse(cnt.begin(), cnt.end()); 17 | int ans = 0; 18 | for (int i = 0; i < min(k, r + 1); i++) 19 | ans += cnt[i]; 20 | cout << ans << endl; 21 | }; -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | vector v(3); 7 | for (int i = 0; i < 3; i++) 8 | cin >> v[i]; 9 | sort(v.rbegin(), v.rend()); 10 | (v[2] - v[1] == v[1] - v[0]) ? cout << "Yes\n" : cout << "No\n"; 11 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int // avoid overflow 5 | 6 | int sum_ap(int x) 7 | { 8 | return ((1 + x) * x) / 2; 9 | } 10 | signed main() 11 | { 12 | int n; 13 | cin >> n; 14 | int walked = 1, last_jump = 1, ans = 1; 15 | while (walked < n) 16 | { 17 | ans++; 18 | int remaining = n - walked; 19 | if (sum_ap(last_jump + 1) <= remaining) 20 | { 21 | walked += last_jump + 1; 22 | last_jump++; 23 | } 24 | else if (sum_ap(last_jump) <= remaining) 25 | { 26 | walked += last_jump; 27 | } 28 | else 29 | { 30 | walked += last_jump - 1; 31 | last_jump--; 32 | } 33 | } 34 | cout << ans << endl; 35 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int // avoid overflow 5 | 6 | signed main() 7 | { 8 | int x; 9 | cin >> x; 10 | vector ans; 11 | set s; 12 | for (int i = 1; i <= 100005; i++) 13 | { 14 | s.insert(i * i); 15 | if (s.count((i * i) - x)) 16 | ans.push_back(i); 17 | } 18 | cout << ans.size() << endl; 19 | for (int i = 0; i < ans.size(); i++) 20 | cout << ans[i] << " \n"[i == ans.size() - 1]; 21 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/g.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | while (n--) 9 | { 10 | int a, b; 11 | cin >> a >> b; 12 | set ans; 13 | auto factorize = [&](int x) 14 | { 15 | for (int i = 2; i * i <= x; i++) 16 | { 17 | while (x % i == 0) 18 | { 19 | x /= i; 20 | ans.insert(i); 21 | } 22 | } 23 | if (x > 1) 24 | ans.insert(x); 25 | }; 26 | factorize(a); 27 | factorize(b); 28 | cout << ans.size() << endl; 29 | } 30 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/h.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | vector v; 9 | auto generates = [&](string s, string t) 10 | { 11 | if (s.size() != t.size()) 12 | return false; 13 | for (int i = 0; i <= s.size(); i++) 14 | { 15 | rotate(s.begin(), s.begin() + 1, s.end()); 16 | if (s == t) 17 | return true; 18 | } 19 | return false; 20 | }; 21 | for (int i = 0; i < n; i++) 22 | { 23 | string s; 24 | cin >> s; 25 | bool ok = false; 26 | for (auto const &t : v) 27 | { 28 | if (generates(s, t)) 29 | { 30 | ok = true; 31 | break; 32 | } 33 | } 34 | if (!ok) 35 | v.push_back(s); 36 | } 37 | cout << v.size() << endl; 38 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/i.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string s, t; 7 | cin >> s >> t; 8 | int j = 0; 9 | for (int i = 0; i < s.size(); i++) 10 | { 11 | if (s[i] == t[j]) 12 | j++; 13 | if (j == t.size()) 14 | break; 15 | } 16 | (j == t.size()) ? cout << "YES\n" : cout << "NO\n"; 17 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/j.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | cout << (1ll << n) - (n + 1) << endl; 9 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | set s; 9 | for (int i = 0; i < n; i++) 10 | { 11 | int year, age; 12 | cin >> year >> age; 13 | s.insert(year - age); 14 | } 15 | (s.size() == 1) ? cout << "idades corretas\n" : cout << "mentiu a idade\n"; 16 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/l.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | vector v(3); 7 | for (int i = 0; i < 3; i++) 8 | cin >> v[i]; 9 | sort(v.begin(), v.end()); 10 | cout << v[1] + v[2] << endl; 11 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, q; 7 | string s; 8 | cin >> n >> q >> s; 9 | int ptr = 0; 10 | while (q--) 11 | { 12 | int t, x; 13 | cin >> t >> x; 14 | if (t == 1) 15 | ptr = (ptr + n - x) % n; 16 | else 17 | cout << s[(ptr + x - 1) % n] << endl; 18 | } 19 | } -------------------------------------------------------------------------------- /Problem Solving/Primeira Seletiva GEMA/n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int // avoid overflow 5 | #define mod 1000000007 // modulo 6 | 7 | signed main() 8 | { 9 | string s; 10 | cin >> s; 11 | vector dp(13, 0); 12 | dp[0] = 1; 13 | for (int i = 0; i < s.size(); i++) 14 | { 15 | vector new_dp(13, 0); 16 | if (s[i] == '?') 17 | { 18 | for (int d = 0; d <= 9; d++) 19 | { 20 | for (int j = 0; j < 13; j++) 21 | { 22 | int new_remainder = ((j * 10) + d) % 13; 23 | new_dp[new_remainder] = (new_dp[new_remainder] + dp[j]) % mod; 24 | } 25 | } 26 | } 27 | else 28 | { 29 | int d = s[i] - '0'; 30 | for (int j = 0; j < 13; j++) 31 | { 32 | int new_remainder = ((j * 10) + d) % 13; 33 | new_dp[new_remainder] = (new_dp[new_remainder] + dp[j]) % mod; 34 | } 35 | } 36 | swap(dp, new_dp); 37 | } 38 | cout << dp[5] << endl; 39 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2013/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define double long double 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pi pair 10 | #define pii pair 11 | #define fir first 12 | #define sec second 13 | #define MAXN 1000001 14 | #define MAXL 20 15 | #define mod 998244353 16 | 17 | signed main() 18 | { 19 | vector v(3); 20 | int m0 = -1, m1 = -1; 21 | for (int i = 0; i < 3; i++) 22 | { 23 | cin >> v[i]; 24 | if (v[i] == 0 && m0 == -1) 25 | m0 = i; 26 | else if (v[i] == 0 && m0 != -1) 27 | m0 = -2; 28 | else if (v[i] == 1 && m1 == -1) 29 | m1 = i; 30 | else if (v[i] == 1 && m1 != -1) 31 | m1 = -2; 32 | } 33 | char aux; 34 | if (m1 == -1 || m0 == -1) 35 | cout << "*" << endl; 36 | else if (m0 != -2) 37 | aux = m0 + 'A', cout << aux << endl; 38 | else 39 | aux = m1 + 'A', cout << aux << endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2013/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define double long double 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pi pair 10 | #define pii pair 11 | #define fir first 12 | #define sec second 13 | #define MAXN 1000001 14 | #define MAXL 20 15 | #define mod 998244353 16 | 17 | signed main() 18 | { 19 | int n, r; 20 | cin >> n >> r; 21 | set s; 22 | for (int i = 1; i <= n; i++) 23 | s.insert(i); 24 | for (int i = 0; i < r; i++) 25 | { 26 | int k; 27 | cin >> k; 28 | s.erase(k); 29 | } 30 | if (s.size() == 0) 31 | { 32 | cout << "*" << endl; 33 | } 34 | else 35 | { 36 | for (auto const &i : s) 37 | cout << i << " "; 38 | cout << endl; 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2013/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define double long double 7 | #define pb push_back 8 | #define mp make_pair 9 | #define pi pair 10 | #define pii pair 11 | #define fir first 12 | #define sec second 13 | #define MAXN 1000001 14 | #define MAXL 20 15 | #define mod 998244353 16 | 17 | signed main() 18 | { 19 | // a - c == b - a == b - c 20 | int n; 21 | cin >> n; 22 | vector p(n); 23 | vector v(n); 24 | set s; 25 | int dist; 26 | for (int i = 0; i < n; i++) 27 | { 28 | cin >> v[i]; 29 | if (i == 0) 30 | p[i] = v[i]; 31 | else 32 | p[i] = p[i - 1] + v[i]; 33 | if (i == n - 1) 34 | dist = p[i] / 3; 35 | } 36 | for (auto const &i : p) 37 | s.insert(i); 38 | int ans = 0; 39 | for (auto const &i : p) 40 | if (s.find(i + dist) != s.end() && s.find(i + dist + dist) != s.end()) 41 | ans++; 42 | cout << ans << endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2013/i.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 1005 17 | #define mod 1000000007 18 | 19 | int n, c, t1, t2; 20 | vector v; 21 | int dp[MAXN]; 22 | 23 | int solve(int i) 24 | { 25 | if (i == n) 26 | return 0; 27 | if (dp[i] != -1) 28 | return dp[i]; 29 | int ret = 1e18; 30 | { 31 | int j = i + 1; 32 | while (j < n && v[j] - v[i] <= t1) 33 | j++; 34 | ret = min(ret, t1 + solve(j)); 35 | } 36 | { 37 | int j = i + 1; 38 | while (j < n && v[j] - v[i] <= t2) 39 | j++; 40 | ret = min(ret, t2 + solve(j)); 41 | } 42 | return dp[i] = ret; 43 | } 44 | signed main() 45 | { 46 | ios_base::sync_with_stdio(false); 47 | cin.tie(NULL); 48 | cin >> n >> c >> t1 >> t2; 49 | v.resize(n); 50 | for (int i = 0; i < n; i++) 51 | cin >> v[i]; 52 | sort(v.begin(), v.end()); 53 | memset(dp, -1, sizeof(dp)); 54 | cout << solve(0) << endl; 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2017/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 100001 11 | #define mod 1000000007 12 | 13 | int lcm(int a, int b) 14 | { 15 | return (a / __gcd(a, b)) * b; 16 | } 17 | signed main() 18 | { 19 | ios_base::sync_with_stdio(false); 20 | cin.tie(NULL); 21 | int n, l; 22 | cin >> n >> l; 23 | vector v(n); 24 | for (int i = 0; i < n; i++) 25 | cin >> v[i]; 26 | int lc = v[0]; 27 | for (int i = 1; i < n; i++) 28 | lc = lcm(lc, v[i]); 29 | int ans = INT_MIN, idx = 1; 30 | for (int i = 1; i <= l; i++) 31 | { 32 | int curr = lcm(lc, i); 33 | if (curr > ans && curr <= l) 34 | { 35 | ans = curr; 36 | idx = i; 37 | } 38 | } 39 | cout << idx << endl; 40 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2017/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 5001 11 | #define mod 1000000007 12 | 13 | vector facts; 14 | 15 | void primefactors(int n) 16 | { 17 | while (n % 2 == 0) 18 | { 19 | facts.pb(2); 20 | n = n / 2; 21 | } 22 | for (int i = 3; i <= sqrt(n); i += 2) 23 | { 24 | while (n % i == 0) 25 | { 26 | facts.pb(i); 27 | n = n / i; 28 | } 29 | } 30 | if (n > 2) 31 | facts.pb(n); 32 | } 33 | bool is(int n) 34 | { 35 | facts.clear(); 36 | primefactors(n); 37 | set s; 38 | for (auto const &i : facts) 39 | s.insert(i); 40 | return (s.size() == facts.size() && facts.size() >= 2); 41 | } 42 | signed main() 43 | { 44 | ios_base::sync_with_stdio(false); 45 | cin.tie(NULL); 46 | int n; 47 | cin >> n; 48 | int ans = 0; 49 | for (int i = 1; i <= sqrt(n); i++) 50 | { 51 | if (!(n % i)) 52 | { 53 | if (is(i)) 54 | ans++; 55 | if (n / i != i) 56 | if (is(n / i)) 57 | ans++; 58 | } 59 | } 60 | cout << ans << endl; 61 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2017/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 100001 11 | #define mod 1000000007 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | int n; 18 | cin >> n; 19 | vector notas = {"do", "do#", "re", "re#", "mi", "fa", "fa#", "sol", "sol#", "la", "la#", "si"}; 20 | vector curr = {2, 2, 1, 2, 2, 2, 1}; 21 | vector v(n); 22 | for (int i = 0; i < n; i++) 23 | { 24 | cin >> v[i]; 25 | v[i]--; 26 | v[i] = v[i] % notas.size(); 27 | } 28 | for (int i = 0; i < notas.size(); i++) 29 | { 30 | bool can = true; 31 | set s; 32 | int pref = i; 33 | s.insert(pref); 34 | for (auto const &j : curr) 35 | { 36 | pref = (pref + j) % notas.size(); 37 | s.insert(pref); 38 | } 39 | for (auto const &j : v) 40 | if (s.find(j) == s.end()) 41 | can = false; 42 | if (can) 43 | { 44 | cout << notas[i] << endl; 45 | return 0; 46 | } 47 | } 48 | cout << "desafinado\n"; 49 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2017/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 5001 11 | #define mod 1000000007 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | int n, k; 18 | cin >> n >> k; 19 | vector v(n); 20 | vector cnt(1001, 0); 21 | for (int i = 0; i < n; i++) 22 | cin >> v[i], cnt[v[i]]++; 23 | int acc = 0; 24 | for (int i = 1000; i >= 0; i--) 25 | { 26 | acc += cnt[i]; 27 | if (acc >= k) 28 | break; 29 | } 30 | cout << acc << endl; 31 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2017/g.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 100002 11 | #define mod 1000000007 12 | 13 | int dp[MAXN][52]; 14 | int t, n, m; 15 | 16 | int solve(int i, int j) 17 | { 18 | if (j == t) 19 | return dp[i][j] = 1; 20 | if (dp[i][j] != -1) 21 | return dp[i][j]; 22 | int resp = 0; 23 | if (i - 1 >= n) 24 | resp = (resp + solve(i - 1, j + 1)) % mod; 25 | if (i + 1 <= m) 26 | resp = (resp + solve(i + 1, j + 1)) % mod; 27 | return dp[i][j] = resp; 28 | } 29 | signed main() 30 | { 31 | ios_base::sync_with_stdio(false); 32 | cin.tie(NULL); 33 | cin >> t >> n >> m; 34 | t--; 35 | memset(dp, -1, sizeof(dp)); 36 | int ans = 0; 37 | for (int i = n; i <= m; i++) 38 | ans = (ans + solve(i, 0)) % mod; 39 | cout << ans << endl; 40 | } 41 | -------------------------------------------------------------------------------- /Problem Solving/Subregional 2017/j.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 100002 11 | #define mod 1000000007 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | string s; 18 | cin >> s; 19 | int acc = 0; 20 | for (int i = 0; i < s.size(); i++) 21 | acc += (s[i] - '0'); 22 | cout << acc % 3 << endl; 23 | } 24 | // 1 - 2 - 1 - 2 - 1 -------------------------------------------------------------------------------- /Problem Solving/Subregional 2017/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 5001 11 | #define mod 1000000007 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | vector v(3); 18 | cin >> v[0] >> v[1] >> v[2]; 19 | int ans = LLONG_MAX; 20 | for (int i = 0; i < 3; i++) 21 | { 22 | int curr = 0; 23 | for (int j = 0; j < 3; j++) 24 | curr += (abs(i - j) * v[j] * 2); 25 | ans = min(ans, curr); 26 | } 27 | cout << ans << endl; 28 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2018/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | #define in insert 7 | #define pi pair 8 | #define pii pair 9 | #define mp make_pair 10 | #define fir first 11 | #define sec second 12 | #define MAXL 20 13 | #define MAXN 50500 14 | 15 | signed main() 16 | { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(NULL); 19 | int n; 20 | cin >> n; 21 | int ans = 0; 22 | for (int i = 0; i < n; i++) 23 | { 24 | int a; 25 | cin >> a; 26 | if (a != 1) 27 | ans++; 28 | } 29 | cout << ans << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Problem Solving/Subregional 2018/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | #define in insert 7 | #define pi pair 8 | #define pii pair 9 | #define mp make_pair 10 | #define fir first 11 | #define sec second 12 | #define MAXL 20 13 | #define MAXN 50500 14 | 15 | signed main() 16 | { 17 | ios_base::sync_with_stdio(false); 18 | cin.tie(NULL); 19 | string a, b; 20 | cin >> a >> b; 21 | int ans = 0, x = 0, y = b.size() - 1; 22 | while (1) 23 | { 24 | if (y == a.size()) 25 | break; 26 | bool flag = true; 27 | for (int i = 0; i < b.size(); i++) 28 | { 29 | if (b[i] == a[x + i]) 30 | { 31 | flag = false; 32 | break; 33 | } 34 | } 35 | if (flag) 36 | ans++; 37 | x++; 38 | y++; 39 | } 40 | cout << ans << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2019/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pb push_back 6 | #define pi pair 7 | #define pii pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 11 11 | 12 | signed main() 13 | { 14 | ios_base::sync_with_stdio(false); 15 | cin.tie(NULL); 16 | int n; 17 | cin >> n; 18 | int best = 0; 19 | vector v(n); 20 | for (int i = 0; i < n; i++) 21 | cin >> v[i], best = max(best, v[i]); 22 | (best == v[0]) ? cout << "S\n" : cout << "N\n"; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2019/h.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define pb push_back 6 | #define pi pair 7 | #define pii pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 11 11 | 12 | signed main() 13 | { 14 | ios_base::sync_with_stdio(false); 15 | cin.tie(NULL); 16 | int v, n; 17 | cin >> v >> n; 18 | double i = 10; 19 | while (i <= 90) 20 | { 21 | double k = (n * v * i) / 100; 22 | int ans = ceil(k); 23 | cout << ans << " "; 24 | i += 10.0; 25 | } 26 | cout << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2019/l.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 11 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | int n; 18 | cin >> n; 19 | int bits = 0; 20 | while (n) 21 | { 22 | if (n & 1) 23 | bits++; 24 | n = n >> 1; 25 | } 26 | int ans = pow(2, bits); 27 | cout << ans << endl; 28 | return 0; 29 | } 30 | // resposta = quantidade de elementos impares em uma linha do triangulo de pascal 31 | // teorema = quantidade de bits setados em n ao quadrado é igual a resposta -------------------------------------------------------------------------------- /Problem Solving/Subregional 2019/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define lli long long int 5 | #define pb push_back 6 | #define MAXN 100001 7 | 8 | lli n, c, t; 9 | lli v[MAXN]; 10 | 11 | bool possible(lli x) 12 | { 13 | lli ans = 1, sum = 0; 14 | 15 | for (lli i = 0; i < n; i++) 16 | { 17 | if (v[i] > x * t) 18 | { 19 | return false; 20 | } 21 | else if (sum + v[i] > x * t) 22 | { 23 | sum = 0; 24 | ans++; 25 | i--; 26 | } 27 | else if (sum + v[i] == x * t) 28 | { 29 | sum = 0; 30 | 31 | if (i != n - 1) 32 | { 33 | ans++; 34 | } 35 | } 36 | else 37 | { 38 | sum += v[i]; 39 | } 40 | } 41 | 42 | return (ans <= c) ? true : false; 43 | } 44 | lli bb() 45 | { 46 | lli i = 0, f = 1000000000000000, m; 47 | 48 | while (i < f) 49 | { 50 | m = (i + f) / 2; 51 | 52 | if (possible(m)) 53 | f = m; 54 | else 55 | i = m + 1; 56 | } 57 | 58 | return i; 59 | } 60 | int main() 61 | { 62 | ios_base::sync_with_stdio(false); 63 | cin.tie(NULL); 64 | 65 | cin >> n >> c >> t; 66 | 67 | for (lli i = 0; i < n; i++) 68 | { 69 | cin >> v[i]; 70 | } 71 | 72 | cout << bb() << endl; 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2020/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define DEBUG 0 17 | #define MAXN 3000001 18 | #define mod 1000000007 19 | 20 | double dp[MAXN]; 21 | double suf[MAXN]; 22 | 23 | signed main() 24 | { 25 | ios_base::sync_with_stdio(false); 26 | cin.tie(NULL); 27 | int n, l, r; 28 | cin >> n >> l >> r; 29 | for (int i = 0; i < MAXN; i++) 30 | { 31 | dp[i] = 1.0; 32 | suf[i] = 0.0; 33 | } 34 | for (int i = n - 1; i >= 0; i--) 35 | { 36 | int len = len = (r - l + 1); 37 | int len2 = (l == 0) ? len - 1 : len; 38 | double prob0 = (l == 0) ? (double)1 / len : 0; 39 | double probother = 1.0 - prob0; 40 | double ev = 1 / probother; 41 | double prob = (double)1 / len2; 42 | dp[i] = ev + ((suf[i + max(l, 1ll)] - suf[i + r + 1]) * prob); 43 | suf[i] = suf[i + 1] + dp[i]; 44 | } 45 | cout << setprecision(15) << dp[0] << endl; 46 | return 0; 47 | } 48 | // pacotes no intervalo [l, r] -------------------------------------------------------------------------------- /Problem Solving/Subregional 2020/g.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define DEBUG 0 17 | #define MAXN 100001 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | int n, k; 25 | cin >> n; 26 | int ans = 100, curr = 100; 27 | for (int i = 0; i < n; i++) 28 | cin >> k, curr += k, ans = max(ans, curr); 29 | cout << ans << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2021/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300005 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n; 24 | cin >> n; 25 | int st = -1, to_go = 0; 26 | vector nxt; 27 | for (int i = 0; i < n; i++) 28 | { 29 | int a, b; 30 | cin >> a >> b; 31 | if (a > to_go) 32 | { 33 | if (nxt.size()) 34 | { 35 | st ^= 1; 36 | to_go += 10; 37 | nxt.clear(); 38 | } 39 | else 40 | { 41 | st = -1; 42 | } 43 | } 44 | if (st == -1 || st == b) 45 | { 46 | st = b; 47 | to_go = a + 10; 48 | } 49 | else 50 | { 51 | nxt.pb(a); 52 | } 53 | } 54 | if (nxt.size()) 55 | { 56 | to_go += 10; 57 | nxt.clear(); 58 | } 59 | cout << to_go << endl; 60 | return 0; 61 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2021/h.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | //#define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300005 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n, k; 24 | cin >> n >> k; 25 | vector v(n); 26 | vector o(n); 27 | for (int i = 0; i < n; i++) 28 | { 29 | cin >> v[i].fir >> v[i].sec; 30 | o[i] = v[i]; 31 | } 32 | sort(v.begin(), v.end()); 33 | bool can = true; 34 | for (int i = 0; i < n; i++) 35 | if (v[i].sec != o[i].sec) 36 | can = false; 37 | (can) ? cout << "Y\n" : cout << "N\n"; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2021/k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | //#define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300005 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int t, d, m; 24 | cin >> t >> d >> m; 25 | if (d < t) 26 | { 27 | cout << "N\n"; 28 | return 0; 29 | } 30 | vector v(m); 31 | for (int i = 0; i < m; i++) 32 | cin >> v[i]; 33 | sort(v.begin(), v.end()); 34 | v.pb(d); 35 | bool at = false; 36 | int sum = 0; 37 | int last = 0; 38 | for (int i = 0; i < v.size(); i++) 39 | { 40 | if (v[i] - last >= t) 41 | at = true; 42 | last = v[i]; 43 | } 44 | (at) ? cout << "Y\n" : cout << "N\n"; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2021/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | //#define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300005 18 | 19 | int id = 1; 20 | vector adj[MAXN]; 21 | bool vis[MAXN]; 22 | queue qq; 23 | 24 | void dfs(int s) 25 | { 26 | qq.push(s); 27 | for (auto const &i : adj[s]) 28 | dfs(i); 29 | } 30 | signed main() 31 | { 32 | ios_base::sync_with_stdio(false); 33 | cin.tie(NULL); 34 | int q; 35 | cin >> q; 36 | vector queries; 37 | while (q--) 38 | { 39 | int a, b; 40 | cin >> a >> b; 41 | b--; 42 | if (a == 1) 43 | adj[b].pb({id++}); 44 | else 45 | queries.pb(b); 46 | } 47 | dfs(0); 48 | for (auto const &i : queries) 49 | { 50 | vis[i] = 1; 51 | while (vis[qq.front()]) 52 | qq.pop(); 53 | cout << qq.front() + 1 << endl; 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2022/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | signed main(){ 7 | int n; 8 | string s; 9 | cin >> n >> s; 10 | int ans = 0; 11 | for (int i = 0; i < n; i++) 12 | { 13 | if (s[i] != 'a') 14 | continue; 15 | if (i - 1 >= 0 && s[i - 1] == 'a') 16 | ans++; 17 | else if (i + 1 < n && s[i + 1] == 'a') 18 | ans++; 19 | } 20 | cout << ans << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2022/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 500001 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | int n, x, y; 25 | cin >> n >> x >> y; 26 | for (int i = 0;; i++) 27 | { 28 | if (y & (1 << i)) 29 | { 30 | cout << n - 1 - i << endl; 31 | break; 32 | } 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2022/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | int s[1000005]; 7 | 8 | signed main(){ 9 | int n; 10 | cin >> n; 11 | int ans = n; 12 | for (int i = 0; i < n; i++) 13 | { 14 | int x; 15 | cin >> x; 16 | if (s[x + 1] > 0) 17 | { 18 | s[x + 1]--; 19 | ans--; 20 | } 21 | s[x]++; 22 | } 23 | cout << ans << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2022/i.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 1010 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | bool ans = 1; 25 | for (int i = 0; i < 8; i++) 26 | { 27 | int a; 28 | cin >> a; 29 | if (a == 9) 30 | ans = 0; 31 | } 32 | (ans) ? cout << "S\n" : cout << "F\n"; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2022/j.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | 6 | int dp[14]; 7 | 8 | void init() 9 | { 10 | for (int i = 1; i <= 13; i++) 11 | { 12 | dp[i] = 4; 13 | } 14 | } 15 | int convert(int x) 16 | { 17 | if (x > 10) 18 | return 10; 19 | return x; 20 | } 21 | signed main() 22 | { 23 | init(); 24 | int n; 25 | cin >> n; 26 | int a1 = 0, a2 = 0, sum_j = 0, sum_m = 0; 27 | cin >> a1 >> a2; 28 | dp[a1]--; 29 | dp[a2]--; 30 | sum_j = convert(a1) + convert(a2); 31 | cin >> a1 >> a2; 32 | dp[a1]--; 33 | dp[a2]--; 34 | sum_m = convert(a1) + convert(a2); 35 | for (int i = 1; i <= n; i++) 36 | { 37 | int k; 38 | cin >> k; 39 | dp[k]--; 40 | sum_j += convert(k); 41 | sum_m += convert(k); 42 | } 43 | int ans = -1; 44 | for (int i = 1; i <= 13; i++) 45 | { 46 | if (dp[i] > 0) 47 | { 48 | if (sum_m + convert(i) == 23 || (sum_j + convert(i) > 23 && sum_m + convert(i) <= 23)) 49 | { 50 | ans = i; 51 | break; 52 | } 53 | } 54 | } 55 | cout << ans << endl; 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2023/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n, h; 8 | cin >> n >> h; 9 | int cnt = 0; 10 | for (int i = 0; i < n; i++) 11 | { 12 | int ch; 13 | cin >> ch; 14 | cnt += (ch <= h); 15 | } 16 | cout << cnt << endl; 17 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2023/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define endl '\n' 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 305 12 | #define mod 998244353 13 | 14 | signed main() 15 | { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | int n; 19 | cin >> n; 20 | vector v(n); 21 | vector pos(n + 1, 0); 22 | for (int i = 0; i < n; i++) 23 | { 24 | cin >> v[i]; 25 | pos[v[i]] = i + 1; 26 | } 27 | for (int i = 0; i < n; i++) 28 | { 29 | v[i] = pos[i + 1]; 30 | } 31 | int need = 0, i = 0; 32 | while (i < n) 33 | { 34 | need++; 35 | while (i + 1 < n && v[i + 1] > v[i]) 36 | i++; 37 | i++; 38 | } 39 | int pot = 0; 40 | while ((1 << pot) < need) 41 | pot++; 42 | cout << pot << endl; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2023/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define MAXN 100005 9 | 10 | int n; 11 | vector adj[MAXN]; 12 | int v[MAXN]; 13 | int ans[MAXN]; 14 | vector q; 15 | vector op; 16 | 17 | void dfs(int s, int p) 18 | { 19 | auto it = lower_bound(q.begin(), q.end(), v[s]); 20 | int pos = -1, prv; 21 | bool fl = 0; 22 | if (it == q.end()) 23 | { 24 | q.pb(v[s]); 25 | pos = q.size() - 1; 26 | fl = 1; 27 | } 28 | else 29 | { 30 | pos = it - q.begin(); 31 | prv = q[pos]; 32 | q[pos] = v[s]; 33 | } 34 | ans[s] = q.size(); 35 | for (auto const &i : adj[s]) 36 | { 37 | if (i == p) 38 | continue; 39 | dfs(i, s); 40 | } 41 | if (fl) 42 | { 43 | q.pop_back(); 44 | } 45 | else 46 | { 47 | q[pos] = prv; 48 | } 49 | } 50 | signed main() 51 | { 52 | cin >> n; 53 | for (int i = 1; i < n; i++) 54 | { 55 | int x; 56 | cin >> x; 57 | x--; 58 | adj[x].pb(i); 59 | adj[i].pb(x); 60 | } 61 | for (int i = 0; i < n; i++) 62 | { 63 | cin >> v[i]; 64 | } 65 | dfs(0, -1); 66 | for (int i = 1; i < n; i++) 67 | cout << ans[i] << " "; 68 | cout << endl; 69 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2023/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define MAXN 1000005 9 | 10 | int n, k; 11 | int v[MAXN]; 12 | int nxt[MAXN]; 13 | int freq[MAXN]; 14 | 15 | int sum(int x) 16 | { 17 | int ans = 0; 18 | while (x > 0) 19 | { 20 | ans += (x % 10); 21 | x /= 10; 22 | } 23 | return ans; 24 | } 25 | signed main() 26 | { 27 | ios_base::sync_with_stdio(false); 28 | cin.tie(NULL); 29 | cin >> n >> k; 30 | for (int i = 0; i < n; i++) 31 | { 32 | cin >> v[i]; 33 | freq[v[i]]++; 34 | } 35 | for (int i = 0; i < MAXN; i++) 36 | { 37 | nxt[i] = i - sum(i); 38 | } 39 | int ans = 0; 40 | for (int i = MAXN - 1; i > 0; i--) 41 | { 42 | k -= freq[i]; 43 | freq[nxt[i]] += freq[i]; 44 | if (k <= 0) 45 | { 46 | ans = i; 47 | break; 48 | } 49 | } 50 | cout << sum(ans) << endl; 51 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2023/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define pb push_back 7 | 8 | signed main() 9 | { 10 | int d, r, c; 11 | cin >> d >> c >> r; 12 | vector v(c); 13 | for (int i = 0; i < c; i++) 14 | cin >> v[i]; 15 | vector v2(r); 16 | for (int i = 0; i < r; i++) 17 | cin >> v2[i]; 18 | int a = 0, b = 0, ans = 0; 19 | while (a < c) 20 | { 21 | if (d >= v[a]) 22 | { 23 | ans++; 24 | d -= v[a]; 25 | a++; 26 | } 27 | else if (b < r) 28 | { 29 | ans++; 30 | d += v2[b]; 31 | b++; 32 | } 33 | else 34 | { 35 | break; 36 | } 37 | } 38 | ans += (r - b); 39 | cout << ans << endl; 40 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2023/i.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define pb push_back 7 | 8 | signed main() 9 | { 10 | int n; 11 | cin >> n; 12 | vector dp(2, 0); 13 | dp[0] = 1; 14 | int ans = 0; 15 | int sum = 0; 16 | for (int i = 0; i < n; i++) 17 | { 18 | int x; 19 | cin >> x; 20 | sum = (sum + x) % 2; 21 | int ot = sum ^ 1; 22 | ans += dp[ot]; 23 | dp[sum]++; 24 | } 25 | cout << ans << endl; 26 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2023/l.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define pb push_back 7 | 8 | signed main() 9 | { 10 | string s; 11 | int k; 12 | cin >> s >> k; 13 | vector adj(k); 14 | for (int i = 0; i < s.size(); i++) 15 | { 16 | adj[i % k].pb(s[i]); 17 | } 18 | for (int i = 0; i < k; i++) 19 | { 20 | sort(adj[i].begin(), adj[i].end()); 21 | } 22 | vector ptr(k, 0); 23 | for (int i = 0; i < s.size(); i++) 24 | { 25 | cout << adj[i % k][ptr[i % k]]; 26 | ptr[i % k]++; 27 | } 28 | cout << endl; 29 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2024/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | 7 | signed main() 8 | { 9 | int n, k; 10 | cin >> n >> k; 11 | int ans = (k - (n - 1)) / n; 12 | cout << ans << endl; 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2024/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define pi pair 7 | #define fir first 8 | #define sec second 9 | 10 | signed main() 11 | { 12 | ios::sync_with_stdio(false); 13 | cin.tie(0); 14 | int n; 15 | 16 | cin >> n; 17 | 18 | int m[n][n]; 19 | 20 | for (int i = 0; i < n; i++) 21 | { 22 | for (int j = 0; j < n; j++) 23 | { 24 | cin >> m[i][j]; 25 | } 26 | } 27 | 28 | int resp = 0, v = m[0][0]; 29 | 30 | if (m[0][n - 1] < v) 31 | { 32 | v = m[0][n - 1]; 33 | resp = 1; 34 | } 35 | 36 | if (m[n - 1][n - 1] < v) 37 | { 38 | v = m[n - 1][n - 1]; 39 | resp = 2; 40 | } 41 | 42 | if (m[n - 1][0] < v) 43 | { 44 | v = m[n - 1][0]; 45 | resp = 3; 46 | } 47 | 48 | cout << resp << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2024/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | #define pi pair 7 | #define fir first 8 | #define sec second 9 | 10 | pi sum(pi a, pi b) 11 | { 12 | pi ans; 13 | ans.sec = (a.sec == b.sec) ? a.sec : (a.sec * b.sec); 14 | ans.fir = a.fir * (ans.sec / a.sec) + b.fir * (ans.sec / b.sec); 15 | return ans; 16 | } 17 | signed main() 18 | { 19 | ios::sync_with_stdio(false); 20 | cin.tie(0); 21 | int n; 22 | cin >> n; 23 | vector a(n + 1); 24 | a[0] = {1, 1}; 25 | for (int i = 1; i <= n; i++) 26 | { 27 | a[i] = sum(pi(1, 1), a[i - 1]); 28 | swap(a[i].fir, a[i].sec); 29 | int g = __gcd(a[i].fir, a[i].sec); 30 | a[i].fir /= g; 31 | a[i].sec /= g; 32 | } 33 | cout << a[n].fir << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Problem Solving/Subregional 2024/l.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define int long long int 6 | 7 | signed main() 8 | { 9 | ios::sync_with_stdio(false); 10 | cin.tie(0); 11 | int n; 12 | cin >> n; 13 | vector cnt(31, 0); 14 | for (int i = 0; i < n; i++) 15 | { 16 | int x; 17 | cin >> x; 18 | for (int b = 0; b <= 30; b++) 19 | { 20 | if (x & (1ll << b)) 21 | cnt[b]++; 22 | } 23 | } 24 | vector ans(n, 0); 25 | for (int b = 0; b <= 30; b++) 26 | { 27 | for (int i = 0; i < cnt[b]; i++) 28 | ans[i] |= (1ll << b); 29 | } 30 | for (int i = 0; i < n; i++) 31 | cout << ans[i] << " \n"[i == (n - 1)]; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Problem Solving/TAP 2023/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define endl '\n' 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 1000005 12 | #define mod 998244353 13 | 14 | signed main() 15 | { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | int n, m; 19 | cin >> n >> m; 20 | vector a(n); 21 | priority_queue pq; 22 | for (int i = 0; i < n; i++) 23 | { 24 | int x; 25 | cin >> x; 26 | pq.push({x, i}); 27 | } 28 | while (m--) 29 | { 30 | int x; 31 | cin >> x; 32 | while (pq.top().fir >= x) 33 | { 34 | auto [y, i] = pq.top(); 35 | pq.pop(); 36 | y %= x; 37 | pq.push({y, i}); 38 | } 39 | } 40 | vector ans(n); 41 | while (!pq.empty()) 42 | { 43 | ans[pq.top().sec] = pq.top().fir; 44 | pq.pop(); 45 | } 46 | for (auto const &i : ans) 47 | cout << i << " "; 48 | cout << endl; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Problem Solving/TAP 2023/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | int n; 9 | cin >> n; 10 | int ans = 0; 11 | for (int i = 1; i <= n; i++) 12 | { 13 | int curr = 0; 14 | string x; 15 | cin >> x; 16 | for (int j = 1; j <= n; j++) 17 | { 18 | if (x[j - 1] == 'B') 19 | { 20 | ans += curr / 2; 21 | curr = 0; 22 | } 23 | else 24 | { 25 | curr++; 26 | } 27 | } 28 | if (curr > 0) 29 | { 30 | ans += curr / 2; 31 | } 32 | } 33 | cout << ans << "\n"; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Problem Solving/TAP 2023/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define endl '\n' 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 1000005 12 | #define mod 998244353 13 | 14 | signed main() 15 | { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | int n; 19 | cin >> n; 20 | vector a(n); 21 | for (int i = 0; i < n; i++) 22 | cin >> a[i]; 23 | vector b(n); 24 | for (int i = 0; i < n; i++) 25 | cin >> b[i]; 26 | sort(a.begin(), a.end()); 27 | sort(b.rbegin(), b.rend()); 28 | vector c; 29 | for (int i = 0; i < n; i++) 30 | c.pb(a[i] + b[i]); 31 | int ans = *max_element(c.begin(), c.end()) - *min_element(c.begin(), c.end()); 32 | cout << ans << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Problem Solving/TAP 2023/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define endl '\n' 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 1000005 12 | #define mod 998244353 13 | 14 | signed main() 15 | { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | int k; 19 | cin >> k; 20 | vector c(k); 21 | for (int i = 0; i < k; i++) 22 | cin >> c[i]; 23 | vector p(k); 24 | for (int i = 0; i < k; i++) 25 | cin >> p[i]; 26 | auto can = [&](int mid) 27 | { 28 | vector stk; 29 | for (int i = k - 1; i >= 0; i--) 30 | { 31 | stk.pb(c[i] * mid); 32 | int x = p[i]; 33 | while (stk.size() > 0 && x > 0) 34 | { 35 | int to_sub = min(x, stk.back()); 36 | x -= to_sub; 37 | stk.back() -= to_sub; 38 | if (stk.back() == 0) 39 | stk.pop_back(); 40 | } 41 | } 42 | return (stk.size() == 0); 43 | }; 44 | int l = 0, r = 1000000005; 45 | while (l < r) 46 | { 47 | int mid = (l + r + 1) >> 1; 48 | (can(mid)) ? l = mid : r = mid - 1; 49 | } 50 | cout << l << endl; 51 | return 0; 52 | } 53 | // um contest precisa do seguinte: 54 | // ter c[i] problemas com o nivel i 55 | 56 | // existem p[i] questoes com o nivel i 57 | // se uma questão tem o nivel i, então 58 | 59 | // se um problema tem um nivel sendo i 60 | // ele pode ser usado no contest com um nivel j 61 | // se j >= i -------------------------------------------------------------------------------- /Problem Solving/TAP 2023/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | double dist(double x1, double y1, double x2, double y2) 5 | { 6 | return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 7 | } 8 | signed main() 9 | { 10 | ios_base::sync_with_stdio(false); 11 | cin.tie(NULL); 12 | int n; 13 | cin >> n; 14 | int w, l, tx, ty; 15 | cin >> w >> l >> tx >> ty; 16 | vector distA; 17 | vector distR; 18 | for (int i = 0; i < n; i++) 19 | { 20 | int x, y; 21 | cin >> x >> y; 22 | double a = dist(x, y, tx, ty); 23 | distA.push_back(a); 24 | // cout<> x >> y; 31 | double b = dist(x, y, tx, ty); 32 | distR.push_back(b); 33 | // cout<= distR[0]) 48 | { 49 | break; 50 | } 51 | ans++; 52 | } 53 | } 54 | else 55 | { 56 | cout << "R" 57 | << " "; 58 | for (int i = 0; i < n; i++) 59 | { 60 | if (distR[i] >= distA[0]) 61 | { 62 | break; 63 | } 64 | ans++; 65 | } 66 | } 67 | 68 | cout << ans << "\n"; 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Problem Solving/TAP 2023/h.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define endl '\n' 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 1005 12 | #define mod 998244353 13 | 14 | int n; 15 | int v[MAXN][MAXN]; 16 | pi pos[1000005]; 17 | int dp[MAXN][MAXN][2]; 18 | int dp_col[MAXN]; 19 | int dp_row[MAXN]; 20 | 21 | signed main() 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | cin >> n; 26 | for (int i = 0; i < n; i++) 27 | { 28 | for (int j = 0; j < n; j++) 29 | { 30 | cin >> v[i][j]; 31 | pos[v[i][j]] = {i, j}; 32 | } 33 | } 34 | for (int i = 0; i < n; i++) 35 | { 36 | dp_col[i] = -1e18; 37 | dp_row[i] = -1e18; 38 | } 39 | int sz = n * n; 40 | int ans = 0; 41 | for (int i = 1; i <= sz; i++) 42 | { 43 | int x = pos[i].fir; 44 | int y = pos[i].sec; 45 | for (int d = 0; d < 2; d++) 46 | { 47 | if (d == 0) 48 | dp[x][y][d] = max(0ll, dp_col[y]); 49 | else 50 | dp[x][y][d] = max(0ll, dp_row[x]); 51 | ans = max(ans, dp[x][y][d]); 52 | } 53 | for (int d = 0; d < 2; d++) 54 | { 55 | if (d == 0) 56 | dp_row[x] = max(dp_row[x], dp[x][y][d] + 1); 57 | else 58 | dp_col[y] = max(dp_col[y], dp[x][y][d] + 1); 59 | } 60 | } 61 | ans++; 62 | cout << ans << endl; 63 | return 0; 64 | } -------------------------------------------------------------------------------- /Problem Solving/TAP 2023/l.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long int 5 | #define endl '\n' 6 | #define pb push_back 7 | #define pi pair 8 | #define pii pair 9 | #define fir first 10 | #define sec second 11 | #define MAXN 1000005 12 | #define mod 998244353 13 | 14 | signed main() 15 | { 16 | ios_base::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | int a, b, c, d; 19 | cin >> a >> b >> c >> d; 20 | a += c; 21 | b += d; 22 | if (a > b) 23 | cout << "A\n"; 24 | else if (a < b) 25 | cout << "P\n"; 26 | else 27 | cout << "D\n"; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Problem Solving/UFPE try-outs 2020/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define mp make_pair 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 2000001 13 | #define MAXL 20 14 | #define mod 998244353 15 | 16 | signed main() 17 | { 18 | ios_base::sync_with_stdio(false); 19 | cin.tie(NULL); 20 | int q; 21 | cin >> q; 22 | while (q--) 23 | { 24 | int n; 25 | cin >> n; 26 | if (n >= 8128) 27 | cout << "8128\n"; 28 | else if (n >= 496) 29 | cout << "496\n"; 30 | else if (n >= 28) 31 | cout << "28\n"; 32 | else if (n >= 6) 33 | cout << "6\n"; 34 | else 35 | cout << "-1\n"; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Problem Solving/UFPE try-outs 2020/g.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define mp make_pair 8 | #define pi pair 9 | #define pii pair 10 | #define fir first 11 | #define sec second 12 | #define MAXN 131072 13 | #define MAXL 20 14 | #define mod 998244353 15 | 16 | vector handles; 17 | map m; 18 | map m2; 19 | 20 | signed main() 21 | { 22 | int c, p, s; 23 | cin >> c >> p >> s; 24 | handles.resize(c); 25 | for (int i = 0; i < c; i++) 26 | cin >> handles[i], m.insert(mp(handles[i], 0)); 27 | for (int i = 0; i < p; i++) 28 | { 29 | string a; 30 | int b; 31 | cin >> a >> b; 32 | m2.insert(mp(a, b)); 33 | } 34 | for (int i = 0; i < s; i++) 35 | { 36 | string a, b, c; 37 | cin >> a >> b >> c; 38 | if (c == "AC") 39 | m[a] += m2[b]; 40 | } 41 | for (auto const &i : handles) 42 | cout << i << " " << m[i] << endl; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Problem Solving/UFPE try-outs 2020/h.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 100001 11 | #define mod 1000000007 12 | 13 | int tempo[MAXN]; 14 | int t[MAXN]; 15 | bool visited[MAXN]; 16 | vector adj[MAXN]; 17 | 18 | void bfs(int s) 19 | { 20 | queue q; 21 | q.push(s); 22 | while (!q.empty()) 23 | { 24 | int v = q.front(); 25 | for (auto const &u : adj[v]) 26 | { 27 | if (tempo[u.fir] > tempo[v] + u.sec + t[u.fir]) 28 | { 29 | tempo[u.fir] = tempo[v] + u.sec + t[u.fir]; 30 | q.push(u.fir); 31 | } 32 | } 33 | q.pop(); 34 | } 35 | } 36 | signed main() 37 | { 38 | ios_base::sync_with_stdio(false); 39 | cin.tie(NULL); 40 | int n, m, k; 41 | cin >> n >> m >> k; 42 | for (int i = 0; i < m; i++) 43 | { 44 | int a, b, c; 45 | cin >> a >> b >> c; 46 | a--, b--; 47 | adj[a].pb({b, c}); 48 | adj[b].pb({a, c}); 49 | } 50 | for (int i = 0; i < n; i++) 51 | { 52 | cin >> t[i]; 53 | tempo[i] = 1e15; 54 | } 55 | for (int i = 0; i < k; i++) 56 | { 57 | int a; 58 | cin >> a; 59 | a--; 60 | tempo[a] = t[a]; 61 | } 62 | for (int i = 0; i < n; i++) 63 | if (!visited[i] && tempo[i] != 1e15) 64 | bfs(i); 65 | for (int i = 0; i < n; i++) 66 | cout << tempo[i] << endl; 67 | } -------------------------------------------------------------------------------- /Problem Solving/UFPE try-outs 2020/k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define PI acos(-1) 5 | #define int long long int 6 | #define pb push_back 7 | #define pi pair 8 | #define fir first 9 | #define sec second 10 | #define MAXN 300001 11 | #define mod 1000000007 12 | 13 | signed main() 14 | { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | int n, h; 18 | cin >> n >> h; 19 | int val[n][h]; 20 | int dp[n][h]; 21 | for (int i = 0; i < n; i++) 22 | for (int j = 0; j < h; j++) 23 | cin >> val[i][j]; 24 | int ans = 0; 25 | for (int j = 0; j < h; j++) 26 | { 27 | for (int i = 0; i < n; i++) 28 | { 29 | dp[i][j] = val[i][j]; 30 | if (i - 1 >= 0 && j - 1 >= 0) 31 | dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + val[i][j]); 32 | if (i + 1 < n && j - 1 >= 0) 33 | dp[i][j] = max(dp[i][j], dp[i + 1][j - 1] + val[i][j]); 34 | if (j - 1 >= 0) 35 | dp[i][j] = max(dp[i][j], dp[i][j - 1] + val[i][j]); 36 | ans = max(ans, dp[i][j]); 37 | } 38 | } 39 | cout << ans << endl; 40 | } -------------------------------------------------------------------------------- /Problem Solving/UFPE try-outs 2021/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 5001 17 | #define mod 1000000007 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | int n, k, q; 24 | cin >> n >> k >> q; 25 | string s, t; 26 | cin >> s >> t; 27 | reverse(s.begin(), s.end()); 28 | for (int i = 0; i < q; i++) 29 | { 30 | string tt = ""; 31 | for (int j = 0; j < n; j++) 32 | { 33 | deque curr; 34 | curr.pb(t[j]); 35 | int l = (j - 1 + n) % n; 36 | int r = (j + 1) % n; 37 | for (int f = 0; f < k; f++) 38 | { 39 | curr.push_front(t[l]); 40 | curr.push_back(t[r]); 41 | l = (l - 1 + n) % n; 42 | r = (r + 1) % n; 43 | } 44 | reverse(curr.begin(), curr.end()); 45 | int bit = 0, num = 0; 46 | for (auto const &i : curr) 47 | { 48 | if (i == '1') 49 | num += (1LL << bit); 50 | bit++; 51 | } 52 | tt += s[num]; 53 | } 54 | t = tt; 55 | } 56 | cout << t << endl; 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Problem Solving/UFPE try-outs 2021/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 5001 17 | #define mod 1000000007 18 | 19 | int query(int l, int r) 20 | { 21 | cout << l + 1 << " " << r + 1 << endl; 22 | fflush(stdout); 23 | int ans; 24 | cin >> ans; 25 | return ans; 26 | } 27 | signed main() 28 | { 29 | ios_base::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | int n; 32 | cin >> n; 33 | int l = 0, r = n - 1; 34 | while (l < r) 35 | { 36 | int mid = (l + r) >> 1; 37 | (query(0, mid)) ? r = mid : l = mid + 1; 38 | } 39 | cout << "! " << l + 1 << endl; 40 | fflush(stdout); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /STL/ordered_set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | #define int long long int 8 | #define pb push_back 9 | #define pi pair 10 | #define pii pair 11 | #define fir first 12 | #define sec second 13 | #define DEBUG false 14 | #define MAXN 200002 15 | 16 | template // template do ordered set 17 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 18 | 19 | signed main() 20 | { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(NULL); 23 | ordered_set s; // ordered_set 24 | s.insert(1); 25 | s.insert(1); 26 | s.insert(2); 27 | s.insert(4); 28 | s.insert(3); 29 | for (auto const &i : s) // nao adiciona elementos repetidos, que nem o set normal 30 | cout << i << " "; 31 | cout << endl; 32 | cout << *(s.find_by_order(0)) << endl; // iterator do elemento 0 33 | cout << *(s.find_by_order(1)) << endl; // iterator do elemento 1 34 | cout << s.order_of_key(4) << endl; // quantidade de elementos que são menores do que 4 35 | cout << s.order_of_key(6) << endl; // quantidade de elementos que são menores do que 4 36 | } 37 | // find_by_order : O(log n), retorna (um iterator) qual o k-ésimo elemento do set 38 | // order_of_key: O(log n), retorna qual a quantidade de elementos menores do que x no set 39 | -------------------------------------------------------------------------------- /Strings/z-function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 2001 18 | #define mod 1000000007 19 | 20 | vector z_function(string &s) 21 | { 22 | int n = s.size(); 23 | vector z(n); 24 | z[0] = n; 25 | for (int i = 1, l = 0, r = 0; i < n; i++) 26 | { 27 | if (i <= r) 28 | z[i] = min(r - i + 1, z[i - l]); 29 | while (i + z[i] < n && s[z[i]] == s[i + z[i]]) 30 | z[i]++; 31 | if (i + z[i] - 1 > r) 32 | l = i, r = i + z[i] - 1; 33 | } 34 | return z; 35 | } 36 | signed main() 37 | { 38 | ios_base::sync_with_stdio(false); 39 | cin.tie(NULL); 40 | string s; 41 | cin >> s; 42 | vector z = z_function(s); 43 | } 44 | // z-function 45 | // calcula para cada i: 46 | // z[i] = o tamanho de lcp(s, s.substr(i, n - i)) 47 | // lcp -> longest comom prefix -------------------------------------------------------------------------------- /Structures/fenwick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define int long long int 11 | #define endl '\n' 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 200005 18 | #define mod 998244353 19 | 20 | struct fenw 21 | { 22 | int n; 23 | vector bit; 24 | fenw() {} 25 | fenw(int sz) 26 | { 27 | n = sz; 28 | bit.assign(sz + 1, 0); 29 | } 30 | int qry(int r) // query de prefixo a[0] + a[1] + ... a[r] 31 | { 32 | int ret = 0; 33 | for (int i = r + 1; i > 0; i -= i & -i) 34 | ret += bit[i]; 35 | return ret; 36 | } 37 | void upd(int r, int x) // a[r] += x 38 | { 39 | for (int i = r + 1; i <= n; i += i & -i) 40 | bit[i] += x; 41 | } 42 | int bs(int x) // retorna o maior indice i (i < n) tal que: qry(i) < x 43 | { 44 | int i = 0, k = 0; 45 | while (1 << (k + 1) <= n) 46 | k++; 47 | while (k >= 0) 48 | { 49 | int nxt_i = i + (1 << k); 50 | if (nxt_i <= n && bit[nxt_i] < x) 51 | { 52 | i = nxt_i; 53 | x -= bit[i]; 54 | } 55 | k--; 56 | } 57 | return i - 1; 58 | } 59 | }; 60 | -------------------------------------------------------------------------------- /Structures/fenwick3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | //#define int long long int 11 | #define pb push_back 12 | #define pi pair 13 | #define pii pair 14 | #define fir first 15 | #define sec second 16 | #define MAXN 200005 17 | #define mod 1000000007 18 | 19 | int v[MAXN]; 20 | 21 | namespace bit 22 | { 23 | ordered_set bit[MAXN]; 24 | 25 | int query(int r, int a, int b) 26 | { 27 | int ret = 0, curr = r; 28 | for (; r >= 0; r = (r & (r + 1)) - 1) 29 | ret += (bit[r].order_of_key(b + 1) - bit[r].order_of_key(a)); 30 | return ret; 31 | } 32 | void add(int idx, int delta) 33 | { 34 | for (; idx < MAXN; idx = idx | (idx + 1)) 35 | bit[idx].insert(delta); 36 | } 37 | void rem(int idx, int delta) 38 | { 39 | for (; idx < MAXN; idx = idx | (idx + 1)) 40 | bit[idx].erase(delta); 41 | } 42 | } 43 | signed main() 44 | { 45 | ios_base::sync_with_stdio(false); 46 | cin.tie(NULL); 47 | return 0; 48 | } 49 | // ideia da merge sort tree na bit (fica mais rapido) 50 | // so fazer uma bit de ordered set ou vector(se nao tiver update) 51 | // add -> adiciona o numero delta na posição idx 52 | // rem -> remove o numero delta na posição idx 53 | // query -> retorna o numero de elementos tal que posição <= r && (a <= num <= b) 54 | -------------------------------------------------------------------------------- /Structures/sparsetable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | 7 | template 8 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 9 | 10 | #define PI acos(-1) 11 | #define pb push_back 12 | #define int long long int 13 | #define pi pair 14 | #define pii pair> 15 | #define fir first 16 | #define sec second 17 | #define DEBUG 0 18 | #define MAXN 10005 19 | #define mod 1000000007 20 | 21 | struct rmq 22 | { 23 | bool is_min; 24 | vector> st; 25 | vector log; 26 | 27 | int f(int a, int b) { return (is_min) ? min(a, b) : max(a, b); } 28 | int qry(int l, int r) { return f(st[l][log[r - l + 1]], st[r - (1 << log[r - l + 1]) + 1][log[r - l + 1]]); } 29 | rmq(vector &v, bool flag) 30 | { 31 | is_min = flag; 32 | int n = v.size(); 33 | log.resize(n + 1); 34 | log[1] = 0; 35 | for (int i = 2; i <= n; i++) 36 | log[i] = log[i / 2] + 1; 37 | int m = log[n] + 2; 38 | st.assign(n + 1, vector(m, 0)); 39 | for (int i = 0; i < n; i++) 40 | st[i][0] = v[i]; 41 | for (int j = 1; j < m; j++) 42 | { 43 | for (int i = 0; i + (1 << j) <= n; i++) 44 | st[i][j] = f(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); 45 | } 46 | } 47 | }; 48 | signed main() 49 | { 50 | ios_base::sync_with_stdio(false); 51 | cin.tie(NULL); 52 | } 53 | -------------------------------------------------------------------------------- /Theorems and Formulas/binomial_theorem.md: -------------------------------------------------------------------------------- 1 | ## Binomial Theorem 2 | 3 | ### Theorem 4 | 5 | $$ 6 | (x + y)^n = \sum_{k = 0}^{n} {n \choose k} x^{n - k}y^{k} 7 | $$ 8 | 9 | in addition, we have: 10 | 11 | $$ 12 | (x - y)^n = \sum_{k = 0}^{n} (-1)^k {n \choose k} x^{n - k}y^{k} 13 | $$ 14 | 15 | $$ 16 | (1 + x)^n = \sum_{k = 0}^{n} {n \choose k} x^{k} 17 | $$ 18 | 19 | ### Cool Problem 20 | 21 | [Fibonacci Fever](https://codeforces.com/gym/104412/problem/F) 22 | 23 | Given n and k you're asked to compute (mod $10^9 + 7$): 24 | 25 | $$ 26 | \sum\_{i = 1}^{n}f_i^k 27 | $$ 28 | 29 | where $f_n$ is the n-th fibonacci number. 30 | 31 | Recall that: 32 | 33 | $$ 34 | f_n = \frac{1}{\sqrt{5}} \left(\frac{1+\sqrt{5}}{2}\right)^n - \frac{1}{\sqrt{5}} \left(\frac{1-\sqrt{5}}{2}\right)^n 35 | $$ 36 | 37 | This is something like that: 38 | 39 | $$ 40 | f_n = ca^n - cb^n 41 | $$ 42 | 43 | In the end, we end up with something like: 44 | 45 | $$ 46 | c^k (\sum_{j = 0}^{k} (-1)^{j} {k \choose j} \sum_{i = 1}^{n} (a^{k - j}b^{j})^{i} ) 47 | $$ 48 | 49 | PS: To handle $\sqrt{5}$ mod $10^9 + 7$, we need to store each number in the form $x = a + b\sqrt{5}$ 50 | 51 | [Code](https://github.com/jonh14lk/Competitive_Programming/blob/master/Math/binomial_theorem.cpp) 52 | -------------------------------------------------------------------------------- /Theorems and Formulas/chicken_mcnugget.md: -------------------------------------------------------------------------------- 1 | ## Chicken McNugget Theorem 2 | 3 | For any two coprime numbers (n > 0, m > 0), the greatest integer that cannot be written in the form: 4 | 5 | an + bm, (a >= 0, b >= 0) 6 | 7 | is (n \* m) - n - m 8 | 9 | ## Consequence of the theorem 10 | 11 | That there are exactly ((n - 1) \* (m - 1)) / 2 positive integers which cannot be expressed in the form an + bm, (a >= 0, b >= 0) 12 | 13 | ## Generalization 14 | 15 | If n and m are not coprime, so all numbers that are not multiples of gcd(n, m) cannot be expressed in the form an + bm, (a >= 0, b >= 0) 16 | 17 | in addition, you can consider n = (n / gcd(n, m)) and m = (m / gcd(n, m)), to find how many multiples of gcd(n, m) cannot be expressed, or to find the greatest multiple of gcd(n, m) that cannot be expressed 18 | 19 | ## Considering a > 0, b > 0 20 | 21 | Considering (n > 0, m > 0), n and m are coprime: 22 | 23 | let y = ((n \* m) + min(n, m)) - 1 24 | 25 | The number of positive integers which cannot be expressed increases by (y / n) 26 | 27 | The number of positive integers which cannot be expressed increases by (y / m) 28 | 29 | you must not count the multiples of (n \* m) more than once, just decrease number of positive integers which cannot be expressed by (y / (n \* m)) 30 | 31 | ## Problems 32 | 33 | - [Forming Compounds](https://codeforces.com/group/XrhoJtxCjm/contest/422716/problem/I) 34 | -------------------------------------------------------------------------------- /Theorems and Formulas/manhattan_and_chebyshev.md: -------------------------------------------------------------------------------- 1 | ## Manhattan and Chebyshev distances equivalences 2 | 3 | It is well known that given points (x, y) and you need to calculate the Manhattan distances between them, instead of using: 4 | |x1-x2|+|y1-y2| 5 | 6 | you can first convert all points (x, y) into (x+y, x-y) (rotate 45 degrees) and the distances will become max(|x1-x2|, |y1-y2|) (also known as Chebyshev distance). 7 | 8 | ### Problems: 9 | 10 | [Manhattan Triangle](https://codeforces.com/contest/1979/problem/E) 11 | [https://atcoder.jp/contests/abc366/tasks/abc366_e](https://atcoder.jp/contests/abc366/tasks/abc366_e) 12 | -------------------------------------------------------------------------------- /Utils/__int128.cpp: -------------------------------------------------------------------------------- 1 | // https://codeforces.com/blog/entry/75044 2 | // functions to print and read a __int128 in c++ 3 | __int128 read() 4 | { 5 | __int128 x = 0, f = 1; 6 | char ch = getchar(); 7 | while (ch < '0' || ch > '9') 8 | { 9 | if (ch == '-') 10 | f = -1; 11 | ch = getchar(); 12 | } 13 | while (ch >= '0' && ch <= '9') 14 | { 15 | x = x * 10 + ch - '0'; 16 | ch = getchar(); 17 | } 18 | return x * f; 19 | } 20 | void print(__int128 x) 21 | { 22 | if (x < 0) 23 | { 24 | cout << "-"; 25 | x = -x; 26 | } 27 | if (x > 9) 28 | print(x / 10); 29 | char at = (x % 10) + '0'; 30 | cout << at; 31 | } 32 | -------------------------------------------------------------------------------- /Utils/execution_time.cpp: -------------------------------------------------------------------------------- 1 | // https://codeforces.com/blog/entry/57647 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | using namespace __gnu_pbds; 7 | 8 | template 9 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 10 | 11 | #define int long long int 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300005 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | // just call clock in the beginning 25 | clock_t time = clock(); 26 | 27 | // ... 28 | // ... 29 | // some code here ... 30 | // ... 31 | // ... 32 | 33 | // execution time: 34 | cout << setprecision(3) << fixed << (double)(clock() - time) / CLOCKS_PER_SEC << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Utils/rand.cpp: -------------------------------------------------------------------------------- 1 | // https://codeforces.com/blog/entry/61587 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | using namespace __gnu_pbds; 7 | 8 | template 9 | using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 10 | 11 | #define int long long int 12 | #define pb push_back 13 | #define pi pair 14 | #define pii pair 15 | #define fir first 16 | #define sec second 17 | #define MAXN 300005 18 | #define mod 1000000007 19 | 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(false); 23 | cin.tie(NULL); 24 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 25 | 26 | int n = 10; 27 | vector v(n); 28 | for (int i = 0; i < n; i++) 29 | v[i] = i; 30 | shuffle(v.begin(), v.end(), rng); // random shuffle 31 | 32 | int x = rng() % 10; // better than rand() 33 | cout << x << endl; 34 | 35 | // random integer values on the closed interval [a, b] 36 | int y = uniform_int_distribution(3, 77)(rng); 37 | cout << y << endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Utils/runner.py: -------------------------------------------------------------------------------- 1 | # This script does the following: 2 | # 1 - Generate a random testcase 3 | # 2 - Run some "naive" code with this input 4 | # 3 - Run your code with this input 5 | # 4 - Compare the outputs 6 | import os 7 | import subprocess 8 | 9 | naive = "brute.cpp" # path to naive code 10 | code = "d.cpp" # path to your code 11 | generator = "g.cpp" # path to test generator 12 | 13 | def compile_codes(): 14 | os.system('g++ ' + generator + ' -o generator -O2') 15 | os.system('g++ ' + naive + ' -o naive -O2') 16 | os.system('g++ ' + code + ' -o code -O2') 17 | 18 | def generate_case(): 19 | os.system('./generator > in'); 20 | 21 | def get_naive_output(): 22 | output = os.popen('./naive