├── .gitignore ├── Graphs ├── Maximun_Clique │ └── max_clique.cpp ├── SCC │ ├── 2sac_rec_ans.cpp │ ├── 2sat.cpp │ └── scc_tarjan.cpp ├── Tree │ ├── AHU_ISO │ │ └── ahu_tree_isomorphism.cpp │ ├── HLD │ │ ├── hld_lazy.cpp │ │ └── hld_seg_iterative_upd_pt.cpp │ ├── LCA │ │ ├── intersection_2_paths.cpp │ │ ├── lca.cpp │ │ └── o1_lca.cpp │ ├── centroidDecomposition │ │ ├── cd_count_nodes_up_to_dist_k_from_every.cpp │ │ ├── cd_minedge_dist_soma_k.cpp │ │ └── centroidDecomposition.cpp │ └── dominator.cpp ├── eulerian_cycle.cpp ├── k-colorable │ └── k-colorable.cpp ├── maxFlow │ ├── Simple_O(e*maxflow).cpp │ ├── dinic │ │ ├── dinicMarcos.cpp │ │ ├── dinic_int.cpp │ │ └── dinic_rec_ans.cpp │ ├── max_bip_unit.cpp │ └── minCostMaxFlow │ │ ├── hung.cpp │ │ └── mcmf.cpp └── tools │ ├── MeuListaPais_to_pairEdges.cpp │ ├── isconected.cpp │ └── listaPai_to_pairOfEdges_csacad.cpp ├── dataStructures ├── orderedSet │ ├── cs_68_c.cpp │ ├── ordered_set.cpp │ └── ordered_set_template.cpp ├── segTree │ ├── classic+simple │ │ ├── classic_normal.cpp │ │ └── lazy_simpler_faster.cpp │ ├── different_segs │ │ ├── non_commutative_seg_iter.cpp │ │ └── seg_seto_range_cor.cpp │ ├── lazys │ │ ├── lazy_mx_id_mn_id_get_first.cpp │ │ └── lazy_sum_mx_mn_get_first.cpp │ ├── old_lazies │ │ ├── lazy_flip_e_euler_tour.cpp │ │ └── lazy_seg_generic.cpp │ └── seg_iterative_node.cpp ├── sparse_table.cpp └── sqrt │ └── count_range_subarray.cpp ├── excessive ├── DSU │ └── dsu.cpp ├── Graph │ ├── diameter_tree.cpp │ ├── dijkistra.cpp │ ├── find_cycle_directed.cpp │ ├── max_dist_tree.cpp │ └── topo_sort.cpp ├── LIS │ └── lis.cpp ├── MO │ └── mo.cpp ├── geometry │ ├── forca_mod.cpp │ ├── pt.cpp │ └── rot.cpp ├── meet_int_the_middle │ ├── 2icmc_seletiva_2017_k.cpp │ ├── eventDatesCF.cpp │ └── icmc_seletiva_2017_k.cpp ├── misc │ ├── debug.cpp │ ├── list_diagonals.cpp │ ├── rmaxid.cpp │ └── tirar_contidos.cpp ├── not_mine │ ├── arborescence.cpp │ └── fft.cpp └── numberTheory │ ├── fat_invfat.cpp │ ├── fubini.cpp │ ├── mi.cpp │ ├── nck_values_bigger_than_mod.cpp │ ├── paschoal_triangle.cpp │ ├── phi.cpp │ └── polynomials.cpp ├── geometry ├── 3d │ ├── dist_ponto_plano3d.cpp │ └── intersect_line_place.cpp ├── Convex_Hull │ ├── CHT │ │ ├── cht.cpp │ │ ├── cht_deque.cpp │ │ ├── cht_wihtout_less.cpp │ │ └── pareto_hull_2d.cpp │ ├── convex_hull.cpp │ ├── dobra_poligono.cpp │ └── ponto_dentro_logn.cpp ├── Lines_and_Segments │ ├── dist_poligons_segment.cpp │ ├── dist_semiLines.cpp │ ├── half_plane_intersection.cpp │ ├── line_intersection_pair_frac.cpp │ ├── line_intersection_pt_double.cpp │ └── segment_intersection.cpp ├── Sort_by_Angle │ ├── sort_pt_by_angle.cpp │ └── sweep_angular_e_reta_tang_pt_curcunf.cpp ├── circles │ ├── circle_nest_tree.cpp │ ├── intersection_circ-circ_and_circ-line.cpp │ └── line_tangent_2_circles.cpp ├── pt_template.cpp └── rot_and_forca_mod.cpp ├── levar_icpc ├── bash_script_run_infinite ├── compare_script.cpp ├── flags_compilacao ├── semantics_to_remember └── template.cpp ├── math ├── Polynomials │ ├── fft_benq.cpp │ ├── fwht.cpp │ ├── lagrange_interpolation.cpp │ ├── poly.cpp │ └── poly_complex_mod1e9.cpp ├── gauss_elimination │ ├── gauss_mod.cpp │ └── gauss_xor.cpp └── numberTheory │ ├── Linear_recurrence │ ├── berlekamp_massey.cpp │ ├── rec_linear.cpp │ ├── rec_linear_mod.cpp │ └── rec_linear_struct.cpp │ ├── gcd_ext │ ├── gcd_ext_crt_solution.cpp │ └── gcd_ext_diophantine_qnt_sol.cpp │ └── pollard_rho │ ├── FindDivisors.cpp │ ├── pollardRhoFactorization.cpp │ └── pollardRhoFfao.cpp ├── strings ├── aho-corasick.cpp ├── hash.cpp ├── pref_automaton.cpp ├── preffix_func_kmp.cpp ├── suffix_array_lcp.cpp └── z_func.cpp └── tecnico ├── Run_multiple ├── compare.cpp ├── input_validator.cpp ├── run_local.cpp └── run_multiple_asserts.cpp ├── big_num.cpp ├── fraction.cpp ├── int128.cpp ├── mod.cpp ├── old ├── Useful_Conversions │ └── makeStringArray.cpp └── mybashes │ ├── ca │ ├── cacomp │ ├── cajava │ ├── cola │ ├── compara_random.cpp │ ├── crjava │ ├── molde.cpp │ └── show_compila ├── optimizations ├── array_de_vector_otimizado_ae.cpp ├── comprime_string.cpp ├── mymap.cpp └── pragma_otimizations │ └── fast_short_loop.cpp ├── random.cpp ├── setup_maratona ├── cr ├── crf └── temp.cpp └── time.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | **/.DS_Store 2 | -------------------------------------------------------------------------------- /Graphs/Maximun_Clique/max_clique.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i>tam[0]; 39 | if( (mask&gg) != (mask^(1<>=tam[0]; 58 | ans = max(ans,b_clique[0][mask]+b_clique[1][mask2]); 59 | } 60 | 61 | return ans; 62 | } 63 | 64 | //--------------------------------------------------- 65 | 66 | //solves problem https://codeforces.com/contest/1105/problem/E 67 | 68 | int n, m; 69 | map mp; 70 | 71 | char str[110]; 72 | 73 | int getid(string s){ 74 | if(mp.count(s)) return mp[s]; 75 | int id = mp.size(); 76 | mp[s] = id; 77 | return id; 78 | } 79 | 80 | int main(){ 81 | cin >> n >> m; 82 | 83 | set s; 84 | 85 | fr(i,n){ 86 | int t; 87 | scanf("%d", &t); 88 | if(t==1){ 89 | for(auto &u : s){ 90 | for(auto &v : s){ 91 | g[u] |= (1ll< 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i g[N]; 17 | 18 | int tempo, ncomp, ss; 19 | int disc[N], st[N], low[N], comp[N]; 20 | 21 | int dfs(int no){ 22 | disc[no] = low[no] = ++tempo; 23 | st[ss++] = no; 24 | for(auto it : g[no]){ 25 | if(!disc[it]) low[no] = min(low[no],dfs(it)); 26 | else if(!comp[it]) low[no] = min(low[no],disc[it]); 27 | //pode ser low[it], mas melhor manter disc[it] q tmb funciona e eh indicado pelo editorial 28 | } 29 | if(low[no]==disc[no]){ 30 | comp[no] = ++ncomp; 31 | while(st[ss-1]!=no){ 32 | comp[st[--ss]] = comp[no]; 33 | } 34 | ss--; 35 | } 36 | return low[no]; 37 | } 38 | 39 | /* 40 | Poe condição (u or v) no 2sat 41 | se du==1, u é 2*u+1 (impar) e significa 42 | que é u normal (verdadeiro), do contrario é not u 43 | */ 44 | void poe(int u, int v, int du, int dv){ 45 | u = 2*u+du; 46 | v = 2*v+dv; 47 | g[u^1].push_back(v); 48 | g[v^1].push_back(u); 49 | } 50 | 51 | bool is_solvable(int n){ 52 | assert(n> ntam >> r >> n; 81 | vector> vp; 82 | fr(icur,n){ 83 | int i, j; cin >> i >> j; i--,j--; 84 | int iant = -1; 85 | for(auto &[a,b] : vp){ 86 | iant++; 87 | if(a==i and abs(b-j)<=r){ 88 | poe(icur,iant,0,0); 89 | } 90 | if(b==j and abs(i-a)<=r){ 91 | poe(icur,iant,1,1); 92 | } 93 | } 94 | vp.emplace_back(i,j); 95 | } 96 | if(is_solvable(n)) cout << "YES\n"; 97 | else cout << "NO\n"; 98 | } 99 | -------------------------------------------------------------------------------- /Graphs/SCC/scc_tarjan.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i g[N]; 16 | 17 | vector comp_to_nos[N]; 18 | 19 | int tempo; 20 | int disc[N]; //primeiro tempo em que nó foi descoberto 21 | int low[N]; //minimo entre disc[no] e low[v] dos vizinhos 22 | 23 | //stack e size of stack 24 | int st[N], ss; 25 | 26 | //componente do noh i (0 se ainda nao pertence a componente) 27 | //comp[no] : [1,ncomp] 28 | int comp[N], ncomp; 29 | 30 | int dfs(int no){ 31 | disc[no] = low[no] = ++tempo; 32 | st[ss++] = no; 33 | for(auto it : g[no]){ 34 | if(!disc[it]) low[no] = min(low[no],dfs(it)); 35 | else if(!comp[it]) low[no] = min(low[no],disc[it]); 36 | } 37 | if(low[no]==disc[no]){ 38 | comp[no] = ++ncomp; 39 | while(st[ss-1]!=no) comp[st[--ss]] = comp[no]; 40 | ss--; 41 | } 42 | return low[no]; 43 | } 44 | 45 | //--------------- 46 | 47 | vector ans; 48 | int vis[N]; 49 | namespace sol{ 50 | void dfs(int cp){ 51 | if(vis[cp]) return; 52 | vis[cp] = 1; 53 | for(auto &no : comp_to_nos[cp]){ 54 | for(auto &it : g[no]){ 55 | if(comp[it]!=comp[no]) dfs(comp[it]); 56 | } 57 | } 58 | ans.push_back(cp); 59 | } 60 | }; 61 | 62 | int main(){ 63 | ios::sync_with_stdio(0); cin.tie(0); 64 | int n, m; cin >> n >> m; 65 | fr(i,m){ 66 | int a, b; cin >> a >> b; 67 | g[a].push_back(b); 68 | } 69 | 70 | //rodar tarjan e definir comps de cada no 71 | fr(i,n) if(!disc[i]) dfs(i); 72 | 73 | //comp_to_nos, nem sempre necessario, comp 1-indexado 74 | fr(i,n) comp_to_nos[comp[i]].push_back(i); 75 | 76 | //----------------------- 77 | 78 | for(int cp = 1; cp<=ncomp; cp++){ 79 | sol::dfs(cp); 80 | } 81 | reverse(all(ans)); 82 | cout << sz(ans) << "\n"; 83 | for(auto &cp : ans){ 84 | cout << sz(comp_to_nos[cp]); 85 | for(auto &no : comp_to_nos[cp]) cout << " " << no; 86 | cout << "\n"; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Graphs/Tree/AHU_ISO/ahu_tree_isomorphism.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i> g; 21 | vector tam; 22 | 23 | void dfs(int no, int pai){ 24 | tam[no] = 1; 25 | for(auto &it : g[no]){ 26 | if(it==pai) continue; 27 | dfs(it,no); 28 | tam[no]+=tam[it]; 29 | } 30 | } 31 | 32 | vector jump(int no, int p){ 33 | for(auto &it : g[no]){ 34 | if(it==p) continue; 35 | if(2*tam[it]>n) return jump(it,no); 36 | if(2*tam[it]==n) return {no,it}; 37 | } 38 | return {no}; 39 | } 40 | 41 | vector get_cent(vector> &go){ 42 | g = go; 43 | n = g.size(); 44 | assert(n); 45 | tam = vector(n,0); 46 | dfs(0,-1); 47 | return jump(0,-1); 48 | } 49 | }; 50 | 51 | namespace niso{ 52 | 53 | int n; 54 | int nl; 55 | vector> g[2]; 56 | vector lvl[2]; 57 | vector> ltn[2]; 58 | vector hash[2]; 59 | vector> hashv[2]; 60 | int cent[2]; 61 | 62 | void dfs(int cor, int no, int p,int l){ 63 | lvl[cor][no] = l; 64 | nl = max(l,nl); 65 | for(auto &it : g[cor][no]){ 66 | if(it==p) continue; 67 | dfs(cor,it,no,l+1); 68 | } 69 | } 70 | 71 | int corg; 72 | 73 | struct cmp{ 74 | bool operator()(int a, int b){ 75 | return hashv[corg][a]()); 85 | hashv[cor].assign(n,vector()); 86 | } 87 | 88 | fr(cor,2){ 89 | dfs(cor,cent[cor],-1,0); 90 | } 91 | fr(cor,2){ 92 | fr(i,n) ltn[cor][lvl[cor][i]].eb(i); 93 | } 94 | for(int l = nl; l>=0;l--){ 95 | fr(cor,2){ 96 | for(auto &no : ltn[cor][l]){ 97 | for(auto &it : g[cor][no]) if(lvl[cor][it]==l+1) hashv[cor][no].eb(hash[cor][it]); 98 | sort(all(hashv[cor][no])); 99 | } 100 | corg = cor; 101 | sort(all(ltn[cor][l]),cmp()); 102 | int h = 0; 103 | 104 | for(int i = 1; i>,2> &go){ 118 | fr(cor,2){ 119 | g[cor] = go[cor]; 120 | } 121 | if(g[0].size()!=g[1].size()) return 0; 122 | n = g[0].size(); 123 | vector vc[2]; 124 | 125 | fr(cor,2) vc[cor] = ncent::get_cent(g[cor]); 126 | cent[1] = vc[1][0]; 127 | for(auto &c1 : vc[0]){ 128 | cent[0] = c1; 129 | if(check()) return 1; 130 | } 131 | return 0; 132 | } 133 | } 134 | 135 | int main(){ 136 | 137 | int t; 138 | cin >> t; 139 | 140 | fr(tt,t){ 141 | int n; 142 | cin >> n; 143 | 144 | array>,2> g; 145 | 146 | fr(cor,2) g[cor].resize(n); 147 | 148 | fr(cor,2) fr(i,n-1){ 149 | int u, v; 150 | scanf("%d%d", &u, &v); 151 | u--,v--; 152 | g[cor][u].eb(v); 153 | g[cor][v].eb(u); 154 | } 155 | 156 | if(niso::iso(g)) puts("YES"); 157 | else puts("NO"); 158 | } 159 | 160 | } 161 | -------------------------------------------------------------------------------- /Graphs/Tree/LCA/intersection_2_paths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i *g; 21 | int pai[N], dist[N]; //pai do nó i (raiz = -1) 22 | int st[N][25]; //sparse table - st[i][j] = pai 2^j niveis acima do nó i 23 | 24 | void dfs(int no, int from, int dac){ 25 | dist[no] = dac; 26 | for(auto it : g[no]){ 27 | if(it==from) continue; 28 | pai[it] = no; 29 | dfs(it,no,dac+1); 30 | } 31 | } 32 | 33 | void make(vector _g[N], int _n, int root){ 34 | g = _g; 35 | n = _n; 36 | 37 | pai[root] = -1; 38 | dfs(root,-1,0); 39 | 40 | nlog = 1; 41 | while((1<=0; i--){ 57 | if((1<=0; i--){ 71 | if(st[p][i]!=st[q][i]){ 72 | p = st[p][i]; 73 | q = st[q][i]; 74 | } 75 | } 76 | return pai[q]; 77 | } 78 | 79 | int get_dist(int u, int v){ 80 | return dist[u]+dist[v]-2*dist[lca(u,v)]; 81 | } 82 | 83 | }; //end lcas 84 | 85 | //solves https://judge.yosupo.jp/problem/lca 86 | vector g[N]; 87 | 88 | int no_in; //true if intersections is empty 89 | pair in_path(int a, int b, int c, int d){ 90 | using lcas::lca; 91 | using lcas::dist; 92 | no_in = 0; 93 | int lab = lca(a,b); 94 | int lcd = lca(c,d); 95 | 96 | //lab must be further away from the root 97 | if(dist[lcd]>dist[lab]) swap(a,c), swap(b,d), swap(lab,lcd); 98 | 99 | int lac = lca(a,c); 100 | int lad = lca(a,d); 101 | int lbc = lca(b,c); 102 | int lbd = lca(b,d); 103 | 104 | int la = lac; 105 | if(dist[lad]>dist[lac]) la = lad; 106 | 107 | int lb = lbc; 108 | if(dist[lbd]>dist[lbc]) lb = lbd; 109 | 110 | if(dist[la]> n >> q; 117 | fr(i,n-1){ 118 | int a, b; cin >> a >> b; a--,b--; 119 | g[a].push_back(b); 120 | g[b].push_back(a); 121 | } 122 | 123 | lcas::make(g,n,0); 124 | 125 | fr(qq,q){ 126 | int a, b, u, v; cin >> a >> b >> u >> v; a--,b--,u--,v--; 127 | 128 | int i, j; tie(i,j) = in_path(a,b,u,v); 129 | int ans = 0; 130 | if(!no_in) ans = lcas::get_dist(i,j)+1; 131 | cout << ans << "\n"; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /Graphs/Tree/LCA/lca.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i *g; 18 | int pai[N], dist[N]; //pai do nó i (raiz = -1) 19 | int st[N][25]; //sparse table - st[i][j] = pai 2^j niveis acima do nó i 20 | 21 | void dfs(int no, int from, int dac){ 22 | dist[no] = dac; 23 | for(auto it : g[no]){ 24 | if(it==from) continue; 25 | pai[it] = no; 26 | dfs(it,no,dac+1); 27 | } 28 | } 29 | 30 | void make(vector _g[N], int _n, int root){ 31 | g = _g; 32 | n = _n; 33 | 34 | pai[root] = -1; 35 | dfs(root,-1,0); 36 | 37 | nlog = 1; 38 | while((1<=0; i--){ 54 | if((1<=0; i--){ 68 | if(st[p][i]!=st[q][i]){ 69 | p = st[p][i]; 70 | q = st[q][i]; 71 | } 72 | } 73 | return pai[q]; 74 | } 75 | 76 | int get_dist(int u, int v){ 77 | return dist[u]+dist[v]-2*dist[lca(u,v)]; 78 | } 79 | 80 | }; //end lcas 81 | 82 | //solves https://judge.yosupo.jp/problem/lca 83 | vector g[N]; 84 | 85 | int main(){ 86 | ios::sync_with_stdio(0); cin.tie(0); 87 | int n, q; cin >> n >> q; 88 | fr(i,n-1){ 89 | int id_pai; cin >> id_pai; 90 | g[id_pai].push_back(i+1); 91 | g[i+1].push_back(id_pai); 92 | } 93 | 94 | lcas::make(g,n,0); 95 | 96 | fr(qq,q){ 97 | int a, b; cin >> a >> b; 98 | cout << lcas::lca(a,b) << "\n"; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Graphs/Tree/LCA/o1_lca.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 16 | 17 | //LEMBRAR DE POR O MAKE DEPOIS DE MONTAR A ARVORE 18 | const int N = 5e5+10; 19 | 20 | int log_floor(int n){ 21 | return 31-__builtin_clz(n); 22 | } 23 | 24 | pair oper(pair &a, pair &b){ 25 | if(a.fi!=b.fi){ 26 | if(a.fi>> mat; 41 | sparse_table(){} 42 | sparse_table(vector> v){ 43 | n = sz(v); 44 | exp2 = log_floor(n)+1; 45 | mat.resize(exp2); 46 | mat[0].resize(n); 47 | fr(i,n) mat[0][i] = v[i]; 48 | for(int k = 1; k qry(int l, int r){ 57 | assert(l<=r and l>=0 and r *g; 66 | int pai[N], dist[N]; //pai do nó i (raiz = -1) 67 | vector> euler; 68 | int topos[N]; 69 | sparse_table se; 70 | 71 | void dfs(int no, int from, int dac){ 72 | dist[no] = dac; 73 | for(auto it : g[no]){ 74 | if(it==from) continue; 75 | pai[it] = no; 76 | dfs(it,no,dac+1); 77 | } 78 | } 79 | 80 | void dfs(int no, int from){ 81 | topos[no] = sz(euler); 82 | euler.emplace_back(dist[no],no); 83 | for(auto &it : g[no]){ 84 | if(it==from) continue; 85 | dfs(it,no); 86 | euler.emplace_back(dist[no],no); 87 | } 88 | } 89 | 90 | void make(vector _g[N], int _n, int root){ 91 | g = _g; 92 | n = _n; 93 | 94 | pai[root] = -1; 95 | dfs(root,-1,0); 96 | euler.reserve(3*n); 97 | dfs(root,-1); 98 | 99 | se = sparse_table(euler); 100 | } 101 | 102 | int lca(int p, int q){ 103 | ll l = topos[p]; 104 | ll r = topos[q]; 105 | if(r g[N]; 117 | 118 | int main(){ 119 | ios::sync_with_stdio(0); cin.tie(0); 120 | int n, q; cin >> n >> q; 121 | fr(i,n-1){ 122 | int id_pai; cin >> id_pai; 123 | g[id_pai].push_back(i+1); 124 | g[i+1].push_back(id_pai); 125 | } 126 | 127 | lcas::make(g,n,0); 128 | 129 | fr(qq,q){ 130 | int a, b; cin >> a >> b; 131 | cout << lcas::lca(a,b) << "\n"; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /Graphs/Tree/centroidDecomposition/cd_count_nodes_up_to_dist_k_from_every.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i freq_cur(idu_cur+1), pref_cur(idu_cur+1); 108 | for(auto &par : v){ 109 | int d, it; tie(d,it) = par; 110 | freq_cur[d]++; 111 | } 112 | fr(i,idu_cur+1){ 113 | pref_cur[i] = freq_cur[i]; 114 | if(i) pref_cur[i]+=pref_cur[i-1]; 115 | } 116 | for(auto &par : v){ 117 | int d, it; tie(d,it) = par; 118 | int idc = min(k-d,idu); 119 | int idc_cur = min(idc,idu_cur); 120 | ans[it]+= pref_dist[idc] - pref_cur[idc_cur]; 121 | } 122 | } 123 | fr(i,idu+1){ 124 | pref_dist[i] = freq_dist[i] = 0; 125 | } 126 | 127 | } 128 | fr(i,n) printf("%lld\n", ans[i]+1); 129 | } 130 | -------------------------------------------------------------------------------- /Graphs/Tree/centroidDecomposition/cd_minedge_dist_soma_k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i g[N]; 10 | 11 | //solves https://codeforces.com/gym/102920/problem/F 12 | 13 | /* 14 | Dado grafo g global, tam n e root 15 | retorna vetor de tamanho n idom[no] = imediate dominator de no wrt to root 16 | -1 se nao tem dominator (root e nos nao alcançaveis sao -1) 17 | 0-indexado msm 18 | */ 19 | vector dominators(int n, int root) 20 | { 21 | vector ord, rank(n, n), prev(n, n), anc(n, n), idom(n, n), semi(n), low(n); 22 | vector> g_rev(n), dom(n); 23 | function dfs = [&](int u) 24 | { 25 | rank[u] = ord.size(); 26 | ord.emplace_back(u); 27 | for (int v : g[u]) 28 | { 29 | g_rev[v].emplace_back(u); 30 | if (rank[v] < n) continue; 31 | prev[v] = u; 32 | dfs(v); 33 | } 34 | }; 35 | dfs(root); 36 | for (int i = 0; i < n; ++i) semi[i] = low[i] = i; 37 | function eval = [&](int v) 38 | { 39 | if (anc[v] < n && anc[anc[v]] < n) 40 | { 41 | int x = eval(anc[v]); 42 | if (rank[semi[low[v]]] > rank[semi[x]]) low[v] = x; 43 | anc[v] = anc[anc[v]]; 44 | } 45 | return low[v]; 46 | }; 47 | for (int i = (int) ord.size() - 1; i >= 1; --i) 48 | { 49 | int w = ord[i]; 50 | for (int v : g_rev[w]) 51 | { 52 | int u = eval(v); 53 | if (rank[semi[w]] > rank[semi[u]]) semi[w] = semi[u]; 54 | } 55 | dom[semi[w]].emplace_back(w); 56 | anc[w] = prev[w]; 57 | for (int v : dom[prev[w]]) 58 | { 59 | int u = eval(v); 60 | idom[v] = (rank[prev[w]] > rank[semi[u]] ? u : prev[w]); 61 | } 62 | dom[prev[w]].clear(); 63 | } 64 | for (int i = 1; i < sz(ord); ++i) 65 | { 66 | int w = ord[i]; 67 | if (idom[w] != semi[w]) idom[w] = idom[idom[w]]; 68 | } 69 | for (int u = 0; u < n; ++u) 70 | if (idom[u] >= n) idom[u] = -1; 71 | return idom; 72 | } 73 | 74 | int main(){ 75 | ios::sync_with_stdio(0); cin.tie(0); 76 | int n, n_esp, n_edge; cin >> n >> n_esp >> n_edge; 77 | fr(i,n_edge){ 78 | int a, b; cin >> a >> b; 79 | g[a].push_back(b); 80 | } 81 | fr(i,n_esp) g[0].push_back(i+1); 82 | int ans = 0; 83 | vector idom = dominators(n+1,0); 84 | for(int i = 1; i<=n; i++) if(idom[i]==0) ans++; 85 | cout << ans << "\n"; 86 | } 87 | -------------------------------------------------------------------------------- /Graphs/eulerian_cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i> g[N]; 11 | 12 | namespace eulerpath_space{ 13 | vector path; 14 | vector idit, used_edge; 15 | 16 | void dfs(int no){ 17 | while(1){ 18 | int &id = idit[no]; 19 | while(id0) return 0; 43 | 44 | path.clear(); 45 | idit = vector(n); 46 | used_edge = vector(m); 47 | dfs(inic); 48 | if(sz(path)==m+1) return 1; 49 | return 0; 50 | } 51 | }; //end hpath_space 52 | 53 | int main(){ 54 | ios::sync_with_stdio(0); cin.tie(0); 55 | int n, m; cin >> n >> m; 56 | 57 | fr(i,m){ 58 | int a, b; cin >> a >> b; a--,b--; 59 | g[a].emplace_back(b,i); 60 | g[b].emplace_back(a,i); 61 | } 62 | 63 | if(eulerpath_space::has_cycle(n,m)){ 64 | vector ans = eulerpath_space::path; 65 | fr(i,sz(ans)){ 66 | if(i) cout << " "; 67 | cout << ans[i]+1; 68 | } 69 | cout << "\n"; 70 | } else { 71 | cout << "IMPOSSIBLE\n"; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Graphs/maxFlow/Simple_O(e*maxflow).cpp: -------------------------------------------------------------------------------- 1 | //O(maxflow*E) 2 | 3 | //O QUE PRECISA LER/mudar: 4 | 5 | //--------------------------------- 6 | // maximo numero de vertices 7 | const int N = 160; 8 | //E = N^2 - > ok 9 | 10 | int g[N][N]; 11 | //--------------------------------- 12 | 13 | // flow network 14 | int flow[N][N]; 15 | // visited array 16 | bool vis[N]; 17 | 18 | int dfs(int cur, int sink, int minc){ 19 | vis[cur] = 1; 20 | 21 | if(cur==sink) return minc; 22 | 23 | fr(i,N){ 24 | int curflow = g[cur][i]-flow[cur][i]; 25 | if(!vis[i] and curflow>0){ 26 | if(int sent = dfs(i,sink,min(minc,curflow))){ 27 | flow[cur][i]+=sent; 28 | flow[i][cur]-=sent; 29 | return sent; 30 | } 31 | } 32 | } 33 | 34 | //nao achou sink 35 | return 0; 36 | } 37 | 38 | int maxflow(int src, int sink){ 39 | int ans = 0; 40 | 41 | memset(flow,0,sizeof(flow)); 42 | 43 | while(int sent = dfs(src,sink,INT_MAX)){ 44 | ans+=sent; 45 | memset(vis,0,sizeof(vis)); 46 | } 47 | 48 | return ans; 49 | } 50 | 51 | int main(){ 52 | //memset(g,0,sizeof(g)); 53 | //g[a][b] = 1 54 | //cout << maxFlow(s,t) << endl; 55 | 56 | return 0; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /Graphs/maxFlow/dinic/dinic_int.cpp: -------------------------------------------------------------------------------- 1 | const int MAXV = 200; // maximo numero de vertices 2 | const int FINF = INT_MAX; // infinite flow 3 | 4 | struct Edge { 5 | int to; 6 | int cap; 7 | Edge(int t, int c) 8 | { 9 | to = t; 10 | cap = c; 11 | } 12 | }; 13 | 14 | vector adj[MAXV]; 15 | vector edge; 16 | int ptr[MAXV], dinic_dist[MAXV]; 17 | 18 | // Inserts an edge u->v with capacity c 19 | inline void add_edge(int u, int v, int c) 20 | { 21 | adj[u].push_back(edge.size()); 22 | edge.push_back(Edge(v, c)); 23 | adj[v].push_back(edge.size()); 24 | edge.push_back(Edge(u, 0)); // modify to Edge(u,c) if graph is non-directed 25 | } 26 | 27 | bool dinic_bfs(int _s, int _t) 28 | { 29 | memset(dinic_dist, -1, sizeof(dinic_dist)); 30 | dinic_dist[_s] = 0; 31 | queue q; 32 | q.push(_s); 33 | while (!q.empty() && dinic_dist[_t] == -1) { 34 | int v = q.front(); 35 | q.pop(); 36 | for (size_t a = 0; a < adj[v].size(); ++a) { 37 | int ind = adj[v][a]; 38 | int nxt = edge[ind].to; 39 | if (dinic_dist[nxt] == -1 && edge[ind].cap) { 40 | dinic_dist[nxt] = dinic_dist[v] + 1; 41 | q.push(nxt); 42 | } 43 | } 44 | } 45 | return dinic_dist[_t] != -1; 46 | } 47 | 48 | int dinic_dfs(int v, int _t, int flow) 49 | { 50 | if (v == _t) 51 | return flow; 52 | for (int& a = ptr[v]; a < (int)adj[v].size(); ++a) { 53 | int ind = adj[v][a]; 54 | int nxt = edge[ind].to; 55 | if (dinic_dist[nxt] == dinic_dist[v] + 1 && edge[ind].cap) { 56 | int got = dinic_dfs(nxt, _t, min(flow, edge[ind].cap)); 57 | if (got) { 58 | edge[ind].cap -= got; 59 | edge[ind ^ 1].cap += got; 60 | return got; 61 | } 62 | } 63 | } 64 | return 0; 65 | } 66 | 67 | int dinic(int _s, int _t) 68 | { 69 | int ret = 0, got; 70 | while (dinic_bfs(_s, _t)) { 71 | memset(ptr, 0, sizeof(ptr)); 72 | while ((got = dinic_dfs(_s, _t, FINF))) 73 | ret += got; 74 | } 75 | return ret; 76 | } 77 | 78 | // Clears dinic structure 79 | inline void dinic_clear() 80 | { 81 | for (int a = 0; a < MAXV; ++a) 82 | adj[a].clear(); 83 | edge.clear(); 84 | } 85 | -------------------------------------------------------------------------------- /Graphs/maxFlow/max_bip_unit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | #define fr(i,n) for(int i = 0; i supoe fluxo unitario!!! 8 | 9 | //M é o numero de candidatos e N o numero de vagas - por isso bipartido! 10 | int n, m; // em problemas de grafo quadrado lembrar de fazer m = n 11 | #define N 3030 12 | #define M 11100 13 | 14 | //g[i] = j (indice no : i (0 a M-1) "source" e it : j "dest" (0 a N-1) 15 | vector g[M]; //pra onde aplicam cada um dos M cand 16 | bool vis[N]; 17 | int match[N]; // vaga i (das N) vai para candidato match[i] (de 0 a M-1) (ou -1 se ngm vai para vaga) 18 | 19 | bool match_vertex(int u) { 20 | for (int v : g[u]) 21 | if (!vis[v]) { 22 | vis[v] = 1; 23 | if (match[v] < 0 or match_vertex(match[v])) { 24 | match[v] = u; 25 | return 1; 26 | } 27 | } 28 | return 0; 29 | } 30 | 31 | int max_match() { 32 | fr(i,n) match[i] = -1; 33 | int result = 0; 34 | fr(u,m){ 35 | fr(i,n) vis[i] = 0; 36 | if (match_vertex(u)) result++; 37 | } 38 | return result; 39 | } 40 | 41 | //-------------------------------------------------------------------------- 42 | 43 | //DESCONSIDERAR 44 | //variveis especificas desse problema 45 | ll x[3030], y[3030]; 46 | set < ll > nums; 47 | map < ll, int > comp; 48 | 49 | //solves problem E from: http://codeforces.com/gym/101485/standings/friends/true 50 | 51 | //-------------------------------------------------------------------------- 52 | 53 | int main() { 54 | scanf("%d", &n); 55 | for (int i = 0; i < n; i++) { 56 | scanf("%lld %lld", &x[i], &y[i]); 57 | nums.insert(x[i] + y[i]); 58 | nums.insert(x[i] - y[i]); 59 | nums.insert(x[i] * y[i]); 60 | } 61 | 62 | int ind = 3030; 63 | for (auto v : nums) 64 | comp[v] = ind++; 65 | m = ind; 66 | 67 | for (int i = 0; i < n; i++) { 68 | g[comp[x[i] + y[i]]].push_back(i); 69 | g[comp[x[i] - y[i]]].push_back(i); 70 | g[comp[x[i] * y[i]]].push_back(i); 71 | } 72 | 73 | int ans = max_match(); 74 | if (ans != n) printf("impossible\n"); 75 | else { 76 | for (int i = 0; i < n; i++) { 77 | if (match[i] == comp[x[i] + y[i]]) printf("%lld + %lld = %lld\n", x[i], y[i], x[i] + y[i]); 78 | else if (match[i] == comp[x[i] - y[i]]) printf("%lld - %lld = %lld\n", x[i], y[i], x[i] - y[i]); 79 | else printf("%lld * %lld = %lld\n", x[i], y[i], x[i] * y[i]); 80 | } 81 | } 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Graphs/tools/MeuListaPais_to_pairEdges.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define FILE_IN freopen("kotlin.in", "r", stdin); 5 | #define FILE_OUT freopen("kotlin.out", "w", stdout); 6 | 7 | #define fr(i,n) for(int i=0;i pii; 21 | typedef pair pll; 22 | 23 | #define PI acos(-1) 24 | ll MOD = 1e9+7; 25 | 26 | //LONG_LONG_MAX 27 | //-DBL_MAX 28 | 29 | bool debug = 1; 30 | #define printa(a) cout << #a << " = " << (a) << endl 31 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 32 | #define soprin(a) if(debug) cout << (a) 33 | #define ppal(a) if(debug) cout << #a << endl 34 | #define prinsep if(debug) cout << "------" << endl 35 | #define cendl if(debug) cout << endl 36 | #define prinpar(p) if(debug) cout << #p << ".fi=" << p.fi << " " << #p << ".se=" << p.se << endl 37 | #define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;} 38 | 39 | #define mt make_tuple 40 | #define get(a,id) get(a) 41 | #define t3ll tuple 42 | 43 | const int N = 1e5+10; 44 | 45 | int main(){ 46 | //FILE_IN FILE_OUT 47 | int n; 48 | cin >> n; 49 | n = 98; 50 | vector v; 51 | frr(i,2,n){ 52 | int x; 53 | cin >> x; 54 | v.pb(pii(i,x)); 55 | } 56 | 57 | cout << n << " " << v.size() << endl; 58 | for(auto it : v){ 59 | cout << it.fi << " " << it.se << endl; 60 | } 61 | 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /Graphs/tools/isconected.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define FILE_IN freopen("kotlin.in", "r", stdin); 5 | #define FILE_OUT freopen("kotlin.out", "w", stdout); 6 | 7 | #define fr(i,n) for(int i=0;i pii; 21 | typedef pair pll; 22 | 23 | #define PI acos(-1) 24 | ll MOD = 1e9+7; 25 | 26 | //LONG_LONG_MAX 27 | //-DBL_MAX 28 | 29 | bool debug = 1; 30 | #define printa(a) cout << #a << " = " << (a) << endl 31 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 32 | #define soprin(a) if(debug) cout << (a) 33 | #define ppal(a) if(debug) cout << #a << endl 34 | #define prinsep if(debug) cout << "------" << endl 35 | #define cendl if(debug) cout << endl 36 | #define prinpar(p) if(debug) cout << #p << ".fi=" << p.fi << " " << #p << ".se=" << p.se << endl 37 | #define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;} 38 | 39 | #define mt make_tuple 40 | #define get(a,id) get(a) 41 | #define t3ll tuple 42 | 43 | const int N = 1e5+10; 44 | 45 | class node{ 46 | public: 47 | list nb; 48 | }; 49 | 50 | vector g; 51 | 52 | bool vis[N]; 53 | 54 | void dfs(int no){ 55 | if(vis[no]) return; 56 | vis[no] = 1; 57 | for(auto it : g[no].nb){ 58 | dfs(it); 59 | } 60 | 61 | return; 62 | } 63 | 64 | int main(){ 65 | bool ans = 0; 66 | int n, m; 67 | cin >> n >> m; 68 | g.resize(n); 69 | 70 | fr(i,m){ 71 | int u, v; 72 | cin >> u >> v; 73 | u--, v--; 74 | g[u].nb.pb(v); 75 | g[v].nb.pb(u); 76 | } 77 | 78 | dfs(0); 79 | 80 | int visitados = 0; 81 | fr(i,n){ 82 | if(vis[i]) visitados++; 83 | else{ 84 | prin(i+1); 85 | } 86 | } 87 | 88 | prin(n); 89 | prin(visitados); 90 | 91 | return 0; 92 | } 93 | 94 | -------------------------------------------------------------------------------- /Graphs/tools/listaPai_to_pairOfEdges_csacad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define FILE_IN freopen("kotlin.in", "r", stdin); 5 | #define FILE_OUT freopen("kotlin.out", "w", stdout); 6 | 7 | #define fr(i,n) for(int i=0;i pii; 21 | typedef pair pll; 22 | 23 | #define PI acos(-1) 24 | ll MOD = 1e9+7; 25 | 26 | //LONG_LONG_MAX 27 | //-DBL_MAX 28 | 29 | bool debug = 1; 30 | #define printa(a) cout << #a << " = " << (a) << endl 31 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 32 | #define soprin(a) if(debug) cout << (a) 33 | #define ppal(a) if(debug) cout << #a << endl 34 | #define prinsep if(debug) cout << "------" << endl 35 | #define cendl if(debug) cout << endl 36 | #define prinpar(p) if(debug) cout << #p << ".fi=" << p.fi << " " << #p << ".se=" << p.se << endl 37 | #define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;} 38 | 39 | #define mt make_tuple 40 | #define get(a,id) get(a) 41 | #define t3ll tuple 42 | 43 | const int N = 1e5+10; 44 | 45 | vector v; 46 | set s; 47 | 48 | int main(){ 49 | int n; 50 | cin >> n; 51 | 52 | frr(i,2,n){ 53 | int x; 54 | scanf("%d", &x); 55 | if(x){ 56 | v.pb(pii(x,i)), s.insert(x), s.insert(i); 57 | } 58 | } 59 | 60 | for(auto it : s){ 61 | cout << it << endl; 62 | } 63 | 64 | for(auto it : v){ 65 | cout << it.fi << " " << it.se << endl; 66 | } 67 | 68 | return 0; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /dataStructures/orderedSet/cs_68_c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include //required 3 | #include //required 4 | using namespace __gnu_pbds; //required 5 | using namespace std; 6 | 7 | #define FILE_IN freopen("kotlin.in", "r", stdin); 8 | #define FILE_OUT freopen("kotlin.out", "w", stdout); 9 | 10 | #define fr(i,n) for(int i=0;i pii; 24 | typedef pair pll; 25 | typedef tuple t3ll; 26 | 27 | #define mt make_tuple 28 | #define get(a,id) get(a) 29 | 30 | const long double PI = acos(-1.0l); 31 | const ll MOD = 1e9+7; 32 | 33 | //LONG_LONG_MAX 34 | //-DBL_MAX 35 | 36 | bool debug = 1; 37 | #define printa(a) cout << #a << " = " << (a) << endl 38 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 39 | #define soprin(a) if(debug) cout << (a) 40 | #define ppal(a) if(debug) cout << #a << endl 41 | #define prinsep if(debug) cout << "------" << endl 42 | #define cendl if(debug) cout << endl 43 | #define prinpar(p) if(debug) cout << #p << ".fi=" << (p).fi << " " << #p << ".se=" << (p).se << endl 44 | #define print(tup) if(debug) cout << #tup << " = {" << get(tup,0) << ", " << get(tup,1) << ", " << get(tup,2) << "}\n" 45 | #define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;} 46 | 47 | const int N = 1e5; 48 | 49 | template class cmp{ 50 | public: 51 | bool operator() (const T& a, const T& b) const {return a.se*b.fi using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 55 | 56 | ordered_set s; 57 | 58 | vector vp; 59 | vector ord; 60 | int ans[N]; 61 | 62 | class cmp2{ 63 | public: 64 | bool operator ()(int a, int b){ 65 | return vp[a]> n; 73 | 74 | fr(i,n){ 75 | int x, y; 76 | scanf("%d%d", &x, &y); 77 | vp.pb(pll(x,y)); 78 | ord.pb(i); 79 | } 80 | 81 | sort(all(ord), cmp2()); 82 | 83 | fr(i,n){ 84 | s.insert(vp[ord[i]]); 85 | ans[ord[i]] = s.order_of_key(vp[ord[i]]); 86 | } 87 | 88 | fr(i,n) printf("%d%c", ans[i], " \n"[i==n-1]); 89 | 90 | return 0; 91 | } 92 | 93 | -------------------------------------------------------------------------------- /dataStructures/orderedSet/ordered_set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include //required 3 | #include //required 4 | using namespace __gnu_pbds; //required 5 | using namespace std; 6 | 7 | //---------------------------------------------------------- 8 | 9 | bool debug = 1; 10 | #define printa(a) cout << #a << " = " << (a) << endl 11 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 12 | 13 | //SEM TEMPLATE PRA SER MAIS RÁPIDO 14 | 15 | typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; 16 | 17 | ordered_set s; 18 | 19 | //FUNÇÃO COMPARADORA CUSTOMIZADA - SEM TEMPLATE E ORDENANDO ÍNDICE (NÃO ELEMENTO EM SI -> MAIS RÁPIDO) 20 | /* 21 | vector v; 22 | 23 | class cmp{ 24 | public: 25 | bool operator() (int a, int b) const { 26 | if(v[a].fi*v[b].se==v[a].se*v[b].fi) return a ordered_set; 32 | 33 | ordered_set s; 34 | */ 35 | 36 | int main(){ 37 | for(auto it : {1,2,4,5}) 38 | s.insert(it); 39 | 40 | prin(s.order_of_key(1)); // 0 (key/elemento 1 ta na posicao 0th) (0-indexed) 41 | 42 | prin(s.order_of_key(3)); // 2 (index) 43 | // 3 seria o terceiro elemnto (2 em 0-indexed) == numero de elemntos estritamente menores que x 44 | 45 | //find retorna iterador!! 46 | prin(*s.find_by_order(0)); // elemento 1 (printa o 0th elemnto) 47 | 48 | //-------------------------------------------------------------------------------------------- 49 | 50 | //* 51 | cout << endl; 52 | prin(s.size()); 53 | cout << "pus elemento repetido" << endl; 54 | s.insert(4); //funciona como set -> nao repete elemnto 55 | prin(s.size()); 56 | cout << endl; 57 | 58 | //tem lower e upper bound tmbm - retorna iterador 59 | prin(*s.lower_bound(3)); 60 | cout << endl; 61 | 62 | //erase - funciona tanto para elemneto qnt pra iterador 63 | s.erase(4); 64 | cout << "apaguei elemento" << endl; 65 | prin(s.size()); 66 | s.erase(s.find(2)); 67 | cout << endl; 68 | 69 | //tem clear e empty tmbm 70 | s.clear(); 71 | prin(s.empty()); 72 | 73 | //*/ 74 | } 75 | 76 | /* 77 | usado no problema do csacademy - cs_68_c.cpp: 78 | 79 | template struct cmp{ 80 | bool operator() (const T& x, const T& y) const {return x.fi*y.se using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 84 | 85 | ordered_set s; 86 | */ 87 | 88 | 89 | /* 90 | template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 91 | 92 | ordered_set s; 93 | 94 | or: 95 | typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; 96 | ordered_set s; 97 | //This works in C++98 but the above version wiht template only works in C++11 98 | //*/ 99 | 100 | -------------------------------------------------------------------------------- /dataStructures/orderedSet/ordered_set_template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i pii; 10 | 11 | bool debug = 1; 12 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 13 | 14 | //------------------------------------------------------------------- 15 | #include //required 16 | #include //required 17 | using namespace __gnu_pbds; //required 18 | 19 | template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 20 | 21 | ordered_set s; 22 | //------------------------------------------------------------------- 23 | 24 | int main(){ 25 | 26 | frr(i,1,5) s.insert({i,i}); 27 | 28 | prin(s.order_of_key({1,1})); // 0 (key/elemento 1 ta na posicao 0th) (0-indexed) 29 | 30 | prin(s.order_of_key({2,3})); // 2 (seria posto no 2o elemento, 0-indexado) 31 | 32 | //find retorna iterador!! 33 | prin(s.find_by_order(3)->fi); // {4,4} (4 é o 3o elemento, 0-indexado) 34 | 35 | prin(s.size()); 36 | 37 | s.erase({2,2}); 38 | 39 | prin(s.size()); 40 | 41 | return 0; 42 | } 43 | 44 | /* 45 | Com comparador feito 46 | 47 | template struct cmp{ 48 | bool operator() (const T& x, const T& y) const {return x.fi*y.se using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 52 | 53 | ordered_set s; 54 | */ 55 | 56 | -------------------------------------------------------------------------------- /dataStructures/segTree/classic+simple/classic_normal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i struct Seg{ 20 | ll *v, *s; 21 | 22 | void build(int no = 1, int l = 0, int r = N){ 23 | if(r-l==1){ 24 | s[no] = v[l]; 25 | return; 26 | } 27 | int mid = (l+r)/2; 28 | build(2*no,l,mid); 29 | build(2*no+1,mid,r); 30 | s[no] = min(s[2*no],s[2*no+1]); 31 | //s[no] = s[2*no]+s[2*no+1]; 32 | } 33 | 34 | Seg(ll *vb, ll *sb){ 35 | v = vb; 36 | s = sb; 37 | build(); 38 | } 39 | 40 | //Muda o valor, v[i] = x 41 | void upd(int i, ll x, int no = 1, int l = 0, int r = N){ 42 | if(r-l==1){ 43 | s[no] = x; // se quiser fazer upd de incremento tal que v[i] += x, basta mudar = por += aqui 44 | return; 45 | } 46 | int mid = (l+r)/2; 47 | if(i> n >> q; 74 | 75 | fr(i,n) scanf("%lld", v+i); 76 | Seg seg(v,s); 77 | 78 | fr(qq,q){ 79 | char c; 80 | scanf(" %c", &c); 81 | if(c=='q'){ 82 | int l, r; 83 | scanf("%d%d", &l, &r); 84 | l--; 85 | printf("%lld\n", seg.qry(l,r)); 86 | } else{ 87 | int p, x; 88 | scanf("%d%d", &p, &x); 89 | p--; 90 | seg.upd(p,x); 91 | } 92 | } 93 | return 0; 94 | } 95 | 96 | /* 97 | input: 98 | 7 11 99 | 1 2 3 4 5 6 7 100 | q 1 7 101 | q 1 6 102 | q 2 6 103 | u 1 7 104 | u 2 3 105 | q 1 6 106 | u 4 -10 107 | q 3 5 108 | u 7 -100 109 | q 4 4 110 | q 2 7 111 | 112 | answer: 113 | 1 114 | 1 115 | 2 116 | 3 117 | -10 118 | -10 119 | -100 120 | */ 121 | 122 | -------------------------------------------------------------------------------- /dataStructures/segTree/different_segs/non_commutative_seg_iter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i s[2*N]; 15 | 16 | int n; 17 | 18 | char str[N]; 19 | 20 | array comb(array &a, array &b){ 21 | array ans; 22 | int mn = min(a[1],b[2]); 23 | fr(i,3) ans[i] = a[i]+b[i]; 24 | ans[0]+=2*mn; 25 | frr(i,1,2) ans[i]-=mn; 26 | return ans; 27 | } 28 | 29 | void build(){ 30 | fr(i,n){ 31 | if(str[i]=='('){ 32 | s[i+n] = {0,1,0}; 33 | } else s[i+n] = {0,0,1}; 34 | } 35 | for(int i = n-1;i>=1;i--){ 36 | s[i] = comb(s[i<<1],s[i<<1|1]); 37 | } 38 | } 39 | 40 | int query(int l, int r){ 41 | array resl = {0,0,0}, resr = {0,0,0}; 42 | for(l+=n,r+=n;l>=1,r>>=1){ 43 | if(l&1) resl = comb(resl,s[l++]); 44 | if(r&1) resr = comb(s[--r],resr); 45 | } 46 | return comb(resl,resr)[0]; 47 | } 48 | 49 | int main(){ 50 | int id = 0; 51 | char c; 52 | while(scanf("%c", &c)!=EOF and c!='\n') str[id++] = c; 53 | 54 | n = id; 55 | build(); 56 | 57 | int q; 58 | cin >> q; 59 | 60 | fr(i,q){ 61 | int l, r; 62 | scanf("%d %d", &l, &r); 63 | l--; 64 | printf("%d\n", query(l,r)); 65 | } 66 | 67 | return 0; 68 | } 69 | 70 | -------------------------------------------------------------------------------- /dataStructures/segTree/old_lazies/lazy_flip_e_euler_tour.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i v; 16 | int *s, *lazy; 17 | 18 | void build(int no = 1, int l = 0, int r = ng){ 19 | if(r-l==1){ 20 | s[no] = v[l]; 21 | return; 22 | } 23 | int mid= (l+r)/2; 24 | build(2*no,l,mid); 25 | build(2*no+1,mid,r); 26 | s[no] = s[2*no] + s[2*no+1]; 27 | } 28 | 29 | Seg(vector &vb, int *sb, int *lazyb){ 30 | v = vb; 31 | ng = vb.size(); 32 | s = sb; 33 | lazy = lazyb; 34 | build(); 35 | } 36 | 37 | void updlazy(int no, int l, int r){ 38 | lazy[no] = !lazy[no]; 39 | s[no] = r-l-s[no]; 40 | } 41 | 42 | void pass(int no, int l, int r){ 43 | if(!lazy[no]) return; 44 | int mid = (l+r)/2; 45 | updlazy(2*no,l,mid); 46 | updlazy(2*no+1,mid,r); 47 | lazy[no] = 0; 48 | } 49 | 50 | int qry(int lq, int rq, int no = 1, int l = 0, int r = ng){ 51 | if(rq<=l or lq>=r) return 0; 52 | if(l>=lq and rq>=r) return s[no]; 53 | pass(no,l,r); 54 | int mid = (l+r)/2; 55 | return qry(lq,rq,2*no,l,mid) + qry(lq,rq,2*no+1,mid,r); 56 | } 57 | 58 | void upd(int lq, int rq, int no = 1, int l = 0, int r = ng){ 59 | if(rq<=l or lq>=r) return; 60 | if(l>=lq and rq>=r){ 61 | updlazy(no,l,r); 62 | return; 63 | } 64 | pass(no,l,r); 65 | int mid = (l+r)/2; 66 | upd(lq,rq,2*no,l,mid), upd(lq,rq,2*no+1,mid,r); 67 | s[no] = s[2*no] + s[2*no+1]; 68 | } 69 | }; 70 | 71 | const int N = 2e5+10; 72 | 73 | int s[4*N], lazy[4*N]; 74 | vector v; 75 | 76 | int n; 77 | vector g[N]; 78 | int val[N]; 79 | int no_to_inic[N], no_to_fim[N]; 80 | char str[10]; 81 | 82 | void dfs(int no){ 83 | no_to_inic[no] = v.size(); 84 | v.pb(val[no]); 85 | for(auto &it : g[no]){ 86 | dfs(it); 87 | } 88 | no_to_fim[no] = v.size(); 89 | } 90 | 91 | int main(){ 92 | cin >> n; 93 | for(int i = 1; i> q; 105 | 106 | fr(qq,q){ 107 | int no; 108 | scanf(" %s %d", str, &no); 109 | no--; 110 | if(str[0]=='g'){ 111 | printf("%d\n", seg.qry(no_to_inic[no],no_to_fim[no])); 112 | } else{ 113 | seg.upd(no_to_inic[no],no_to_fim[no]); 114 | } 115 | } 116 | return 0; 117 | } 118 | 119 | -------------------------------------------------------------------------------- /dataStructures/segTree/seg_iterative_node.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i s; 28 | Seg(){} 29 | void build(){ 30 | for(int i = n-1;i>0;i--){ 31 | s[i] = oper(s[i<<1],s[i<<1|1]); 32 | } 33 | } 34 | 35 | Seg(int _n){ 36 | n = _n; 37 | s = vector(2*n); 38 | for(int i = n; i<2*n; i++) s[i] = nulo(); 39 | build(); 40 | } 41 | 42 | Seg(vector v){ 43 | n = sz(v); 44 | s = vector(2*n); 45 | for(int i = n; i<2*n; i++) s[i] = node{v[i-n]}; //mudar inicializacao de node a partir de v[i] 46 | build(); 47 | } 48 | //pos 0-indexed (incrementa/faz operacao, nao atualiza/seta) 49 | void upd(int pos, node val){ 50 | pos+=n; 51 | s[pos] = oper(s[pos],val); 52 | for(;pos>1;pos>>=1) 53 | s[pos>>1] = oper(s[pos],s[pos^1]); 54 | //para atualizar/setar: 55 | //for(s[pos+=n]=val;pos>1;pos>>=1) 56 | // s[pos>>1] = oper(s[pos],s[pos^1]); 57 | } 58 | //array é abstraido para 0-indexed (nas folhas da seg) e [l,r) 59 | node qry(int l, int r){ 60 | node ans = nulo(); 61 | for(l+=n,r+=n;l>=1,r>>=1){ 62 | if(l&1) ans = oper(ans,s[l++]); 63 | if(r&1) ans = oper(ans,s[--r]); 64 | } 65 | return ans; 66 | } 67 | }; // end seg 68 | 69 | int main(){ 70 | ios::sync_with_stdio(0); cin.tie(0); 71 | ll n, q; cin >> n >> q; 72 | vector v(n); 73 | fr(i,n) cin >> v[i]; 74 | Seg seg(v); 75 | 76 | //build alternativo, precisa especificar node() 77 | //Seg seg(vector(n,node())); 78 | 79 | fr(qq,q){ 80 | int tipo; cin >> tipo; 81 | if(tipo==0){ 82 | int p, x; cin >> p >> x; 83 | seg.upd(p,node{x}); 84 | } else{ 85 | int l, r; cin >> l >> r; 86 | cout << seg.qry(l,r).val << "\n"; 87 | } 88 | } 89 | } 90 | /* 91 | input: 92 | 5 5 93 | 1 2 3 4 5 94 | 1 0 5 95 | 1 2 4 96 | 0 3 10 97 | 1 0 5 98 | 1 0 3 99 | 100 | output: 101 | 15 102 | 7 103 | 25 104 | 6 105 | */ 106 | -------------------------------------------------------------------------------- /dataStructures/sparse_table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 9 | 10 | //solves https://atcoder.jp/contests/arc067/tasks/arc067_d 11 | 12 | int log_floor(int n){ 13 | return 31-__builtin_clz(n); 14 | } 15 | 16 | ll oper(ll a, ll b){ 17 | return max(a,b); 18 | } 19 | 20 | /* 21 | Sparse table de maximo 22 | Ou definida de acordo com funcao oper acima 23 | */ 24 | struct sparse_table{ 25 | int exp2; 26 | int n; 27 | vector> mat; 28 | sparse_table(){} 29 | sparse_table(vector v){ 30 | n = sz(v); 31 | exp2 = log_floor(n)+1; 32 | mat.resize(exp2); 33 | mat[0].resize(n); 34 | fr(i,n) mat[0][i] = v[i]; 35 | for(int k = 1; k=0 and r=r) return; 65 | ll id = (l+r)/2; 66 | ll idc = optl; 67 | ll resp = 0; 68 | 69 | for(int j = optl; jresp){ 73 | resp = cur; 74 | idc = j; 75 | } 76 | } 77 | rmax(ans,resp); 78 | rec(l,id,optl,idc+1); 79 | rec(id+1,r,idc,optr); 80 | } 81 | 82 | int main(){ 83 | ios::sync_with_stdio(0); cin.tie(0); 84 | cin >> n >> m; 85 | fr(i,n-1) cin >> pref[i]; 86 | for(int i = n-2; i>=0; i--) pref[i] = pref[i]+pref[i+1]; 87 | vector> mat(m,vector(n)); 88 | fr(i,n) fr(j,m) cin >> mat[j][i]; 89 | 90 | fr(i,m) st[i] = sparse_table(mat[i]); 91 | 92 | rec(0,n,0,n); 93 | cout << ans << "\n"; 94 | } 95 | -------------------------------------------------------------------------------- /excessive/DSU/dsu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; ipai[v]) swap(u,v); 27 | pai[u]+=pai[v]; 28 | pai[v] = u; 29 | } 30 | } 31 | 32 | int freq[N]; 33 | 34 | int main(){ 35 | ios::sync_with_stdio(0); cin.tie(0); 36 | int t; cin >> t; 37 | 38 | fr(tt,t){ 39 | int n; cin >> n; 40 | string a, b; cin >> a >> b; 41 | 42 | for(char c = 'a'; c<='z'; c++){ 43 | freq[c] = 0; 44 | pai[c] = -1; 45 | } 46 | 47 | int ok = 1; 48 | fr(i,n){ 49 | if(a[i]>b[i]) ok = 0; 50 | else if(a[i] 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i *g; 18 | 19 | void dfs(int no, int from, int dac, ll d[]){ 20 | d[no] = dac; 21 | for(auto it : g[no]){ 22 | if(it!=from) dfs(it,no,dac+1,d); 23 | } 24 | } 25 | 26 | int max_dist_glob = 0; // diametro da arvore 27 | 28 | int get_most_dist(int no, ll d[]){ 29 | dfs(no,-1,0,d); 30 | int max_dist_cur = -1; 31 | int no_max_dist = -1; 32 | fr(i,n){ 33 | if(d[i]>max_dist_cur){ 34 | max_dist_cur = d[i]; 35 | no_max_dist = i; 36 | } 37 | } 38 | max_dist_glob = max(max_dist_glob,max_dist_cur); 39 | return no_max_dist; 40 | } 41 | 42 | vector nos_diam; 43 | 44 | int no0, no1; 45 | 46 | //retorna diametro em numero de arestas! (ou seja, vertices -1) 47 | ll find_diametro(vector _g[N], int _n, ll vd[2][N]){ 48 | g = _g; 49 | n = _n; 50 | max_dist_glob = 0; 51 | 52 | no1 = get_most_dist(0,vd[0]); 53 | no0 = get_most_dist(no1,vd[1]); 54 | 55 | //commentar se listar os nos no diametro nao eh necessario 56 | get_most_dist(no0,vd[0]); 57 | fr(i,n) 58 | if(vd[0][i]+vd[1][i]==max_dist_glob) 59 | nos_diam.push_back(i); 60 | //------------------------- 61 | 62 | return max_dist_glob; 63 | } 64 | 65 | }; //end space diametro 66 | 67 | vector g[2][N]; 68 | ll vdist[2][2][N]; 69 | 70 | int main(){ 71 | ll n[2]; 72 | while(scanf("%lld%lld", n, n+1)!=EOF){ 73 | fr(cor,2){ 74 | fr(i,n[cor]-1){ 75 | int a, b; scanf("%d%d", &a, &b); a--,b--; 76 | g[cor][a].push_back(b); 77 | g[cor][b].push_back(a); 78 | } 79 | } 80 | ll max_diam = 0; 81 | fr(cor,2) rmax(max_diam,diametro_space::find_diametro(g[cor],n[cor],vdist[cor])); 82 | 83 | fr(cor,2) fr(i,n[cor]) g[cor][i].clear(); 84 | 85 | vector v[2]; 86 | fr(cor,2){ 87 | fr(i,n[cor]){ 88 | v[cor].push_back(max(vdist[cor][0][i],vdist[cor][1][i])); 89 | } 90 | sort(all(v[cor])); 91 | } 92 | 93 | vector soma_suf(n[1]); 94 | for(int i = n[1]-1; i>=0; i--){ 95 | soma_suf[i] = v[1][i]; 96 | if(i+1=0 and v[1][j-1]+v[0][i]+1>=max_diam) j--; 103 | ll tam_suf = n[1]-j; 104 | 105 | soma += (n[1]-tam_suf)*max_diam; 106 | if(tam_suf) soma += tam_suf*v[0][i] + soma_suf[j] + tam_suf; 107 | } 108 | 109 | printf("%.3lf\n", (double)soma/(n[1]*n[0])); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /excessive/Graph/dijkistra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i> g[N]; 13 | 14 | template 15 | using pqt = priority_queue,greater>; 16 | 17 | vector> edges_sp; //edges in shortes path 18 | 19 | /* 20 | Computes the distance array, distance from source src to every node 21 | or LLONG_MAX if node not reachable 22 | dest, the destination, can be specified or not to reconstruct the path to it 23 | */ 24 | void dij(int src, int n, ll *dist, int dest = -1){ 25 | vector vis(n); 26 | vector ant(n,-1); 27 | fr(i,n) dist[i] = LLONG_MAX; 28 | pqt> pq; 29 | pq.emplace(0,src); 30 | dist[src] = 0; 31 | while(!pq.empty()){ 32 | ll dac, no; tie(dac,no) = pq.top(); pq.pop(); 33 | if(vis[no]) continue; 34 | vis[no] = 1; 35 | for(auto &[it,c] : g[no]){ 36 | if(vis[it]) continue; 37 | ll dit = dac+c; 38 | if(dit> n >> m >> src >> dest; 61 | fr(i,m){ 62 | ll a, b, c; cin >> a >> b >> c; 63 | g[a].emplace_back(b,c); 64 | } 65 | dij(src,n,dist,dest); 66 | if(dist[dest]==LLONG_MAX){ 67 | cout << -1 << "\n"; 68 | } else{ 69 | cout << dist[dest] << " " << sz(edges_sp) << "\n"; 70 | for(auto &[a,b] : edges_sp){ 71 | cout << a << " " << b << "\n"; 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /excessive/Graph/find_cycle_directed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i g[N]; 9 | 10 | //directed graph 11 | namespace find_cycle{ 12 | 13 | int found_cycle = 0; 14 | int first_node = -1; 15 | vector nodes_in_cycle; 16 | int vis[N], ativ[N]; 17 | 18 | void reset(int n){ 19 | fr(i,n){ 20 | vis[i] = ativ[i] = 0; 21 | g[i].clear(); 22 | } 23 | nodes_in_cycle.clear(); 24 | found_cycle = 0; 25 | first_node = -1; 26 | } 27 | 28 | int dfs(int no){ // dfs(int no, int from){ 29 | if(found_cycle) return 0; 30 | if(vis[no] and !ativ[no]) return 0; 31 | vis[no] = 1; 32 | ativ[no] = 1; 33 | 34 | for(auto it : g[no]){ 35 | //if(it==from) continue; 36 | if(ativ[it]){ 37 | nodes_in_cycle.push_back(no); 38 | first_node = it; 39 | found_cycle = 1; 40 | return 1; 41 | } 42 | if(dfs(it)){ 43 | nodes_in_cycle.push_back(no); 44 | if(no==first_node){ 45 | reverse(all(nodes_in_cycle)); 46 | return 0; 47 | } 48 | return 1; 49 | } 50 | } 51 | 52 | ativ[no] = 0; 53 | 54 | return 0; 55 | } 56 | 57 | bool go_find(int n){ 58 | fr(i,n){ 59 | if(!vis[i]){ 60 | dfs(i); //dfs(i,-1); 61 | if(found_cycle) return 1; 62 | } 63 | } 64 | return 0; 65 | } 66 | 67 | }; 68 | 69 | //tested in https://codeforces.com/contest/1476/submission/105919213 70 | 71 | #define prinv(v) cout << #v << " = "; for(auto &it : v) cout << it << ","; cout << endl; 72 | 73 | int main(){ 74 | g[0].push_back(2); 75 | g[2].push_back(1); 76 | g[1].push_back(3); 77 | g[3].push_back(2); 78 | if(find_cycle::go_find(4)){ 79 | prinv(find_cycle::nodes_in_cycle); 80 | } 81 | find_cycle::reset(4); 82 | g[0].push_back(1); 83 | g[2].push_back(1); 84 | if(find_cycle::go_find(4)){ 85 | prinv(find_cycle::nodes_in_cycle); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /excessive/Graph/topo_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i g[N]; 9 | 10 | namespace topo_sort{ 11 | vector p; 12 | bool vis[N]; 13 | 14 | void dfs(int no){ 15 | if(vis[no]) return; 16 | vis[no] = 1; 17 | for(auto &it : g[no]){ 18 | dfs(it); 19 | } 20 | p.push_back(no); 21 | } 22 | 23 | vector get_topo(int n){ 24 | fr(i,n) vis[i] = 0;//reset 25 | p.clear(); 26 | 27 | fr(i,n) dfs(i); 28 | reverse(all(p)); 29 | return p; 30 | } 31 | 32 | } 33 | 34 | //tested in https://codeforces.com/contest/1476/submission/105957239 35 | 36 | #define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl 37 | 38 | int main(){ 39 | ios::sync_with_stdio(0); cin.tie(0); 40 | g[2].push_back(1); 41 | g[2].push_back(0); 42 | g[0].push_back(1); 43 | vector p = topo_sort::get_topo(3); 44 | prinv(p); 45 | } 46 | -------------------------------------------------------------------------------- /excessive/LIS/lis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | //solves https://codeforces.com/contest/1662/problem/L 19 | 20 | //use: mymap mp 21 | template 22 | struct mymap{ 23 | vector keys; 24 | vector values; 25 | 26 | mymap(vector _keys){ 27 | keys = _keys; 28 | sort(all(keys)); 29 | keys.resize(unique(all(keys))-keys.begin()); 30 | values = vector(sz(keys)); //valores iniciados como default (nulo) 31 | fr(i,sz(values)) values[i] = i; 32 | } 33 | 34 | value_type &operator[](key_type key){ 35 | //assert(binary_search(all(keys),key)); 36 | int id = lower_bound(all(keys),key)-keys.begin(); 37 | return values[id]; 38 | } 39 | }; //end mymap 40 | 41 | struct node{ 42 | ll val; 43 | }; 44 | 45 | node oper(node a, node b){ 46 | return node{max(a.val,b.val)}; 47 | } 48 | 49 | struct Seg{ 50 | node nulo(){ 51 | return node{0}; 52 | } 53 | int n; 54 | vector s; 55 | Seg(){} 56 | void build(){ 57 | for(int i = n-1;i>0;i--){ 58 | s[i] = oper(s[i<<1],s[i<<1|1]); 59 | } 60 | } 61 | Seg(vector v){ 62 | n = sz(v); 63 | s = vector(2*n); 64 | for(int i = n; i<2*n; i++) s[i] = node{v[i-n]}; //mudar inicializacao de node a partir de v[i] 65 | build(); 66 | } 67 | //pos 0-indexed (incrementa/faz operacao, nao atualiza/seta) 68 | void upd(int pos, node val){ 69 | pos+=n; 70 | s[pos] = oper(s[pos],val); 71 | for(;pos>1;pos>>=1) 72 | s[pos>>1] = oper(s[pos],s[pos^1]); 73 | //para atualizar/setar: 74 | //for(s[pos+=n]=val;pos>1;pos>>=1) 75 | // s[pos>>1] = oper(s[pos],s[pos^1]); 76 | } 77 | //array é abstraido para 0-indexed (nas folhas da seg) e [l,r) 78 | node qry(int l, int r){ 79 | node ans = nulo(); 80 | for(l+=n,r+=n;l>=1,r>>=1){ 81 | if(l&1) ans = oper(ans,s[l++]); 82 | if(r&1) ans = oper(ans,s[--r]); 83 | } 84 | return ans; 85 | } 86 | }; // end seg 87 | 88 | int lis(vector &v){ 89 | bool is_non_decreasing = 1; //setar se quero estritamente crescenete ou nao 90 | mymap mp(v); 91 | Seg seg(vector(sz(mp.keys))); 92 | for(auto &x : v){ 93 | int is = mp[x]; 94 | auto no = seg.qry(0,is+is_non_decreasing); 95 | no.val++; 96 | seg.upd(is,no); 97 | } 98 | return seg.qry(0,sz(mp.keys)).val; 99 | } 100 | 101 | 102 | const int N = 2e5+10; 103 | ll t[N], x[N]; 104 | 105 | int n, v; 106 | 107 | ll f1(int i){ 108 | return v*t[i]-x[i]; 109 | } 110 | 111 | ll f2(int i){ 112 | return v*t[i]+x[i]; 113 | } 114 | 115 | bool alc(int i){ 116 | return abs(x[i]) <= v*t[i]; 117 | } 118 | 119 | int main(){ 120 | ios::sync_with_stdio(0); cin.tie(0); 121 | cin >> n >> v; 122 | fr(i,n) cin >> t[i]; 123 | fr(i,n) cin >> x[i]; 124 | 125 | vector> vt; 126 | vector vv; 127 | fr(i,n){ 128 | if(!alc(i)) continue; 129 | vt.emplace_back(f1(i), f2(i), i); 130 | } 131 | sort(all(vt)); 132 | for(auto &[v1,v2,i] : vt){ 133 | vv.push_back(v2); 134 | } 135 | cout << lis(vv) << "\n"; 136 | } 137 | 138 | -------------------------------------------------------------------------------- /excessive/MO/mo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i> qry; 23 | 24 | //comparador dos indices das querys do mo 25 | struct cmp{ 26 | bool operator ()(int i, int j){ 27 | auto &a = qry[i], &b = qry[j]; 28 | if(a.fi/rz!=b.fi/rz) return a.fi/rz < b.fi/rz; 29 | if(a.se!=b.se) return a.semax_freq) max_freq = freq[id]; 41 | } 42 | 43 | void tira(int id){ 44 | qnts_freq[freq[id]]--; 45 | freq[id]--; 46 | qnts_freq[freq[id]]++; 47 | if(qnts_freq[max_freq]==0) max_freq--; 48 | } 49 | 50 | int main(){ 51 | ios::sync_with_stdio(0); cin.tie(0); 52 | 53 | int n, q; 54 | cin >> n >> q; 55 | 56 | vector ans(q); 57 | vector v(n); 58 | fr(i,n) cin >> v[i]; 59 | 60 | fr(i,q){ 61 | int l, r; cin >> l >> r; 62 | l--,r--; 63 | //[l,r] é o intervalo incluso da query 64 | qry.emplace_back(l,r); 65 | } 66 | rz = max(1,(int)sqrt(n)); 67 | 68 | vector ito; 69 | fr(i,q) ito.push_back(i); 70 | sort(all(ito),cmp()); 71 | 72 | //nao necessario, mas bom para coordenar q tem n elementos com frequencia 0 73 | qnts_freq[0] = n; 74 | 75 | //curr começa negativo para o indice 0 ser ativado 76 | int curl = 0, curr = -1; 77 | fr(i,q){ 78 | int l ,r; 79 | tie(l,r) = qry[ito[i]]; 80 | //fazer poes antes dos tira para nao dar negativo no meio 81 | while(currl) poe(v[--curl]); 83 | while(curr>r) tira(v[curr--]); 84 | while(curl 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 13 | T sq(T x){ 14 | return x*x; 15 | } 16 | 17 | const long double pi = acos(-1.0l); 18 | 19 | struct pt{ 20 | long double x, y; 21 | pt(){} 22 | pt(long double _x, long double _y){ 23 | x = _x, y = _y; 24 | } 25 | long double mod(){ 26 | return sqrt(sq(x)+sq(y)); 27 | } 28 | pt operator -(pt b){ 29 | return pt(x-b.x,y-b.y); 30 | } 31 | pt operator +(pt b){ 32 | return pt(x+b.x,y+b.y); 33 | } 34 | long double operator ^(pt b) const{ 35 | return x*b.y-y*b.x; 36 | } 37 | int quad() const{ 38 | int ans = 0; 39 | if(x<0) ans++; 40 | if(y<0) ans^=1, ans+=2; 41 | return ans; 42 | } 43 | bool operator <(pt b) const{ 44 | if(this->quad()==b.quad()){ 45 | return ((*this)^b)>0; 46 | } 47 | return this->quad() 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 13 | T sq(T x){ 14 | return x*x; 15 | } 16 | 17 | struct pt{ 18 | long double x, y; 19 | pt(){} 20 | pt(long double _x, long double _y){ 21 | x = _x, y = _y; 22 | } 23 | long double mod(){ 24 | return sqrt(sq(x)+sq(y)); 25 | } 26 | pt operator -(pt b){ 27 | return pt(x-b.x,y-b.y); 28 | } 29 | pt operator +(pt b){ 30 | return pt(x+b.x,y+b.y); 31 | } 32 | long double operator ^(pt b) const{ 33 | return x*b.y-y*b.x; 34 | } 35 | int quad() const{ 36 | int ans = 0; 37 | if(x<0) ans++; 38 | if(y<0) ans^=1, ans+=2; 39 | return ans; 40 | } 41 | bool operator <(pt b) const{ 42 | if(this->quad()==b.quad()){ 43 | return ((*this)^b)>0; 44 | } 45 | return this->quad() 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 13 | T sq(T x){ 14 | return x*x; 15 | } 16 | 17 | const long double pi = acos(-1.0l); 18 | 19 | struct pt{ 20 | long double x, y; 21 | pt(){} 22 | pt(long double _x, long double _y){ 23 | x = _x, y = _y; 24 | } 25 | long double mod(){ 26 | return sqrt(sq(x)+sq(y)); 27 | } 28 | pt operator -(pt b){ 29 | return pt(x-b.x,y-b.y); 30 | } 31 | pt operator +(pt b){ 32 | return pt(x+b.x,y+b.y); 33 | } 34 | long double operator ^(pt b) const{ 35 | return x*b.y-y*b.x; 36 | } 37 | int quad() const{ 38 | int ans = 0; 39 | if(x<0) ans++; 40 | if(y<0) ans^=1, ans+=2; 41 | return ans; 42 | } 43 | bool operator <(pt b) const{ 44 | if(this->quad()==b.quad()){ 45 | return ((*this)^b)>0; 46 | } 47 | return this->quad() 2 | using namespace std; 3 | 4 | //mudar file.in pelo nome do input e por FILE_IN 5 | //antes dos scanfs 6 | #define FILE_IN freopen("file.in", "r", stdin); 7 | 8 | #define fr(i,n) for(int i=0;i pii; 22 | 23 | #define PI acos(-1) 24 | ll MOD = 1e9+7; 25 | 26 | //LONG_LONG_MAX 27 | //-DBL_MAX 28 | 29 | bool debug = 1; 30 | #define printa(a) cout << #a << " = " << (a) << endl 31 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 32 | #define soprin(a) if(debug) cout << (a) 33 | #define ppal(a) if(debug) cout << #a << endl 34 | #define prinsep if(debug) cout << "------" << endl 35 | #define cendl if(debug) cout << endl 36 | 37 | const ll MN = (1<<21), N = 1e5+10; 38 | 39 | ll dp[MN], cnt[MN]; 40 | 41 | int main(){ 42 | ll n, m; 43 | 44 | scanf("%lld%lld",&n, &m); 45 | 46 | fr(i,n){ 47 | ll x; 48 | scanf("%lld",&x); 49 | cnt[x]++, dp[x]++; 50 | } 51 | 52 | fr(j,21){ 53 | fr(i,MN){ 54 | if(i&(1< 4 | using namespace std; 5 | 6 | //mudar file.in pelo nome do input e por FILE_IN 7 | //antes dos scanfs 8 | #define FILE_IN freopen("file.in", "r", stdin); 9 | 10 | #define fr(i,n) for(int i=0;i pii; 25 | #define PI acos(-1) 26 | ll MOD = 1e9+7; 27 | 28 | //LONG_LONG_MAX 29 | //-DBL_MAX 30 | 31 | bool debug = 1; 32 | #define printa(a) cout << #a << " = " << (a) << endl 33 | #define prin(a) (debug?cout << #a << " = " << (a) << endl:cout << "") 34 | #define soprin(a) if(debug) cout << (a); 35 | #define ppal(a) if(debug) cout << #a << endl; 36 | #define prinsep if(debug) cout << "------" << endl; 37 | #define cendl if(debug) cout << endl 38 | 39 | const int N = 1e7+1000; 40 | 41 | vector v[N]; 42 | 43 | int main(){ 44 | ll n; 45 | scanf("%lld", &n); 46 | 47 | fr(i,n){ 48 | ll x, r; 49 | scanf("%lld%lld", &x, &r); 50 | v[x].pb({r,i}); 51 | 52 | } 53 | 54 | vector ans(n); 55 | multiset st; 56 | fr(i,N-5){ 57 | while(!v[i].empty()){ 58 | st.insert(v[i].back()); 59 | v[i].pop_back(); 60 | } 61 | 62 | if(!st.empty()){ 63 | ans[st.begin()->se] = i; 64 | st.erase(st.begin()); 65 | 66 | } 67 | } 68 | 69 | for(auto it : ans){ 70 | cout << it << " "; 71 | } 72 | cout << endl; 73 | 74 | return 0; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /excessive/meet_int_the_middle/icmc_seletiva_2017_k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //mudar file.in pelo nome do input e por FILE_IN 5 | //antes dos scanfs 6 | #define FILE_IN freopen("file.in", "r", stdin); 7 | 8 | #define fr(i,n) for(int i=0;i pii; 21 | 22 | #define PI acos(-1) 23 | 24 | //LONG_LONG_MAX 25 | //-DBL_MAX 26 | 27 | bool debug = 0; 28 | #define printa(a) cout << #a << " = " << (a) << endl 29 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 30 | #define soprin(a) if(debug) cout << (a) 31 | #define ppal(a) if(debug) cout << #a << endl 32 | #define prinsep if(debug) cout << "------" << endl 33 | #define cendl if(debug) cout << endl 34 | 35 | const int N = 1e5+10; 36 | const int MN = (1<<21) + 10; 37 | 38 | int freqmais[MN], freqmenos[MN]; 39 | 40 | vector v; 41 | int n, m; 42 | int m1, m2; 43 | 44 | int prim(int x){ 45 | return x>>m2; 46 | } 47 | 48 | int seg(int x){ 49 | return x%(1< eq; 68 | 69 | sort(all(v)); 70 | 71 | int ant = -1; 72 | int cur = 0; 73 | fr(i,n){ 74 | if(v[i]==ant){ 75 | cur++; 76 | } else{ 77 | if(cur>1){ 78 | eq.pb(cur); 79 | } 80 | cur = 1; 81 | ant = v[i]; 82 | } 83 | } 84 | 85 | if(cur>1){ 86 | eq.pb(cur); 87 | } 88 | 89 | m1 = (m+1)/2, m2 = m/2; 90 | long long ans = 1ll*n*(n-1)/2; 91 | 92 | fr(i,n){ 93 | int pcur = prim(v[i]); 94 | int scur = seg(v[i]); 95 | int mm = (1ll< 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 15 | #define rmax(a,b) a = max(a,b) 16 | 17 | #define fi first 18 | #define se second 19 | 20 | template 21 | string to_string(pair p); 22 | 23 | template 24 | string to_string(tuple p); 25 | 26 | template 27 | string to_string(tuple p); 28 | 29 | string to_string(const string& s) { 30 | return '"' + s + '"'; 31 | } 32 | 33 | string to_string(const char* s) { 34 | return to_string((string) s); 35 | } 36 | 37 | string to_string(bool b) { 38 | return (b ? "true" : "false"); 39 | } 40 | 41 | string to_string(vector v) { 42 | bool first = true; 43 | string res = "{"; 44 | for (int i = 0; i < static_cast(v.size()); i++) { 45 | if (!first) { 46 | res += ", "; 47 | } 48 | first = false; 49 | res += to_string(v[i]); 50 | } 51 | res += "}"; 52 | return res; 53 | } 54 | 55 | template 56 | string to_string(bitset v) { 57 | string res = ""; 58 | for (size_t i = 0; i < N; i++) { 59 | res += static_cast('0' + v[i]); 60 | } 61 | return res; 62 | } 63 | 64 | template 65 | string to_string(A v) { 66 | bool first = true; 67 | string res = "{"; 68 | for (const auto &x : v) { 69 | if (!first) { 70 | res += ", "; 71 | } 72 | first = false; 73 | res += to_string(x); 74 | } 75 | res += "}"; 76 | return res; 77 | } 78 | 79 | template 80 | string to_string(pair p) { 81 | return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; 82 | } 83 | 84 | template 85 | string to_string(tuple p) { 86 | return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; 87 | } 88 | 89 | template 90 | string to_string(tuple p) { 91 | return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; 92 | } 93 | 94 | void debug_out() { cerr << endl; } 95 | 96 | template 97 | void debug_out(Head H, Tail... T) { 98 | cerr << " " << to_string(H); 99 | debug_out(T...); 100 | } 101 | 102 | #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) 103 | 104 | clock_t ts; // time where the clock starts 105 | double get_time(){ 106 | return (double)(clock() - ts)/CLOCKS_PER_SEC; 107 | } 108 | 109 | ll gera(ll minx, ll maxx){ 110 | assert(maxx>=minx); 111 | return minx + rand()%(maxx-minx+1); 112 | } 113 | 114 | int main(){ 115 | ios::sync_with_stdio(0); cin.tie(0); 116 | srand(time(0)); 117 | set> st; 118 | st.emplace(1,2); 119 | st.emplace(3,4); 120 | debug(st); 121 | } 122 | -------------------------------------------------------------------------------- /excessive/misc/rmaxid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | //solves social distancing from ieeextreme 2020 19 | 20 | template 21 | void rmaxid(T &ans, int &id_ans, T cur, int id_cur){ 22 | if(cur>ans){ 23 | ans = cur; 24 | id_ans = id_cur; 25 | } 26 | } 27 | 28 | const int N = 1e5+10; 29 | vector g[N]; 30 | 31 | ll subsz[N], somadist[N]; 32 | 33 | namespace calc_subsz{ 34 | void dfs(int no, int from){ 35 | subsz[no] = 1; 36 | somadist[no] = 1; 37 | for(auto &it : g[no]){ 38 | if(it==from) continue; 39 | dfs(it,no); 40 | subsz[no]+=subsz[it]; 41 | somadist[no] += subsz[it]+somadist[it]; 42 | } 43 | } 44 | }; 45 | 46 | int n; 47 | 48 | ll somadistpai[N]; 49 | bool tem2cent = 0; 50 | int cent, paicent; 51 | 52 | namespace get_cent{ 53 | void dfs(int no, int from){ 54 | ll tot = 0; 55 | int id_max = -2; 56 | ll max_somadist = 0; 57 | for(auto &it : g[no]){ 58 | if(it==from) continue; 59 | tot += somadist[it]; 60 | rmaxid(max_somadist,id_max,somadist[it],it); 61 | } 62 | if(from!=-1){ 63 | tot+= somadistpai[from]; 64 | rmaxid(max_somadist,id_max,somadistpai[from],from); 65 | } 66 | 67 | /* 68 | prin(no+1); 69 | prin(id_max+1); 70 | prin(tot); 71 | prin(max_somadist); 72 | cout << endl; 73 | */ 74 | 75 | if(2*max_somadist <= tot or id_max==from){ 76 | cent = no; 77 | if(id_max==from){ 78 | paicent = from; 79 | tem2cent = 1; 80 | } 81 | return; 82 | } 83 | 84 | assert(id_max!=-2); 85 | assert(id_max!=from); 86 | 87 | somadistpai[no] = somadist[no]-(somadist[id_max]+subsz[id_max]); 88 | if(from!=-1){ 89 | somadistpai[no] += somadistpai[from]+(n-subsz[no]); 90 | } 91 | dfs(id_max,no); 92 | 93 | } 94 | }; 95 | 96 | double ans = 0; 97 | namespace get_sum_sq{ 98 | void dfs(int no, int from, double dac){ 99 | ans += dac*dac; 100 | for(auto &it : g[no]){ 101 | if(it==from) continue; 102 | dfs(it,no,dac+1); 103 | } 104 | } 105 | }; 106 | 107 | int main(){ 108 | ios::sync_with_stdio(0); cin.tie(0); 109 | cin >> n; 110 | fr(i,n-1){ 111 | int pai; cin >> pai; 112 | pai--; 113 | g[pai].push_back(i+1); 114 | g[i+1].push_back(pai); 115 | } 116 | 117 | calc_subsz::dfs(0,-1); 118 | 119 | /* 120 | fr(i,n){ 121 | prin(i+1); 122 | prin(somadist[i]); 123 | }*/ 124 | 125 | get_cent::dfs(0,-1); 126 | 127 | if(tem2cent){ 128 | ll sda = somadist[cent]-subsz[cent]; 129 | ll sza = subsz[cent]; 130 | 131 | ll szb = n-subsz[cent]; 132 | ll sdb = somadistpai[paicent]-szb; 133 | 134 | double x = (double)(sdb-sda+szb)/(sza+szb); 135 | if(x<0) x = 0; 136 | if(x>1) x = 1; 137 | 138 | get_sum_sq::dfs(cent,paicent,x); 139 | double ans_tot = ans; 140 | ans = 0; 141 | get_sum_sq::dfs(paicent,cent,1-x); 142 | ans_tot+=ans; 143 | 144 | ans_tot*=n; 145 | 146 | cout << fixed << setprecision(10) << ans_tot << "\n"; 147 | } else{ 148 | ans = 0; 149 | get_sum_sq::dfs(cent,-1,0); 150 | ans *= n; 151 | cout << fixed << setprecision(10) << ans << "\n"; 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /excessive/misc/tirar_contidos.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i pii; 15 | typedef vector vi; 16 | 17 | struct DSU { 18 | vi e; void init(int N) { e = vi(N,-1); } 19 | int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } 20 | bool sameSet(int a, int b) { return get(a) == get(b); } 21 | int size(int x) { return -e[get(x)]; } 22 | bool unite(int x, int y) { // union by size 23 | x = get(x), y = get(y); if (x == y) return 0; 24 | if (e[x] > e[y]) swap(x,y); 25 | e[x] += e[y]; e[y] = x; return 1; 26 | } 27 | }; 28 | 29 | struct Edge { int a, b; ll w; }; 30 | struct Node { /// lazy skew heap node 31 | Edge key; 32 | Node *l, *r; 33 | ll delta; 34 | void prop() { 35 | key.w += delta; 36 | if (l) l->delta += delta; 37 | if (r) r->delta += delta; 38 | delta = 0; 39 | } 40 | Edge top() { prop(); return key; } 41 | }; 42 | Node *merge(Node *a, Node *b) { 43 | if (!a || !b) return a ?: b; 44 | a->prop(), b->prop(); 45 | if (a->key.w > b->key.w) swap(a, b); 46 | swap(a->l, (a->r = merge(b, a->r))); 47 | return a; 48 | } 49 | void pop(Node*& a) { a->prop(); a = merge(a->l, a->r); } 50 | 51 | /* 52 | Acha menor arborescencia (menor soma de arestas) referente 53 | ao no r de um grafo g com n vertices 54 | Ou seja, tal que no r alcance todos 55 | ou -1 se não há 56 | */ 57 | ll dmstKACTL(int n, int r, const vector& g) { 58 | DSU dsu; dsu.init(n); 59 | vector heap(n); 60 | trav(e, g) heap[e.b] = merge(heap[e.b], new Node{e}); 61 | ll res = 0; 62 | vi seen(n, -1), path(n); seen[r] = r; 63 | F0R(s,n) { 64 | int u = s, qi = 0, w; 65 | while (seen[u] < 0) { 66 | path[qi++] = u, seen[u] = s; 67 | if (!heap[u]) return -1; 68 | Edge e = heap[u]->top(); 69 | heap[u]->delta -= e.w, pop(heap[u]); 70 | res += e.w, u = dsu.get(e.a); 71 | if (seen[u] == s) { 72 | Node* cyc = 0; 73 | do cyc = merge(cyc, heap[w = path[--qi]]); 74 | while (dsu.unite(u, w)); 75 | u = dsu.get(u); 76 | heap[u] = cyc, seen[u] = -1; 77 | } 78 | } 79 | } 80 | return res; 81 | } 82 | 83 | int main(){ 84 | ios::sync_with_stdio(0); cin.tie(0); 85 | vector edges; 86 | vector> vtup = {{0,1,10},{0,2,10},{0,4,15},{2,3,5},{4,3,5}}; 87 | for(auto [a,b,w] : vtup){ 88 | 89 | Edge e; e.a = a, e.b = b, e.w = w; 90 | edges.emplace_back(e); 91 | } 92 | prin(dmstKACTL(5,1,edges)); 93 | } 94 | -------------------------------------------------------------------------------- /excessive/not_mine/fft.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i) 19 | #define F0R(i,a) FOR(i,0,a) 20 | #define trav(a,x) for (auto& a: x) 21 | #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) 22 | #define R0F(i,a) ROF(i,0,a) 23 | 24 | /* 25 | Implementacao de fft do Benq 26 | Codigo original em: https://github.com/bqi343/USACO/tree/0efee4d738682218c089e25f94281de5acffa98e/Implementations/content/numerical/Polynomials 27 | 28 | Tem versao usando vector e vector (double) 29 | */ 30 | 31 | const int root = 3, MOD = (119<<23)+1; // 998244353 32 | const double PI = acos(-1.0l); 33 | // For p < 2^30 there is also e.g. (5<<25, 3), (7<<26, 3) 34 | /// (479<<21, 3) and (483<<21, 5). Last two are > 10^9. 35 | /* 36 | void genRoots(vmi& roots) { // REPLACE DEF OF MOD 37 | int n = sz(roots); mi r = pow(mi(root),(MOD-1)/n); 38 | roots[0] = 1; FOR(i,1,n) roots[i] = roots[i-1]*r; } 39 | */ 40 | typedef complex cd; 41 | typedef vector vcd; 42 | void genRoots(vcd& roots) { // primitive n-th roots of unity 43 | int n = sz(roots); double ang = 2*PI/n; 44 | /// good way to compute these trig functions more quickly? 45 | F0R(i,n) roots[i] = cd(cos(ang*i),sin(ang*i)); } 46 | 47 | int size(int s) { return s > 1 ? 32-__builtin_clz(s-1) : 0; } 48 | template void fft(vector& a, 49 | const vector& rts, bool inv = 0) { 50 | int n = sz(a); 51 | for (int i = 1, j = 0; i < n; i++) { 52 | int bit = n>>1; for (; j&bit; bit /= 2) j ^= bit; 53 | j ^= bit; if (i < j) swap(a[i],a[j]); 54 | } // sort #s from 0 to n-1 by reverse binary 55 | for (int len = 1; len < n; len *= 2) 56 | for (int i = 0; i < n; i += 2*len) F0R(j,len) { 57 | T u = a[i+j], v = a[i+j+len]*rts[n/2/len*j]; 58 | a[i+j] = u+v, a[i+j+len] = u-v; 59 | } 60 | if (inv) { 61 | reverse(1+all(a)); 62 | T i = T(1)/T(n); trav(x,a) x *= i; 63 | } 64 | } 65 | template vector mul(vector a, vector b) { 66 | if (!min(sz(a),sz(b))) return {}; 67 | int s = sz(a)+sz(b)-1, n = 1< roots(n); genRoots(roots); 69 | a.resize(n), fft(a,roots); b.resize(n), fft(b,roots); 70 | F0R(i,n) a[i] *= b[i]; 71 | fft(a,roots,1); a.resize(s); return a; 72 | } 73 | 74 | int main(){ 75 | ios::sync_with_stdio(0); cin.tie(0); 76 | 77 | vector a = {1,3,2}; 78 | vector b = {1,0,1,1}; 79 | 80 | //precisa ser vector de complex 81 | vector ca, cb; 82 | fr(i,sz(a)) ca.push_back(cd(a[i],0)); 83 | fr(i,sz(b)) cb.push_back(cd(b[i],0)); 84 | 85 | vcd ans = mul(ca,cb); 86 | cout << "ans = "; 87 | fr(i,sz(ans)){ 88 | cout << ans[i].real() << ","; 89 | } 90 | cout << endl; 91 | } 92 | -------------------------------------------------------------------------------- /excessive/numberTheory/fat_invfat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 15 | T fp(T x, long long e) { 16 | T ans(1); 17 | for(; e > 0; e /= 2) { 18 | if(e & 1) ans = ans * x; 19 | x = x * x; 20 | } 21 | return ans; 22 | } 23 | 24 | const ll mod = round(1e9)+7; 25 | struct mb { 26 | mb(int v = 0) : val(v < 0 ? v + mod : v) {} 27 | mb(ll v){ val = (v%mod+mod)%mod; } 28 | int val; 29 | 30 | void operator += (mb o) { *this = *this + o; } 31 | void operator -= (mb o) { *this = *this - o; } 32 | void operator *= (mb o) { *this = *this * o; } 33 | mb operator * (mb o) { return (int)((long long) val * o.val % mod); } 34 | mb operator / (mb o) { return *this * fp(o, mod - 2); } 35 | //bool operator == (mb o) { return val==o.val; } //usar soh para hashes 36 | mb operator + (mb o) { return val + o.val >= mod ? val + o.val - mod : val + o.val; } 37 | mb operator - (mb o) { return val - o.val < 0 ? val - o.val + mod : val - o.val; } 38 | }; 39 | 40 | const int N = 1e5+10; 41 | mb fat[N], invfat[N]; 42 | 43 | mb nck(int n, int k){ 44 | assert(n>=k); 45 | return fat[n]*invfat[k]*invfat[n-k]; 46 | } 47 | 48 | int main(){ 49 | 50 | fat[0] = invfat[0] = mb(1); 51 | for(int i = 1; i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | const int N = 1e6+10; 19 | ll mi[N]; 20 | bool b[N]; 21 | vector primos; 22 | 23 | void build_mi(){ 24 | mi[1] = 1; 25 | for(ll i = 2; iN) break; 33 | b[k] = 1; 34 | if(i%p!=0){ 35 | mi[k] = -mi[i]; 36 | } else{ 37 | mi[k] = 0; 38 | break; 39 | } 40 | } 41 | } 42 | } 43 | 44 | int main(){ 45 | ios::sync_with_stdio(0); cin.tie(0); 46 | 47 | build_mi(); 48 | 49 | fr(i,11){ 50 | prin(i); 51 | prin(mi[i]); 52 | cout << endl; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /excessive/numberTheory/nck_values_bigger_than_mod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; ivdd()+o.vdd())%mod,0); 42 | } 43 | }; 44 | 45 | const int N = 4e5+10; 46 | mb fat[N]; 47 | 48 | mb nck(ll n, ll k){ 49 | mb ans = fat[n]/fat[n-k]/fat[k]; 50 | return ans; 51 | } 52 | 53 | mb val_to_mb(ll x){ 54 | ll qnt = 0; 55 | while(x%mod==0){ 56 | x/=mod; 57 | qnt++; 58 | } 59 | return mb(x%mod,qnt); 60 | } 61 | 62 | int char_to_int[256]; 63 | char int_to_char[3]; 64 | 65 | int main(){ 66 | ios::sync_with_stdio(0); cin.tie(0); 67 | 68 | fat[0] = mb(1,0); 69 | for(int i = 1; i> n; 74 | string s; cin >> s; 75 | vector v(n); 76 | char_to_int['R'] = 0; 77 | char_to_int['B'] = 1; 78 | char_to_int['W'] = 2; 79 | int_to_char[0] = 'R'; 80 | int_to_char[1] = 'B'; 81 | int_to_char[2] = 'W'; 82 | fr(i,n) v[i] = char_to_int[(int)s[i]]; 83 | 84 | mb ans(0,0); 85 | 86 | fr(i,n){ 87 | ans = ans+mb(v[i],0)*nck(n-1,i); 88 | } 89 | 90 | int id = ans.vdd(); 91 | if(n%2==0 and id) id = mod-id; 92 | cout << int_to_char[id] << "\n"; 93 | } 94 | -------------------------------------------------------------------------------- /excessive/numberTheory/paschoal_triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 15 | T fp(T x, long long e) { 16 | T ans(1); 17 | for(; e > 0; e /= 2) { 18 | if(e & 1) ans = ans * x; 19 | x = x * x; 20 | } 21 | return ans; 22 | } 23 | 24 | const ll mod = round(1e9)+7; 25 | struct mb { 26 | mb(int v = 0) : val(v < 0 ? v + mod : v) {} 27 | mb(ll v){ val = (v%mod+mod)%mod; } 28 | int val; 29 | 30 | void operator += (mb o) { *this = *this + o; } 31 | void operator -= (mb o) { *this = *this - o; } 32 | void operator *= (mb o) { *this = *this * o; } 33 | mb operator * (mb o) { return (int)((long long) val * o.val % mod); } 34 | mb operator / (mb o) { return *this * fp(o, mod - 2); } 35 | //bool operator == (mb o) { return val==o.val; } //usar soh para hashes 36 | mb operator + (mb o) { return val + o.val >= mod ? val + o.val - mod : val + o.val; } 37 | mb operator - (mb o) { return val - o.val < 0 ? val - o.val + mod : val - o.val; } 38 | }; 39 | 40 | const int N = 1e3+10; 41 | mb nck[N][N]; 42 | 43 | int main(){ 44 | nck[0][0] = 1; 45 | for(int i = 1; i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | //solves https://codeforces.com/contest/1717/problem/E 19 | 20 | const int N = 1e5+10; 21 | ll phi[N]; 22 | 23 | void calc_phi(){ 24 | for(int i = 1; i divi[N]; 35 | 36 | void calc_divi(){ 37 | for(int i = 1; i 45 | T fp(T x, long long e) { 46 | T ans(1); 47 | for(; e > 0; e /= 2) { 48 | if(e & 1) ans = ans * x; 49 | x = x * x; 50 | } 51 | return ans; 52 | } 53 | 54 | const ll mod = round(1e9)+7; 55 | struct mb { 56 | mb(int v = 0) : val(v < 0 ? v + mod : v) {} 57 | mb(ll v){ val = (v%mod+mod)%mod; } 58 | int val; 59 | 60 | void operator += (mb o) { *this = *this + o; } 61 | void operator -= (mb o) { *this = *this - o; } 62 | void operator *= (mb o) { *this = *this * o; } 63 | mb operator * (mb o) { return (int)((long long) val * o.val % mod); } 64 | mb operator / (mb o) { return *this * fp(o, mod - 2); } 65 | //bool operator == (mb o) { return val==o.val; } //usar soh para hashes 66 | mb operator + (mb o) { return val + o.val >= mod ? val + o.val - mod : val + o.val; } 67 | mb operator - (mb o) { return val - o.val < 0 ? val - o.val + mod : val - o.val; } 68 | }; 69 | 70 | int main(){ 71 | ios::sync_with_stdio(0); cin.tie(0); 72 | 73 | calc_phi(); 74 | calc_divi(); 75 | 76 | int n; cin >> n; 77 | 78 | mb ans = 0; 79 | 80 | for(int c = 1; c<=n-2; c++){ 81 | int k = n-c; 82 | for(auto &d : divi[k]){ 83 | int dinv = k/d; 84 | if(d==k) continue; 85 | ans += mb(phi[dinv]*lcm(c,d)); 86 | } 87 | } 88 | cout << ans.val << "\n"; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /geometry/Convex_Hull/CHT/cht.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i eh pra deixar container trasnparente e ter 2 tipos de comparador pro multiset 21 | struct Cht : multiset> { 22 | const ll inf = LLONG_MAX; 23 | ll div(ll a, ll b){ 24 | return a/b - ( (a^b)<0 and a%b); 25 | } 26 | //retas x e y tem inclinacoes vizinhas no container, mx <= my. Seta-se o valor de p de x, ou seja, onde intersecta com y, e retorna true se a reta x esta acima de y inteiramente no intervalo em q y era a maxima, ou seja, y deve ser apagada 27 | bool bad2(iterator x, iterator y){ 28 | if(y==end()){ 29 | x->p = inf; 30 | return 0; 31 | } 32 | //se tem inclinacao igual e o x eh pior, marco ele como -inf, assim, msm q x continue no set, ele nao funciona pra ngm cujo x>=-inf, ou seja, nenhum x msm! 33 | if(x->m==y->m) x->p = (x->k>y->k)? inf : -inf; 34 | else x->p = div(x->k-y->k, y->m-x->m); 35 | return x->p>=y->p; 36 | } 37 | //tenta adicionar a reta y=m*x+k ao CHTmaximo e o ajusto caso necessario 38 | void add(ll m, ll k){ 39 | //nao da pra usar emplace(m,k,0) pois nao define construtor para Line 40 | auto z = insert({m,k,0}), y = z++, x = y; 41 | while(bad2(y,z)) z = erase(z); 42 | if(x!=begin() and bad2(--x,y)) bad2(x,erase(y)); //no original se usa y=erase(y), mas nao precisa pois y sera redefinido na proxima linha msm 43 | while((y=x)!=begin() and (--x)->p >= y->p) bad2(x,erase(y)); 44 | } 45 | //retorna o valor do qual do convex hull de maximo na coordenada x 46 | ll query(ll x){ 47 | assert(!empty()); 48 | auto l = *lower_bound(x); 49 | return x*l.m + l.k; 50 | } 51 | }; 52 | 53 | int main(){ 54 | ll n; 55 | cin >> n; 56 | 57 | vector> v; 58 | fr(i,n){ 59 | ll x, y, z; 60 | scanf("%lld%lld%lld", &x, &y, &z); 61 | v.emplace_back(x,y,z); 62 | } 63 | 64 | sort(all(v)); 65 | 66 | Cht cht; 67 | 68 | ll ans = 0; 69 | 70 | cht.add(0,0); 71 | for(auto &tup : v){ 72 | ll x, y, a; 73 | tie(x,y,a) = tup; 74 | ll dp = cht.query(y) + x*y - a; 75 | ans = max(ans,dp); 76 | cht.add(-x,dp); 77 | } 78 | 79 | cout << ans << endl; 80 | } 81 | -------------------------------------------------------------------------------- /geometry/Convex_Hull/CHT/cht_wihtout_less.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i> that makes the code cleaner but may nto compile on older judges 19 | 20 | //solves https://www.urionlinejudge.com.br/judge/en/problems/view/1282 21 | 22 | bool Q; 23 | struct Line { 24 | mutable ll k, m, p; 25 | bool operator<(const Line& o) const { 26 | return Q ? p < o.p : k < o.k; 27 | } 28 | }; 29 | 30 | struct Cht : multiset { 31 | // (for doubles, use inf = 1/.0, div(a,b) = a/b) 32 | const ll inf = LLONG_MAX; 33 | ll div(ll a, ll b) { // floored division 34 | return a / b - ((a ^ b) < 0 && a % b); } 35 | bool isect(iterator x, iterator y) { 36 | if (y == end()) { x->p = inf; return false; } 37 | if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; 38 | else x->p = div(y->m - x->m, x->k - y->k); 39 | return x->p >= y->p; 40 | } 41 | void add(ll k, ll m) { 42 | auto z = insert({k, m, 0}), y = z++, x = y; 43 | while (isect(y, z)) z = erase(z); 44 | if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); 45 | while ((y = x) != begin() && (--x)->p >= y->p) 46 | isect(x, erase(y)); 47 | } 48 | ll query(ll x) { 49 | assert(!empty()); 50 | Q = 1; auto l = *lower_bound({0,0,x}); Q = 0; 51 | return l.k * x + l.m; 52 | } 53 | }; 54 | 55 | int main(){ 56 | //ios::sync_with_stdio(0); cin.tie(0); 57 | int n, k; 58 | while(scanf("%d%d", &n, &k)!=EOF){ 59 | vector> dp(n+1,vector(k+1)); 60 | vector x(n+1), w(n+1); 61 | 62 | vector peso(n+1), pt(n+1); 63 | 64 | vector vc(k+1); 65 | 66 | for(int i = 1; i<=n; i++){ 67 | scanf("%lld", &x[i]); 68 | scanf("%lld", &w[i]); 69 | } 70 | 71 | for(int i = 1; i<=n; i++){ 72 | peso[i] = peso[i-1]+w[i]; 73 | pt[i] = pt[i-1] + (x[i]-x[i-1])*peso[i-1]; 74 | } 75 | 76 | for(int i = n; i>=1; i--){ 77 | for(int can_use = k; can_use>=1; can_use--){ 78 | int tam_suf = n - i + 1; 79 | if(can_use>=tam_suf){ 80 | dp[i][can_use] = 0; 81 | } else if(can_use==1){ 82 | dp[i][can_use] = pt[n]-pt[i-1] - (x[n]-x[i-1])*peso[i-1]; 83 | } else{ 84 | dp[i][can_use] = -vc[can_use-1].query(peso[i-1]) + x[i-1]*peso[i-1] - pt[i-1]; 85 | } 86 | vc[can_use].add(x[i-1], -pt[i-1] - dp[i][can_use]); 87 | } 88 | } 89 | 90 | printf("%lld\n", dp[1][k]); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /geometry/Convex_Hull/CHT/pareto_hull_2d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; ip.y; 15 | return x{ 37 | bool bad(iterator b){ 38 | auto c = next(b); 39 | if(c==end()) return 0; 40 | if(b==begin()) return b->x < c->x and b->y < c->y; 41 | auto a = prev(b); 42 | return vet(menos(*b,*a),menos(*c,*a))>0; 43 | } 44 | void insert(ll x, ll y){ 45 | auto b = emplace(x,y); 46 | if(bad(b)){ 47 | erase(b); 48 | return; 49 | } 50 | while(next(b)!=end() and bad(next(b))) erase(next(b)); 51 | while(b!=begin() and bad(prev(b))) erase(prev(b)); 52 | } 53 | }; 54 | 55 | //solves https://codeforces.com/gym/102302/problem/I 56 | 57 | int main(){ 58 | int n; 59 | cin >> n; 60 | 61 | HullD hd; 62 | 63 | fr(i,n){ 64 | ll x, y; 65 | scanf("%lld%lld", &x, &y); 66 | 67 | hd.insert(x,y); 68 | 69 | printf("%d\n", i+1-(int)hd.size()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /geometry/Convex_Hull/convex_hull.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define all(v) (v).begin(),(v).end() 5 | #define sz(v) (int)(v.size()) 6 | #define fr(i,n) for(int i = 0;i pll; 10 | 11 | /* 12 | finds convex hull of set of pointes 13 | area and perimeter 14 | solves this problem: 15 | https://codeforces.com/group/3qadGzUdR4/contest/101706/problem/G 16 | */ 17 | 18 | struct pt{ 19 | ll x, y; 20 | int id; 21 | pt(){} 22 | pt(ll xx, ll yy){ 23 | x = xx, y = yy; 24 | } 25 | 26 | pt operator -(pt p2){ 27 | return pt(x-p2.x,y-p2.y); 28 | } 29 | 30 | ll operator ^(pt p2){ 31 | return x*p2.y-y*p2.x; 32 | } 33 | 34 | double mod(){ 35 | return hypot(x,y); 36 | } 37 | }; 38 | 39 | bool operator <(const pt &p1, const pt &p2){ 40 | return pll(p1.x,p1.y) mch(vector v){ 57 | sort(all(v)); 58 | v.resize(unique(all(v))-v.begin()); 59 | 60 | vector ans; 61 | 62 | fr(cor,2){ 63 | vector h; 64 | fr(i,sz(v)){ 65 | while(sz(h)>=2){ 66 | pt v1 = h.back()-h[h.size()-2]; 67 | pt v2 = v[i]-h.back(); 68 | if( (v1^v2) > 0 ) break; 69 | h.pop_back(); 70 | } 71 | h.emplace_back(v[i]); 72 | } 73 | fr(i,sz(h)-1){ 74 | ans.emplace_back(h[i]); 75 | } 76 | reverse(all(v)); 77 | } 78 | 79 | return ans; 80 | } 81 | 82 | int main(){ 83 | ll n; 84 | cin >> n; 85 | vector v; 86 | fr(i,n){ 87 | pt p; 88 | p.id = i+1; 89 | scanf("%lld%lld", &p.x, &p.y); 90 | v.emplace_back(p); 91 | } 92 | 93 | v = mch(v); 94 | n = v.size(); 95 | 96 | double per = 0; 97 | fr(i,n) per += (v[(i+1)%n]-v[i]).mod(); 98 | ll area = 0; 99 | for(int i = 1; i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i pll; 10 | 11 | struct pt{ 12 | ll x, y; 13 | pt(){} 14 | pt(ll xx, ll yy){ 15 | x = xx, y = yy; 16 | } 17 | 18 | bool operator == (pt p2){ 19 | return x==p2.x and y==p2.y; 20 | } 21 | }; 22 | 23 | /* 24 | Function that checks for intersection between two lines p1-p2 and p3-p4 in 2d 25 | 26 | You can alter the function to receive the paremeters of the line: a*x+b*y=c 27 | 28 | Returns: 29 | 0: No intersection, 1: One intersecao, 2: inifinite intersections 30 | 31 | "Pintersection" is the pair of pairs "returned" by reference that represents the irrecductible fraction 32 | of a intersection (if any), that is for examle x=3/4 and y=-1/1 33 | 34 | Watch out for overflow, |x|,|y|<=5e5 35 | */ 36 | int line_intersection(pt p1, pt p2, pt p3, pt p4, pair &pintersection){ 37 | if(p1==p2 or p3==p4) return 0; //assert(0); //? 38 | ll a1, a2, b1, b2, c1, c2; 39 | fr(cor,2){ 40 | a1 = p1.y-p2.y, b1 = p2.x-p1.x; 41 | c1 = p1.x*a1 + p1.y*b1; 42 | swap(p1,p3); 43 | swap(p2,p4); 44 | swap(a1,a2); 45 | swap(b1,b2); 46 | swap(c1,c2); 47 | } 48 | ll det = a1*b2-a2*b1; 49 | 50 | if(!det){ 51 | if(p1.x*a2+p1.y*b2!=c2) return 0; 52 | pintersection = {{p1.x,1},{p1.y,1}}; //pintersection = {p1.x,p1.y}; 53 | return 2; 54 | } 55 | //For pair double: 56 | //pintersection = {(1.0l*c1*b2-1.0l*b1*c2)/det,(1.0l*a1*c2-1.0l*a2*c1)/det}; 57 | //return 1; 58 | 59 | pll x, y; 60 | ll num, div, g; 61 | fr(cor,2){ 62 | if(cor) num = a1*c2-a2*c1; //possible overflow here (8*|x|^3) 63 | else num = c1*b2-b1*c2; 64 | div = det; 65 | g = __gcd(abs(num),abs(div)); 66 | num/=g, div/=g; 67 | if(div<0) div*=-1, num*=-1; 68 | x = {num,div}; 69 | swap(x,y); 70 | } 71 | pintersection = {x,y}; 72 | return 1; 73 | } 74 | 75 | 76 | //solves problem: https://www.spoj.com/problems/TAP2013A/ 77 | 78 | // variacao CF: http://codeforces.com/contest/993/problem/C 79 | 80 | map,pair>,int> mp; 81 | map,int> mp2; 82 | 83 | const int N = 1e3+10; 84 | pt v[N]; 85 | ll n; 86 | 87 | ll f(ll x){ 88 | ll lo = 2, hi = n; 89 | while(lo=x){ 92 | hi = mid; 93 | } else{ 94 | lo = mid+1; 95 | } 96 | } 97 | if(lo*(lo-1)/2!=x){ 98 | exit(0); 99 | } 100 | return lo-1; 101 | } 102 | 103 | int main(){ 104 | cin >> n; 105 | fr(i,n){ 106 | ll x, y; 107 | scanf("%lld%lld", &x, &y); 108 | v[i] = {x,y}; 109 | 110 | } 111 | pair p1, p2; 112 | fr(i,n){ 113 | fr(j,i){ 114 | if(line_intersection(pt(0,0),pt(1,0),v[i],v[j],p1)){ 115 | line_intersection(pt(0,1),pt(1,1),v[i],v[j],p2); 116 | mp[{p1,p2}]++; 117 | } 118 | } 119 | } 120 | set s; 121 | s.insert(n); 122 | for(auto &it : mp){ 123 | mp2[it.fi.fi] += f(it.se); 124 | } 125 | for(auto it : mp2){ 126 | s.insert(n-it.se); 127 | } 128 | cout << s.size() << endl; 129 | return 0; 130 | } 131 | 132 | //SIMPLE CHECK 133 | 134 | /* 135 | int main(){ 136 | //FILE_IN FILE_OUT 137 | 138 | pair p; 139 | if(line_intersection(pt(0,0),pt(1,1),pt(0,1),pt(1,0),p)){ 140 | puts("Retas tem intersecao:"); 141 | cout << "X = " << p.fi.fi << "/" << p.fi.se << ", Y = " << p.se.fi << "/" << p.se.se << endl; 142 | } else{ 143 | puts("Retas paralelas"); 144 | } 145 | 146 | return 0; 147 | } 148 | */ 149 | -------------------------------------------------------------------------------- /geometry/Lines_and_Segments/line_intersection_pt_double.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i> a >> b; 17 | x = a, y = b; 18 | } 19 | void print(){ 20 | cout << (int)round(x) << " " << (int)round(y) << "\n"; 21 | } 22 | pt operator-(pt &p2){ 23 | return pt(x-p2.x,y-p2.y); 24 | } 25 | double mod2(){ 26 | return x*x+y*y; 27 | } 28 | double mod(){ 29 | return sqrt(this->mod2()); 30 | } 31 | //----------- 32 | }; 33 | 34 | double eps = 1e-7; 35 | int sign(double x){ 36 | if(x>eps) return 1; 37 | if(x<-eps) return -1; 38 | return 0; 39 | } 40 | 41 | bool eq(double x, double y){ 42 | return sign(x-y)==0; 43 | } 44 | 45 | int line_intersection(pt p1, pt p2, pt p3, pt p4, pt &pans){ 46 | double a1, a2, b1, b2, c1, c2; 47 | fr(cor,2){ 48 | a1 = p1.y-p2.y, b1 = p2.x-p1.x; 49 | c1 = p1.x*a1 + p1.y*b1; 50 | swap(p1,p3); 51 | swap(p2,p4); 52 | swap(a1,a2); 53 | swap(b1,b2); 54 | swap(c1,c2); 55 | } 56 | double det = a1*b2-a2*b1; 57 | if(sign(det)==0){ 58 | if(eq(p1.x*a2+p1.y*b2,c2)) return 0; 59 | pans = p1; 60 | return 2; 61 | } 62 | pans = pt((1.0l*c1*b2-1.0l*b1*c2)/det,(1.0l*a1*c2-1.0l*a2*c1)/det); 63 | return 1; 64 | } 65 | 66 | //----------------------------------------------- 67 | 68 | //rotaciona sentido anti horario 69 | pt rot(pt p, double teta){ 70 | return pt(p.x*cos(teta)-p.y*sin(teta),p.y*cos(teta)+p.x*sin(teta)); 71 | } 72 | 73 | pt forca_mod(pt p, long double m){ 74 | long double cm = p.mod(); 75 | return pt(p.x*m/cm,p.y*m/cm); 76 | } 77 | 78 | double pi = acos(-1.0l); 79 | 80 | pair get_pts(pt p1, pt p2){ 81 | pt vd = p2-p1; 82 | vd = forca_mod(vd,vd.mod()/2); 83 | pt pm = p2-vd; 84 | vd = rot(vd,pi/2); 85 | pt pc = pm-vd; 86 | return {pm,pc}; 87 | } 88 | 89 | int main(){ 90 | ios::sync_with_stdio(0); cin.tie(0); 91 | int n; cin >> n; 92 | vector v(n); 93 | fr(i,n) v[i].read(); 94 | 95 | for(int i = 2; i 2 | using namespace std; 3 | 4 | #define FILE_IN freopen("kotlin.in", "r", stdin); 5 | #define FILE_OUT freopen("kotlin.out", "w", stdout); 6 | 7 | #define fr(i,n) for(int i=0;i pii; 21 | typedef pair pll; 22 | 23 | const long double PI = acos(-1.0l); 24 | const ll MOD = 1e9+7; 25 | 26 | //LLONG_MAX 27 | //-DBL_MAX 28 | 29 | bool debug = 1; 30 | #define printa(a) cout << #a << " = " << (a) << endl 31 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 32 | #define soprin(a) if(debug) cout << (a) 33 | #define ppal(a) if(debug) cout << #a << endl 34 | #define prinsep if(debug) cout << "------" << endl 35 | #define cendl if(debug) cout << endl 36 | #define prinpar(p) if(debug) cout << #p << ".fi=" << (p).fi << " " << #p << ".se=" << (p).se << endl 37 | #define print(tup) if(debug) cout << #tup << " = {" << get(tup,0) << ", " << get(tup,1) << ", " << get(tup,2) << "}\n" 38 | #define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;} 39 | 40 | const int N = -1; 41 | 42 | struct pt{ 43 | ll x, y; 44 | pt(){} 45 | pt(ll xx, ll yy){ 46 | x = xx, y = yy; 47 | } 48 | 49 | pt operator - (pt p2){ 50 | return pt(x-p2.x,y-p2.y); 51 | } 52 | ll operator * (pt p2){ 53 | return x*p2.x + y*p2.y; 54 | } 55 | 56 | ll operator ^(pt p2){ 57 | return x*p2.y-y*p2.x; 58 | } 59 | 60 | long double mod(){ 61 | return sqrt((long double)(x*x+y*y)); 62 | } 63 | }; 64 | 65 | //checa se ponto esta dentro do segmento 66 | bool inptseg(pt a1, pt b1, pt b2){ 67 | pt v1 = (a1-b1), v2 = (b2-b1); 68 | if(v1^v2) return 0; 69 | v1 = b1-a1, v2 = b2-a1; 70 | return (v1*v2)<=0; 71 | } 72 | 73 | //checa se segmentos intersectam (bordas inclusas) 74 | bool seg_intersect(pt a1, pt a2, pt b1, pt b2){ 75 | fr(i,2){ 76 | fr(j,2){ 77 | if(inptseg(a1,b1,b2)) return 1; 78 | swap(a1,a2); 79 | } 80 | swap(a1,b1); 81 | swap(a2,b2); 82 | } 83 | 84 | fr(cor,2){ 85 | //o resultado eh inteiro, entao nao preciso me preocupar com precisão, apenas overflow 86 | pt v1 = (a1-b1), v2 = (a2-b1), vs = (b2-b1); 87 | if( 1.0l*(v1^vs)*(v2^vs) >= -0.5 ) return 0; 88 | swap(a1,b1), swap(a2,b2); 89 | } 90 | 91 | return 1; 92 | } 93 | 94 | int main(){ 95 | //FILE_IN FILE_OUT 96 | 97 | //segmentos a1-a2 e b1-b2; 98 | 99 | pt a1(-1,-1), a2(1,1), b1(-1,1), b2(1,-1); 100 | 101 | prin(seg_intersect(a1,a2,b1,b2)); 102 | 103 | return 0; 104 | } 105 | 106 | -------------------------------------------------------------------------------- /geometry/Sort_by_Angle/sort_pt_by_angle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i> x >> y; 22 | } 23 | }; 24 | 25 | ll operator ^(const pt &a, const pt &b){ 26 | return a.x*b.y-a.y*b.x; 27 | } 28 | 29 | int quad(const pt& p){ 30 | int ans = 0; 31 | if(p.x<0) ans++; 32 | if(p.y<0) ans^=1, ans+=2; 33 | return ans; 34 | } 35 | 36 | bool operator ==(const pt &a, const pt &b){ 37 | return quad(a)==quad(b) and (a^b)==0; 38 | } 39 | 40 | // Vector by angle comparator - return 0 if they are equal 41 | bool operator <(const pt &a, const pt &b){ 42 | if(quad(a)==quad(b)){ 43 | return (a^b)>0; 44 | } 45 | return quad(a)> n; 57 | vector v(n); 58 | fr(i,n) v[i].read(); 59 | 60 | int maxf = 0; 61 | fr(no,n){ 62 | vector vo; 63 | fr(i,n){ 64 | if(i==no) continue; 65 | vo.emplace_back(v[i]-v[no]); 66 | } 67 | sort(all(vo)); 68 | int i = 0; 69 | while(i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i::value) cout << x << " " << y << "\n"; 29 | else cout << fixed << setprecision(10) << x << " " << y << "\n"; 30 | } 31 | long double mod(){ 32 | return sqrtl(sq(x)+sq(y)); 33 | } 34 | template 35 | pt operator -(pt b){ 36 | return pt(x-b.x,y-b.y); 37 | } 38 | template 39 | pt operator +(pt b){ 40 | return pt(x+b.x,y+b.y); 41 | } 42 | //produto vetorial 43 | T operator ^(pt b) const{ 44 | return x*b.y-y*b.x; 45 | } 46 | //produto escalar 47 | T operator *(pt b) const{ 48 | return x*b.x+y*b.y; 49 | } 50 | int quad() const{ 51 | int ans = 0; 52 | if(x<0) ans++; 53 | if(y<0) ans^=1, ans+=2; 54 | return ans; 55 | } 56 | bool operator <(pt b) const{ 57 | if(this->quad()==b.quad()){ 58 | return ((*this)^b)>0; 59 | } 60 | return this->quad() add_mod(pt p, double add){ 65 | double cm = p.mod(); 66 | double m = cm+add; 67 | return pt(p.x*m/cm,p.y*m/cm); 68 | } 69 | 70 | int main(){ 71 | ios::sync_with_stdio(0); cin.tie(0); 72 | pt a, b, c; 73 | a.read(); 74 | b.read(); 75 | c.read(); 76 | pt vab = b-a, vac = c-a, vcb = b-c, vca = a-c; 77 | if(vac*vab<=0 or vca*vcb==0){ 78 | cout << "YES\n"; 79 | return 0; 80 | } 81 | 82 | double add; 83 | if(vca*vcb>0){ 84 | double cos_alfa = (vca*vcb)/(vca.mod()*vcb.mod()); 85 | double d = cos_alfa*vcb.mod(); 86 | add = -2*d; 87 | if(2*d>=vac.mod()){ 88 | cout << "YES\n"; 89 | return 0; 90 | } 91 | } else{ 92 | double cos_alfa = (vac*vcb)/(vac.mod()*vcb.mod()); 93 | double d = cos_alfa*vcb.mod(); 94 | add = 2*d; 95 | } 96 | 97 | pt vac_double = add_mod(vac,add); 98 | pt ans = vac_double+a; 99 | 100 | cout << "NO\n"; 101 | a.print(); 102 | b.print(); 103 | ans.print(); 104 | } 105 | -------------------------------------------------------------------------------- /geometry/rot_and_forca_mod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 13 | T sq(T x){ 14 | return x*x; 15 | } 16 | 17 | const long double pi = acos(-1.0l); 18 | 19 | struct pt{ 20 | long double x, y; 21 | pt(){} 22 | pt(long double _x, long double _y){ 23 | x = _x, y = _y; 24 | } 25 | long double mod(){ 26 | return sqrt(sq(x)+sq(y)); 27 | } 28 | pt operator -(pt b){ 29 | return pt(x-b.x,y-b.y); 30 | } 31 | pt operator +(pt b){ 32 | return pt(x+b.x,y+b.y); 33 | } 34 | long double operator ^(pt b) const{ 35 | return x*b.y-y*b.x; 36 | } 37 | int quad() const{ 38 | int ans = 0; 39 | if(x<0) ans++; 40 | if(y<0) ans^=1, ans+=2; 41 | return ans; 42 | } 43 | bool operator <(pt b) const{ 44 | if(this->quad()==b.quad()){ 45 | return ((*this)^b)>0; 46 | } 47 | return this->quad() auxin 4 | ./cr A.cpp < auxin 5 | while true; 6 | do 7 | echo $RANDOM > auxin 8 | ./a.out < auxin 9 | done 10 | ------------------------------ 11 | 12 | #O script acima deve ser usado para rodar um programas varias vezes para achar algum erro. Usar da seguinte forma: 13 | -PRINTAR SEED NO PROGRAMA EM Q SE BUSCA ERRO (SEED É RECEBIDA PELA VARIÁVEL BASH $RANOM) 14 | -CRIAR GERADOR DE INPUT NO PROPRIO PROGRAMA A PARTIR DA SEED 15 | -FAZER CHECKER, SE DER RUIM, GERAR LOOP INFINITO 16 | -USAR O SIMPLES SCRIPT ACIMA DE RODAR PROGRAMA VÁRIAS VEZES 17 | 18 | SE DER RUIM, ULTIMA SEED PRINTADA CAUSA O ERRO 19 | 20 | Explicação comandos script: 21 | #criando arquivo chamado auxin com um valor randomico 22 | echo $RANDOM > auxin 23 | 24 | #seedando o proprio RANDOM, nao necessario, mas se quiser por no inicio do script 25 | RANDOM=123 26 | 27 | No programa lembrar de 28 | ---------------------------- 29 | int seed; cin >> seed; 30 | prin(seed); 31 | srand(seed); 32 | ---------------------------- 33 | 34 | 35 | Tmbm lembrar de substituir asserts de forma facil: 36 | 37 | ---------------------------- 38 | #define assert meu_assert 39 | void assert(bool x){ 40 | if(!x) cout << "erro" << endl; 41 | while(!x) continue; 42 | } 43 | ---------------------------- 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | ----------------------------------------------------- 52 | Antiga forma de gerar random: 53 | Gerar seed no arquivo usando mt19937 (usando time(0) como seed soh vai mudar seed a cada segundo e demorar para achar erro): 54 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 55 | int seed = uniform_int_distribution(0, INT_MAX)(rng); 56 | prin(seed); 57 | srand(seed); 58 | 59 | //outro uso randomico: random_shuffle(all(v),rng); 60 | -------------------------------------------------------------------------------- /levar_icpc/compare_script.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i a1"); 73 | system("./prog2 < input > a2"); 74 | 75 | pfile1 = fopen("a1","r"); 76 | pfile2 = fopen("a2","r"); 77 | 78 | ///* 79 | ll ans1, ans2; 80 | fscanf(pfile1,"%lld", &ans1); 81 | fscanf(pfile2,"%lld", &ans2); 82 | //*/ 83 | 84 | //Le e comparar vetor inves de ll 85 | /* 86 | vector ans1(n), ans2(n); 87 | //fscanf(pfile1,"%*d"); //caso no output seja printado n 88 | //fscanf(pfile2,"%*d"); 89 | fr(i,n){ 90 | fscanf(pfile1,"%lld", &ans1[i]); 91 | } 92 | fr(i,n){ 93 | fscanf(pfile2,"%lld", &ans2[i]); 94 | } 95 | //*/ 96 | 97 | fclose(pfile1); 98 | fclose(pfile2); 99 | 100 | if(ans1!=ans2){ 101 | prin(ii); 102 | //Se parou eh pq input esta com o arquivo que quebra 103 | goto end; 104 | } 105 | 106 | } 107 | } 108 | 109 | end: 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /levar_icpc/flags_compilacao: -------------------------------------------------------------------------------- 1 | Criar arquivos chamados cr com os seguintes conteudos para compilar e rodar arquivos: 2 | 3 | Conteudo To run safe (cr): 4 | g++ -std=c++17 -Wshadow -fsanitize=address -D_GLIBCXX_DEBUG -W -Wall -Wextra $1 && ./a.out 5 | 6 | Conteudo To run fast (crf): 7 | g++ -std=c++17 -O2 -w $1 && ./a.out 8 | 9 | Lembrar de tornar executavel: 10 | chmod +x cr 11 | 12 | Para usar fazer: 13 | ./cr A.cpp 14 | Ou: 15 | ./cr A.cpp < input 16 | -------------------------------------------------------------------------------- /levar_icpc/semantics_to_remember: -------------------------------------------------------------------------------- 1 | Bitset: 2 | 3 | bitset e vector ambos usam compressao de 32/64 vezes 4 | "bitset implementa funcoes de contar qnts tem setados e bitwise operators, mas posso fazer map 5 | de vector bool pq, para ele, o comparision esta definido, bitset nao, vector bool tmbm tem tamanho "dinamico" 6 | 7 | bitset<500> reg[N] 8 | bitset<1000> a, b, c; 9 | c = a&b; 10 | a.any(), b.none(), c.all(), a[13]; 11 | reg[i].flip(), reg[i].count(), reg[i].reset(); 12 | 13 | map,int> mp; 14 | vector vec; 15 | vec.flip(); 16 | 17 | LARGE PRIMES (desconhecidos - bom pra hash) 18 | 19 | cabe int 20 | 1.5e9+1 21 | 1163926061 22 | 23 | long long 24 | 1e15+37 25 | -------------------------------------------------------------------------------- /levar_icpc/template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | int main(){ 19 | ios::sync_with_stdio(0); cin.tie(0); 20 | } 21 | -------------------------------------------------------------------------------- /math/Polynomials/fft_benq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i) 19 | #define F0R(i,a) FOR(i,0,a) 20 | #define trav(a,x) for (auto& a: x) 21 | #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) 22 | #define R0F(i,a) ROF(i,0,a) 23 | 24 | /* 25 | Implementacao de fft do Benq 26 | Codigo original em: https://github.com/bqi343/USACO/tree/0efee4d738682218c089e25f94281de5acffa98e/Implementations/content/numerical/Polynomials 27 | 28 | Tem versao usando vector e vector (double) 29 | */ 30 | 31 | const int root = 3, MOD = (119<<23)+1; // 998244353 32 | const double PI = acos(-1.0l); 33 | // For p < 2^30 there is also e.g. (5<<25, 3), (7<<26, 3) 34 | /// (479<<21, 3) and (483<<21, 5). Last two are > 10^9. 35 | /* 36 | void genRoots(vmi& roots) { // REPLACE DEF OF MOD 37 | int n = sz(roots); mi r = pow(mi(root),(MOD-1)/n); 38 | roots[0] = 1; FOR(i,1,n) roots[i] = roots[i-1]*r; } 39 | */ 40 | typedef complex cd; 41 | typedef vector vcd; 42 | void genRoots(vcd& roots) { // primitive n-th roots of unity 43 | int n = sz(roots); double ang = 2*PI/n; 44 | /// good way to compute these trig functions more quickly? 45 | F0R(i,n) roots[i] = cd(cos(ang*i),sin(ang*i)); } 46 | 47 | int size(int s) { return s > 1 ? 32-__builtin_clz(s-1) : 0; } 48 | template void fft(vector& a, 49 | const vector& rts, bool inv = 0) { 50 | int n = sz(a); 51 | for (int i = 1, j = 0; i < n; i++) { 52 | int bit = n>>1; for (; j&bit; bit /= 2) j ^= bit; 53 | j ^= bit; if (i < j) swap(a[i],a[j]); 54 | } // sort #s from 0 to n-1 by reverse binary 55 | for (int len = 1; len < n; len *= 2) 56 | for (int i = 0; i < n; i += 2*len) F0R(j,len) { 57 | T u = a[i+j], v = a[i+j+len]*rts[n/2/len*j]; 58 | a[i+j] = u+v, a[i+j+len] = u-v; 59 | } 60 | if (inv) { 61 | reverse(1+all(a)); 62 | T i = T(1)/T(n); trav(x,a) x *= i; 63 | } 64 | } 65 | template vector mul(vector a, vector b) { 66 | if (!min(sz(a),sz(b))) return {}; 67 | int s = sz(a)+sz(b)-1, n = 1< roots(n); genRoots(roots); 69 | a.resize(n), fft(a,roots); b.resize(n), fft(b,roots); 70 | F0R(i,n) a[i] *= b[i]; 71 | fft(a,roots,1); a.resize(s); return a; 72 | } 73 | 74 | int main(){ 75 | ios::sync_with_stdio(0); cin.tie(0); 76 | 77 | vector a = {1,3,2}; 78 | vector b = {1,0,1,1}; 79 | 80 | //precisa ser vector de complex 81 | vector ca, cb; 82 | fr(i,sz(a)) ca.push_back(cd(a[i],0)); 83 | fr(i,sz(b)) cb.push_back(cd(b[i],0)); 84 | 85 | vcd ans = mul(ca,cb); 86 | cout << "ans = "; 87 | fr(i,sz(ans)){ 88 | cout << ans[i].real() << ","; 89 | } 90 | cout << endl; 91 | } 92 | -------------------------------------------------------------------------------- /math/Polynomials/fwht.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //solves https://codeforces.com/contest/1523/problem/D 5 | 6 | template 7 | vector FWHT(vector a, const bool inv = false) { 8 | int n = (int) a.size(); 9 | for(int len = 1; len < n; len += len) { 10 | for(int i = 0; i < n; i += 2 * len) { 11 | for(int j = 0; j < len; j++) { 12 | auto u = a[i + j], v = a[i + j + len]; 13 | if(ch == '^') { 14 | a[i + j] = u + v; 15 | a[i + j + len] = u - v; 16 | } 17 | if(ch == '|') { 18 | if(!inv) { 19 | a[i + j + len] += a[i + j]; 20 | } else { 21 | a[i + j + len] -= a[i + j]; 22 | } 23 | } 24 | if(ch == '&') { 25 | if(!inv) { 26 | a[i + j] += a[i + j + len]; 27 | } else { 28 | a[i + j] -= a[i + j + len]; 29 | } 30 | } 31 | } 32 | } 33 | } 34 | if(ch == '^' && inv) { 35 | for(int i = 0; i < n; i++) { 36 | a[i] = a[i] / n; 37 | } 38 | } 39 | return a; 40 | } 41 | 42 | mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count()); 43 | 44 | int main() { 45 | ios_base::sync_with_stdio(false); cin.tie(NULL); 46 | int n, m, p; 47 | cin >> n >> m >> p; 48 | vector a(n); 49 | for(int i = 0; i < n; i++) { 50 | cin >> a[i]; 51 | } 52 | int ans = 0; 53 | string wtf(m, '0'); 54 | for(int rep = 0; rep < 20; rep++) { 55 | vector f(1 << p, 0); 56 | int id = rng() % n; 57 | vector pos; 58 | for(int i = 0; i < m; i++) { 59 | if(a[id][i] == '1') { 60 | pos.push_back(i); 61 | } 62 | } 63 | 64 | for(int i = 0; i < n; i++) { 65 | int mask = 0; 66 | for(int j = 0; j < (int) pos.size(); j++) { 67 | if(a[i][pos[j]] == '1') { 68 | mask |= 1 << j; 69 | } 70 | } 71 | f[mask]++; 72 | } 73 | f = FWHT<'&'>(f); 74 | for(int i = 0; i < (1 << p); i++) { 75 | if(2 * f[i] >= n) { 76 | int bits = 0; 77 | for(int j = 0; j < p; j++) { 78 | if(i & (1 << j)) { 79 | bits++; 80 | } 81 | } 82 | if(ans < bits) { 83 | ans = bits; 84 | wtf = string(m, '0'); 85 | for(int j = 0; j < p; j++) { 86 | if(i & (1 << j)) { 87 | wtf[pos[j]] = '1'; 88 | } 89 | } 90 | } 91 | } 92 | } 93 | } 94 | cout << wtf << '\n'; 95 | } 96 | -------------------------------------------------------------------------------- /math/gauss_elimination/gauss_mod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | //solves: https://codeforces.com/gym/102861/problem/K 19 | 20 | template 21 | T fp(T x, long long e) { 22 | T ans(1); 23 | for(; e > 0; e /= 2) { 24 | if(e & 1) ans = ans * x; 25 | x = x * x; 26 | } 27 | return ans; 28 | } 29 | 30 | const ll mod = 2; 31 | struct mb { 32 | mb(int v = 0) : val(v < 0 ? v + mod : v) {} 33 | mb(ll v){ val = (v%mod+mod)%mod; } 34 | int val; 35 | 36 | void operator += (mb o) { *this = *this + o; } 37 | void operator -= (mb o) { *this = *this - o; } 38 | void operator *= (mb o) { *this = *this * o; } 39 | mb operator * (mb o) { return (int)((long long) val * o.val % mod); } 40 | mb operator / (mb o) { return *this * fp(o, mod - 2); } 41 | //bool operator == (mb o) { return val==o.val; } //usar soh para hashes 42 | mb operator + (mb o) { return val + o.val >= mod ? val + o.val - mod : val + o.val; } 43 | mb operator - (mb o) { return val - o.val < 0 ? val - o.val + mod : val - o.val; } 44 | }; 45 | 46 | /* 47 | retorno: 48 | 0 - sem solução 49 | 1 - uma solução 50 | 2 - infinitas soluções 51 | 52 | resolve sistma - acha X para 53 | a*X = b 54 | nos parametros da funcao, b é a ultima coluna da matriz a 55 | */ 56 | int gauss (vector < vector > a, vector & ans) { 57 | int n = sz(a), m = sz(a[0])-1; 58 | 59 | vector where (m, -1); 60 | for (int col=0, row=0; col a[sel][col].val) 64 | sel = i; 65 | if (a[sel][col].val==0) 66 | continue; 67 | for (int i=col; i<=m; ++i) 68 | swap (a[sel][i], a[row][i]); 69 | where[col] = row; 70 | 71 | for (int i=0; i g[N]; 100 | 101 | int main(){ 102 | ios::sync_with_stdio(0); cin.tie(0); 103 | int n, m; cin >> n >> m; 104 | 105 | fr(i,m){ 106 | int u, v; cin >> u >> v; u--,v--; 107 | g[u].push_back(v); 108 | g[v].push_back(u); 109 | } 110 | 111 | vector> mat(n,vector(n+1)); 112 | 113 | fr(i,n){ 114 | vector &v = mat[i]; 115 | if(sz(g[i])%2==1){ 116 | v[i] = 1; 117 | for(auto &it : g[i]){ 118 | v[it] = 1; 119 | } 120 | } else{ 121 | for(auto &it : g[i]){ 122 | v[it] = 1; 123 | } 124 | v[n] = 1; 125 | } 126 | } 127 | 128 | vector ans; 129 | if(gauss(mat,ans)>=1) cout << "Y" << "\n"; 130 | else cout << "N\n"; 131 | } 132 | -------------------------------------------------------------------------------- /math/gauss_elimination/gauss_xor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | //solves https://www.spoj.com/problems/XMAX/ 19 | 20 | 21 | //returns the maximun xor over all subsets of the values 22 | ll gauss_xor_subset(vector v){ 23 | int i = 0; 24 | for(int col = 62; col>=0 and i ans) ans ^= it; 48 | 49 | return ans; 50 | } 51 | 52 | int main(){ 53 | ios::sync_with_stdio(0); cin.tie(0); 54 | int n; cin >> n; 55 | 56 | vector v(n); 57 | fr(i,n) cin >> v[i]; 58 | 59 | cout << gauss_xor_subset(v) << "\n"; 60 | } 61 | -------------------------------------------------------------------------------- /math/numberTheory/Linear_recurrence/rec_linear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i> mm(vector> a, vector> b){ 18 | int l = sz(a); 19 | int c = sz(b[0]); 20 | assert(sz(a[0])==sz(b)); 21 | 22 | vector> ans(l,vector(c)); 23 | 24 | fr(i,l){ 25 | fr(j,c){ 26 | ll tot = 0; 27 | fr(k,a[0].size()){ 28 | tot = (tot+a[i][k]*b[k][j])%MOD; 29 | } 30 | ans[i][j] = tot; 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | 37 | /* 38 | Eleva matriz a um expoente que deve ser >=1 39 | se for zero deveria retornar matriz identidade 40 | */ 41 | vector> em(vector> a, ll exp){ 42 | if(exp==1) return a; 43 | vector> mid = em(a,exp/2); 44 | if(exp%2) return mm(mm(mid,mid),a); 45 | return mm(mid,mid); 46 | } 47 | 48 | int main(){ 49 | ll n, k; 50 | cin >> n >> k; 51 | 52 | /* 53 | n-- pois embora no problema, 54 | nao haja dp[0] (1<=i<=n apenas), 55 | a construcao que uso comeca de 0, portanto 56 | fazer n-- 57 | */ 58 | n--; 59 | 60 | /* 61 | Ao fazer matrizes, cuidado com coisas como 62 | valores maiores que MOD (pegar o modulo dele) 63 | se valor negativo, -1 = MOD-1 64 | CUIDADO com eo = eo%MOD, if(eo==0) cout << 0 65 | POR if ANTES DO MOD! 66 | */ 67 | 68 | vector> f0, t(k,vector(k)); 69 | 70 | //fazendo f0 - MUDA 71 | vector aux; 72 | aux.eb(2); 73 | fr(i,k-1) aux.eb(1); 74 | f0.eb(aux); 75 | 76 | //fazendo matriz t 77 | 78 | //isso nao muda geralmente 79 | fr(i,k-1) t[i][i+1] = 1; 80 | 81 | //pondo coeficientes na primeira coluna 82 | t[0][0] = 1; 83 | t[k-1][0] = 1; 84 | 85 | if(n> ans = mm(f0,em(t,n-k+1)); 89 | cout << ans[0][0] << endl; 90 | } 91 | 92 | return 0; 93 | } 94 | 95 | -------------------------------------------------------------------------------- /math/numberTheory/Linear_recurrence/rec_linear_mod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 17 | T fp(T x, long long e) { 18 | T ans(1); 19 | for(; e > 0; e /= 2) { 20 | if(e & 1) ans = ans * x; 21 | x = x * x; 22 | } 23 | return ans; 24 | } 25 | 26 | const ll mod = round(1e9)+7; 27 | struct mb { 28 | mb(int v = 0) : val(v < 0 ? v + mod : v) {} 29 | mb(ll v){ val = (v%mod+mod)%mod; } 30 | int val; 31 | 32 | void operator += (mb o) { *this = *this + o; } 33 | void operator -= (mb o) { *this = *this - o; } 34 | void operator *= (mb o) { *this = *this * o; } 35 | mb operator * (mb o) { return (int)((long long) val * o.val % mod); } 36 | mb operator / (mb o) { return *this * fp(o, mod - 2); } 37 | //bool operator == (mb o) { return val==o.val; } //usar soh para hashes 38 | mb operator + (mb o) { return val + o.val >= mod ? val + o.val - mod : val + o.val; } 39 | mb operator - (mb o) { return val - o.val < 0 ? val - o.val + mod : val - o.val; } 40 | }; 41 | 42 | vector> mm(vector> a, vector> b){ 43 | int l = sz(a); 44 | int c = sz(b[0]); 45 | assert(sz(a[0])==sz(b)); 46 | vector> ans(l,vector(c)); 47 | fr(i,l){ 48 | fr(j,c){ 49 | fr(k,sz(a[0])){ 50 | ans[i][j] += a[i][k]*b[k][j]; 51 | } 52 | } 53 | } 54 | return ans; 55 | } 56 | 57 | /* 58 | Eleva matriz a um expoente que deve ser >=1 59 | se for zero deveria retornar matriz identidade 60 | */ 61 | vector> em(vector> a, ll exp){ 62 | if(exp==1) return a; 63 | vector> mid = em(a,exp/2); 64 | if(exp%2) return mm(mm(mid,mid),a); 65 | return mm(mid,mid); 66 | } 67 | 68 | int main(){ 69 | //Coeficientes da recorrencia linear decrescente (em mod) 70 | //f[n] = coef[0]*f[n-1] + coef[1]*f[n-2] + ... 71 | vector coef = {6, 999999999, 999999999, 16, 0}; 72 | 73 | //Valores iniciais originais de forma crescente 74 | // f[0], f[1], f[2], ... 75 | vector vo_int = {2, 24, 96, 416, 1536}; 76 | reverse(all(vo_int)); 77 | 78 | assert(sz(coef)==sz(vo_int)); 79 | 80 | vector vo; 81 | for(auto &x : vo_int) vo.emplace_back(x); 82 | 83 | vector> mat(sz(vo),vector(sz(vo))); 84 | fr(i,sz(vo)){ 85 | mat[i][0] = coef[i]; 86 | } 87 | fr(i,sz(vo)-1) mat[i][i+1] = 1; 88 | 89 | /* 90 | //Printar matriz 91 | fr(i,sz(vo)){ 92 | fr(j,sz(vo)) cout << mat[i][j].val << " "; 93 | cout << endl; 94 | } 95 | */ 96 | 97 | ll n; 98 | cin >> n; 99 | n--; //subtrair se 1-indexado em relacao ao vo 100 | if(n> mo; 107 | mo.emplace_back(vo); 108 | mo = mm(mo,em(mat,n)); 109 | cout << mo[0][0].val << "\n"; 110 | } 111 | -------------------------------------------------------------------------------- /math/numberTheory/Linear_recurrence/rec_linear_struct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i> mm(vector> a, vector> b){ 31 | int l = sz(a); 32 | int c = sz(b[0]); 33 | assert(sz(a[0])==sz(b)); 34 | 35 | vector> ans(l,vector(c)); 36 | 37 | fr(i,l){ 38 | fr(j,c){ 39 | mynum tot(0,0); 40 | fr(k,a[0].size()){ 41 | tot = soma(tot,mult(a[i][k],b[k][j])); 42 | } 43 | ans[i][j] = tot; 44 | } 45 | } 46 | 47 | return ans; 48 | } 49 | 50 | //Qnd elevo a zero preciso codar matriz identidade! 51 | vector> ident(int n){ 52 | vector> ans(n,vector(n)); 53 | 54 | fr(i,n) ans[i][i].a = 1; 55 | 56 | return ans; 57 | } 58 | 59 | vector> em(vector> a, ll exp){ 60 | if(exp==0) return ident(a.size()); 61 | if(exp==1) return a; 62 | vector> mid = em(a,exp/2); 63 | if(exp%2) return mm(mm(mid,mid),a); 64 | return mm(mid,mid); 65 | } 66 | 67 | ll fp(ll base, ll exp){ 68 | if(exp==0) return 1; 69 | if(exp==1) return base; 70 | 71 | ll mid = fp(base, exp/2); 72 | 73 | if(exp%2) return mid*mid%MOD*base%MOD; 74 | return mid*mid%MOD; 75 | } 76 | 77 | int main(){ 78 | int ttt; 79 | cin >> ttt; 80 | 81 | fr(tt,ttt){ 82 | scanf("%lld", &MOD); 83 | 84 | vector> fn[2]; 85 | 86 | fr(x,2){ 87 | ll a[2], b[2], c[2], k[2], n[2]; 88 | scanf("%lld%lld%lld%lld%lld", &a[x], &b[x], &c[x], &k[x], &n[x]); 89 | 90 | vector> f1, t; 91 | 92 | f1 = {{mynum(a[x],0),mynum(b[x],0),mynum(c[x],0),mynum(0,1)}}; 93 | 94 | //Para escrever matriz T nesse formato, saber que eh igual ao que se le sim! 95 | t = { 96 | {mynum(0,0),mynum(k[x],0),mynum(1,0),mynum(0,0)}, 97 | {mynum(1,0),mynum(0,0),mynum(k[x],0),mynum(0,0)}, 98 | {mynum(1,0),mynum(0,0),mynum(0,0),mynum(1,0)}, 99 | {mynum(0,0),mynum(1,0),mynum(0,0),mynum(0,0)} 100 | }; 101 | 102 | // = *T 103 | 104 | //vetores base e resposta sao em linhas, mas os coefs em T sao em colunas!! 105 | // 106 | // = *(coefa coefb coefc) 107 | // (coefa coefb coefc) 108 | // (coefa coefb coefc) 109 | 110 | fn[x] = mm(f1,em(t,n[x]-1)); 111 | } 112 | 113 | ll x; 114 | scanf("%lld", &x); 115 | 116 | if(fn[0][0][0].b==0 and fn[1][0][0].b!=0) puts("UNKNOWN"); 117 | else if(fn[0][0][0].b==0 and fn[1][0][0].b==0){ 118 | ll y = fn[1][0][0].a; 119 | printf("%lld\n", y); 120 | } else{ 121 | ll h0 = (x-fn[0][0][0].a+MOD)%MOD*fp(fn[0][0][0].b,MOD-2)%MOD; 122 | ll y = (fn[1][0][0].a+h0*fn[1][0][0].b)%MOD; 123 | printf("%lld\n", y); 124 | } 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /math/numberTheory/gcd_ext/gcd_ext_crt_solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 7 | 8 | //solves https://atcoder.jp/contests/abc193/tasks/abc193_e 9 | 10 | ll div(ll a, ll b, bool ceil){ 11 | ll ans = abs(a/b); 12 | bool pos = (a<0)==(b<0); 13 | if(a%b and ceil==pos) ans++; 14 | if(!pos) ans*=-1; 15 | return ans; 16 | } 17 | 18 | ll gcd_ext(ll a, ll b, ll &xo, ll &yo){ 19 | if(b==0){ 20 | xo = 1, yo = 0; 21 | return a; 22 | } 23 | ll x1, y1; 24 | ll g = gcd_ext(b,a%b,x1,y1); 25 | xo = y1; 26 | yo = x1-(a/b)*y1; 27 | return g; 28 | } 29 | 30 | /* 31 | Retorna qual o menor x positivo que satisfaz 32 | a*x + b*y = c (obviamente o y correspondente eh negativo) 33 | (ou -1 se nao existe) 34 | 35 | Util em CRT para achar menor r positivo que 36 | r = ra (mod a) 37 | r = rb (mod b) 38 | -> 39 | a*x-b*y = rb-ra 40 | r = a*x + ra 41 | */ 42 | ll qual_sol(ll a, ll b, ll c){ 43 | ll xo, yo; 44 | ll g = gcd_ext(a,b,xo,yo); 45 | if(c%g!=0) return -1; 46 | c/=g, a/=g,b/=g; 47 | xo*=c,yo*=c; 48 | 49 | ll k = div(-xo,b,b>0); 50 | 51 | return xo+k*b; 52 | } 53 | 54 | /* 55 | Return minimun r such that: 56 | r = ra (mod a) 57 | r = rb (mod b) 58 | Or -1 if no such r 59 | */ 60 | ll solve_crt(ll ra, ll a, ll rb, ll b){ 61 | ll minx = qual_sol(a,-b,rb-ra); 62 | if(minx==-1) return minx; 63 | return a*minx+ra; 64 | } 65 | 66 | int main(){ 67 | ios::sync_with_stdio(0); cin.tie(0); 68 | ll t; cin >> t; 69 | fr(tt,t){ 70 | ll X, Y, P, Q; cin >> X >> Y >> P >> Q; 71 | 72 | ll t1 = P+Q, t2 = 2*X+2*Y; 73 | ll ans = LLONG_MAX; 74 | fr(i,Q) fr(j,Y){ 75 | ll cand = solve_crt(P+i,t1,X+j,t2); 76 | if(cand!=-1) rmin(ans,cand); 77 | } 78 | if(ans 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(0,n-1)(rnd); 12 | } 13 | 14 | ll mulmod(ll a, ll b, ll mod){ 15 | if(b<0) return mulmod(a,(b%mod+mod)%mod,mod); 16 | if(b==0) return 0LL; 17 | ll ans = (2LL*mulmod(a,b/2,mod))%mod; 18 | if(b%2==0) return ans; 19 | return (ans+a)%mod; 20 | } 21 | 22 | ll exp_mod(ll a, ll x, ll m) { 23 | if (x == 0) return 1; 24 | ll res = exp_mod(a, x/2, m); 25 | res = mulmod(res, res, m); //(res * res) % m; 26 | if(x % 2 == 1) res = mulmod(res, a, m); // (res * a) % m 27 | return res; 28 | } 29 | 30 | //Rabin Miller 31 | bool ispp(ll n){ 32 | if(n<=1) return 0; 33 | if(n<=3) return 1; 34 | ll s = 0, d = n-1; 35 | while(d%2==0){ 36 | d/=2; 37 | s++; 38 | } 39 | fr(k,64){ 40 | ll a = grand(n-3)+2; 41 | ll x = exp_mod(a,d,n); 42 | if(x!=1 and x!=n-1){ 43 | for(int r = 1;r F; 70 | 71 | void factor(ll n){ 72 | if(n==1) return; 73 | if(ispp(n)){ 74 | F[n]++; 75 | return; 76 | } 77 | ll d = rho(n); 78 | factor(d); 79 | factor(n/d); 80 | return; 81 | } 82 | 83 | //-------------------------- 84 | //solves problem f from: https://codeforces.com/gym/102299/standings 85 | // USP 2019 try outs 86 | 87 | ll mypot(ll b, ll x){ 88 | ll ans = 1; 89 | fr(i,x) ans*=b; 90 | return ans; 91 | } 92 | 93 | int main(){ 94 | ll a, b; 95 | cin >> a >> b; 96 | 97 | ll g = gcd(a,b); 98 | 99 | b/=g; 100 | 101 | if(b==1){ 102 | cout << 2 << endl; 103 | return 0; 104 | } 105 | 106 | factor(b); 107 | 108 | ll ans = 1; 109 | 110 | for(auto &par : F){ 111 | ans *= par.fi; 112 | } 113 | 114 | cout << ans << endl; 115 | } 116 | -------------------------------------------------------------------------------- /strings/aho-corasick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | //solves: https://codeforces.com/contest/963/problem/D 19 | 20 | struct no{ 21 | int prox[26]; //qual o proximo vertice na trie se por caracter 22 | int go[26]; //qual proximo vertice no automato (suffix link + pc se nao tiver) 23 | int p = -1; //vertice do pai 24 | char pc; //caracter do qual pai atinge vertice atual 25 | int link = -1; //suffix link 26 | 27 | bool is_leaf = 0; 28 | int id_leaf = -1; //indice da string que eh leaf nesse vertice (nao pode repetir) 29 | int p_leaf = -2; //vertice na trie que é sufixo da string atual e eh leaf 30 | 31 | no(int _p = -1, char _pc = '$'){ 32 | p = _p, pc = _pc; 33 | fr(i,26) prox[i] = -1, go[i] = -1; 34 | } 35 | }; 36 | 37 | vector trie(1); 38 | 39 | void add_string(string &s, int ids){ 40 | int v = 0; 41 | fr(i,sz(s)){ 42 | int ic = s[i]-'a'; 43 | if(trie[v].prox[ic]==-1){ 44 | trie[v].prox[ic] = sz(trie); 45 | trie.emplace_back(v,s[i]); 46 | } 47 | v = trie[v].prox[ic]; 48 | } 49 | trie[v].is_leaf = 1; 50 | trie[v].id_leaf = ids; 51 | } 52 | 53 | int go(int v, char c); 54 | 55 | int get_link(int v){ 56 | if(trie[v].link==-1){ 57 | if(v==0 or trie[v].p==0) trie[v].link = 0; 58 | else trie[v].link = go(get_link(trie[v].p),trie[v].pc); 59 | } 60 | return trie[v].link; 61 | } 62 | 63 | int go(int v, char c){ 64 | int ic = c-'a'; 65 | if(trie[v].go[ic]==-1){ 66 | if(trie[v].prox[ic]!=-1) trie[v].go[ic] = trie[v].prox[ic]; 67 | else if(v==0) trie[v].go[ic] = 0; 68 | else trie[v].go[ic] = go(get_link(v),c); 69 | } 70 | return trie[v].go[ic]; 71 | } 72 | 73 | const int N = 1e5+10; 74 | int tam[N], k[N]; 75 | vector oc[N]; 76 | 77 | int get_previous_leaf(int v){ 78 | if(trie[v].p_leaf!=-2) return trie[v].p_leaf; 79 | if(trie[v].is_leaf) return trie[v].p_leaf = v; 80 | if(v==0) return trie[v].p_leaf = -1; 81 | return trie[v].p_leaf = get_previous_leaf(get_link(v)); 82 | } 83 | 84 | void process_all_leaves(int v, int i){ 85 | while(1){ 86 | v = get_previous_leaf(v); 87 | if(v==-1) return; 88 | oc[trie[v].id_leaf].emplace_back(i); 89 | v = get_link(v); 90 | } 91 | } 92 | 93 | int main(){ 94 | ios::sync_with_stdio(0); cin.tie(0); 95 | string t; cin >> t; 96 | int q; cin >> q; 97 | 98 | fr(qq,q){ 99 | string s; cin >> k[qq] >> s; 100 | add_string(s,qq); 101 | tam[qq] = sz(s); 102 | } 103 | 104 | int v = 0; 105 | fr(i,sz(t)){ 106 | v = go(v,t[i]); 107 | process_all_leaves(v,i); 108 | } 109 | 110 | fr(qq,q){ 111 | if(sz(oc[qq]) 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 0; e /= 2) { 18 | if(e & 1) ans = ans * x; 19 | x = x * x; 20 | } 21 | return ans; 22 | } 23 | 24 | mt19937 rng(time(0)); 25 | 26 | vector perm; 27 | ull p27[N]; 28 | ull inv27[N]; 29 | 30 | void init_hash(int n){ 31 | fr(i,26) perm.push_back(i+1); 32 | shuffle(all(perm),rng); 33 | p27[0] = inv27[0] = 1; 34 | for(int i = 1; i pref; 49 | meuhash(){} 50 | meuhash(string &s){ 51 | assert(sz(s)=0 and l=0 and r=0){ 66 | ans -= pref[l]; 67 | ans *= inv27[l+1]; 68 | } 69 | return ans; 70 | } 71 | }; //end hash 72 | 73 | vector mat[610][610]; 74 | 75 | int fcnt(ull cur, int l, int r){ 76 | return upper_bound(all(mat[l][r]),cur)-lower_bound(all(mat[l][r]),cur); 77 | } 78 | 79 | int main(){ 80 | ios::sync_with_stdio(0); cin.tie(0); 81 | 82 | init_hash(N); 83 | 84 | ll n; cin >> n; 85 | map> mp; 86 | fr(i,n){ 87 | string s; cin >> s; 88 | string aux = s; 89 | sort(all(aux)); 90 | mp[aux].push_back(s); 91 | } 92 | 93 | ll ans = n*(n-1)/2*1337; 94 | 95 | for(auto &[s_sort,v] : mp){ 96 | ans -= (ll)sz(v)*(sz(v)-1)/2*1335; 97 | int tam = sz(v[0]); 98 | if(sz(v)<=600){ 99 | fr(i,sz(v)) fr(j,i){ 100 | if(i==j) continue; 101 | int l = 0, r = tam-1; 102 | while(l=0 and v[i][r]==v[j][r]) r--; 104 | int ok = 0; 105 | for(int k = l+1; k<=r; k++){ 106 | if(v[i][k] hashes(sz(v)); 117 | fr(i,sz(v)) hashes[i] = meuhash(v[i]); 118 | fr(r,tam) fr(l,r+1) mat[l][r].clear(); 119 | fr(i,sz(v)){ 120 | fr(r,tam) fr(l,r+1){ 121 | ull cur = 0; 122 | if(l) cur = hashes[i].gethash(0,l-1); 123 | if(r=v[i][r]) r++; 133 | ull cur = 0; 134 | if(l) cur = hashes[i].gethash(0,l-1); 135 | if(r 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i fpref(string &s){ 26 | vector pref(s.size()); 27 | 28 | for(int i = 1; i pref = fpref(s); 39 | int n = sz(s); 40 | vector v(n); 41 | fr(i,n) v[i] = s[i]-'a'; 42 | fr(c,26) prox[c][0] = 0; 43 | prox[v[0]][0] = 1; 44 | for(int i = 1; i<=n; i++){ 45 | fr(c,26){ 46 | prox[c][i] = prox[c][pref[i-1]]; 47 | } 48 | if(i= mod ? val + o.val - mod : val + o.val; } 67 | mb operator - (mb o) { return val - o.val < 0 ? val - o.val + mod : val - o.val; } 68 | }; 69 | 70 | const int M = 1e3+10; 71 | mb dp[M][3][N]; 72 | int vis[M][3][N]; 73 | 74 | class MostSubstrings 75 | { 76 | public: 77 | int count(string s, int l) { 78 | fr(it,l+1) fr(noc,3) fr(is,sz(s)+1) dp[it][noc][is] = 0, vis[it][noc][is] = 0; 79 | dp[0][0][0] = 1; 80 | vis[0][0][0] = 1; 81 | build_aut(s); 82 | int i0 = 0, i1 = 1; 83 | fr(it,l){ 84 | bool aumentou = 0; 85 | for(int noc : {i0,i1}){ 86 | fr(is,sz(s)+1){ 87 | if(!vis[it][noc][is]) continue; 88 | fr(c,26){ 89 | int js = prox[c][is]; 90 | int poc = noc; 91 | if(js==sz(s)) poc = (poc+1)%3; 92 | if(poc!=i0 and poc!=i1) aumentou = 1; 93 | vis[it+1][poc][js] = 1; 94 | dp[it+1][poc][js] += dp[it][noc][is]; 95 | } 96 | } 97 | } 98 | if(aumentou){ 99 | i0 = (i0+1)%3; 100 | i1 = (i0+1)%3; 101 | } 102 | } 103 | for(int noc : {i1,i0}){ 104 | mb ans = 0; 105 | int ok = 0; 106 | fr(is,sz(s)+1){ 107 | if(vis[l][noc][is]){ 108 | ok = 1; 109 | ans+=dp[l][noc][is]; 110 | } 111 | } 112 | if(ok) return ans.val; 113 | } 114 | //assert(0); 115 | return 0; 116 | } 117 | }; 118 | 119 | //comment main afterwards 120 | int main(){ 121 | int ans = MostSubstrings().count("aaaa",13); 122 | prin(ans); //ans = 1 123 | 124 | ans = MostSubstrings().count("helloworld",7); 125 | prin(ans); //ans = 31810120 126 | 127 | ans = MostSubstrings().count("decode",11); 128 | prin(ans); //ans = 52 129 | } 130 | -------------------------------------------------------------------------------- /strings/preffix_func_kmp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i fpref(string &s){ 10 | vector pref(s.size()); 11 | 12 | for(int i = 1; i> t; 26 | 27 | fr(tt,t){ 28 | string s; 29 | cin >> s; 30 | 31 | vector pref = fpref(s); 32 | 33 | int atm = s.size()-pref[s.size()-1]; 34 | int d = s.size()%atm; 35 | 36 | fr(i,8) cout << s[(d+i)%atm]; 37 | cout << "..." << endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /strings/suffix_array_lcp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 9 | #define rmax(a,b) a = max(a,b) 10 | 11 | //solves https://codeforces.com/gym/103081/problem/K 12 | //also used in https://www.codechef.com/problems/ANUSAR 13 | 14 | /* 15 | Retorna vector p, suffix array 16 | p[0] eh o indice do menor menor sufixo 17 | para usar em vector de inteiros, fazer compressao de coordenadas 18 | para [1,n] e o caracter especial adicionado sera o 0 19 | Note que n eh incrementado e com caracter especial valores sao [0,n-1] 20 | */ 21 | //vector make_suf(vector s){ 22 | vector make_suf(string s){ 23 | s+=(char)0; 24 | //s.push_back(0); 25 | int n = sz(s); 26 | vector p(n), c(n), cnt(max(256,n)); 27 | 28 | fr(i,n) cnt[s[i]]++; 29 | for(int i = 1; i pn(n), cn(n); 38 | for(int k = 0; (1<=0; i--) p[--cnt[c[pn[i]]]] = pn[i]; 44 | nc = 1; 45 | cn[p[0]] = 0; 46 | for(int i = 1; i make_lcp(vector &s, vector &p){ 60 | vector make_lcp(string &s, vector &p){ 61 | int n = sz(s); 62 | vector rank(n), lcp(n-1); 63 | 64 | fr(i,n) rank[p[i]] = i; 65 | 66 | int k = 0; 67 | fr(i,n){ 68 | if(rank[i]==n-1){ 69 | k = 0; 70 | continue; 71 | } 72 | int j = p[rank[i]+1]; 73 | while(i+k> s; 83 | 84 | //suffix array and lcp 85 | vector p = make_suf(s); 86 | vector lcp = make_lcp(s,p); 87 | 88 | int n = sz(s); 89 | ll mins = LLONG_MAX, id = -1; 90 | fr(i,sz(s)){ 91 | ll cur = 1; 92 | if(i-1>=0) rmax(cur,lcp[i-1]+1); 93 | if(i=cur and (cur 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i =0 ; i 2 | using namespace std; 3 | 4 | #define FILE_IN freopen("kotlin.in", "r", stdin); 5 | #define FILE_OUT freopen("kotlin.out", "w", stdout); 6 | 7 | #define fr(i,n) for(int i = 0; i a1"); 63 | system("./prog2 < input > a2"); 64 | 65 | pfile1 = fopen("a1","r"); 66 | pfile2 = fopen("a2","r"); 67 | 68 | ll ans1, ans2; 69 | //vector ans1(n), ans2(n); 70 | 71 | ///* 72 | fscanf(pfile1,"%lld", &ans1); 73 | fscanf(pfile2,"%lld", &ans2); 74 | //*/ 75 | 76 | 77 | /* 78 | //fscanf(pfile1,"%*d"); //caso no output seja printado n 79 | //fscanf(pfile2,"%*d"); 80 | 81 | fr(i,n){ 82 | fscanf(pfile1,"%lld", &ans1[i]); 83 | } 84 | 85 | fr(i,n){ 86 | fscanf(pfile2,"%lld", &ans2[i]); 87 | } 88 | //*/ 89 | 90 | fclose(pfile1); 91 | fclose(pfile2); 92 | 93 | ///* 94 | if(ans1!=ans2){ 95 | prin(ii); 96 | //Se parou eh pq input esta com o arquivo que quebra 97 | goto end; 98 | } 99 | //*/ 100 | 101 | 102 | /* 103 | bool dif = 0; 104 | fr(i,n){ 105 | if(ans1[i]!=ans2[i]) dif = 1; 106 | } 107 | 108 | if(dif){ 109 | cout << n << endl; 110 | fr(i,n){ 111 | cout << v[i] << " "; 112 | } 113 | cout << endl; 114 | goto end; 115 | } 116 | //*/ 117 | 118 | } 119 | 120 | end: 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /tecnico/Run_multiple/run_multiple_asserts.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; ir){ 93 | is_big[i] = 1; 94 | for(auto &it : g[i]){ 95 | viz_big[it].push_back(i); 96 | } 97 | } 98 | } 99 | 100 | int q; scanf("%d", &q); 101 | 102 | fr(qq,q){ 103 | int tipo, no; scanf("%d%d", &tipo, &no); no--; 104 | 105 | if(tipo==1){ 106 | if(!is_big[no]){ 107 | mb cur_val = get_val(no); 108 | for(auto &it : g[no]){ 109 | val[it] += cur_val; 110 | } 111 | } else{ 112 | for(auto &it : viz_big[no]){ 113 | val[it] += val[no]; 114 | } 115 | inc[no] += val[no]; 116 | } 117 | } else{ 118 | printf("%d\n", get_val(no).val); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /tecnico/old/Useful_Conversions/makeStringArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define FILE_IN freopen("kotlin.in", "r", stdin); 5 | #define FILE_OUT freopen("kotlin.out", "w", stdout); 6 | 7 | #define fr(i,n) for(int i=0;i pii; 21 | typedef pair pll; 22 | 23 | const long double PI = acos(-1.0l); 24 | const ll MOD = 1e9+7; 25 | 26 | //LLONG_MAX 27 | //-DBL_MAX 28 | //__builtin_popcountll(ll x) - __builtin_popcount(int x) 29 | 30 | bool debug = 1; 31 | #define printa(a) cout << #a << " = " << (a) << endl 32 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 33 | #define soprin(a) if(debug) cout << (a) 34 | #define ppal(a) if(debug) cout << #a << endl 35 | #define prinsep if(debug) cout << "------" << endl 36 | #define cendl if(debug) cout << endl 37 | #define prinpar(p) if(debug) cout << #p << ".fi=" << (p).fi << " " << #p << ".se=" << (p).se << endl 38 | #define print(tup) if(debug) cout << #tup << " = {" << get(tup,0) << ", " << get(tup,1) << ", " << get(tup,2) << "}\n" 39 | #define prinv(v) if(debug){ cout << #v << ":" << endl; for(auto it = (v).begin(); it!=(v).end();it++){ cout << *it << " ";} cout << endl;} 40 | 41 | const int N = -1; 42 | 43 | int main(){ 44 | //FILE_IN FILE_OUT 45 | vector mat; 46 | vector v; 47 | 48 | bool comAspas = 1; 49 | 50 | char c; 51 | string linha; 52 | 53 | while(scanf("%c", &c)!=EOF){ 54 | if(c=='\n'){ 55 | if(linha.size()) mat.pb(linha); 56 | linha = ""; 57 | } else{ 58 | linha+=c; 59 | } 60 | } 61 | if(linha.size()) mat.pb(linha); 62 | 63 | string pal; 64 | int lastif = 0; 65 | 66 | for(auto &l : mat){ 67 | int i = 0; 68 | 69 | while(i v = {"; 84 | fr(i,v.size()){ 85 | if(comAspas) printf("\"%s\"%c ", v[i].c_str(), ",}"[i==v.size()-1]); 86 | else printf("%s%c ", v[i].c_str(), ",}"[i==v.size()-1]); 87 | } 88 | printf(";\n"); 89 | 90 | return 0; 91 | } 92 | 93 | -------------------------------------------------------------------------------- /tecnico/old/mybashes/ca: -------------------------------------------------------------------------------- 1 | if [ -e $1 ]; then 2 | printf $1" ja existe\n" 3 | printf "Usar gedit "$1" &\n" 4 | else 5 | cp /home/vlamarca/myBashes/molde.cpp $1 6 | gedit $1 & 7 | fi 8 | -------------------------------------------------------------------------------- /tecnico/old/mybashes/cacomp: -------------------------------------------------------------------------------- 1 | cp /home/vlamarca/myBashes/compara_random.cpp $1 2 | gedit $1 & 3 | -------------------------------------------------------------------------------- /tecnico/old/mybashes/cajava: -------------------------------------------------------------------------------- 1 | cp /home/vlamarca/myBashes/molde.java $1".java" 2 | gedit $1".java" & 3 | sed -i -e 's/HelloWorld/'$1'/g' $1".java" 4 | -------------------------------------------------------------------------------- /tecnico/old/mybashes/cola: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | xsel --clipboard > input 4 | if [ "`tail -c 1 input`" != "`printf "\n"`" ] 5 | then 6 | printf "\n" >> input 7 | fi 8 | cat input 9 | 10 | -------------------------------------------------------------------------------- /tecnico/old/mybashes/compara_random.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define FILE_IN freopen("kotlin.in", "r", stdin); 5 | #define FILE_OUT freopen("kotlin.out", "w", stdout); 6 | 7 | #define fr(i,n) for(int i=0;i pii; 20 | typedef pair pll; 21 | 22 | const long double PI = acos(-1.0l); 23 | const ll MOD = 1e9+7; 24 | 25 | //LONG_LONG_MAX 26 | //-DBL_MAX 27 | 28 | bool debug = 1; 29 | #define prin(a) if(debug) cout << #a << " = " << (a) << endl 30 | 31 | /* 32 | rodar com crman para ver evolucao (nao requer input) 33 | 34 | o que geralmente é necessario mudar oq esta comentado 35 | ao lado ou abaixo 36 | */ 37 | 38 | string f(int tams){ 39 | string s; 40 | fr(i,tams){ 41 | s+= 'a'+rand()%26; 42 | } 43 | 44 | return s; 45 | } 46 | 47 | const int N = 2e5+10; 48 | ll v[N]; 49 | FILE *pfile, *pfile1, *pfile2; 50 | //string v[1010]; 51 | 52 | ll n; 53 | 54 | int main(){ 55 | 56 | system("g++ -std=c++14 -Wshadow -fsanitize=address -D_GLIBCXX_DEBUG -o prog1 net.cpp"); //alterar nome 57 | system("g++ -std=c++14 -Wshadow -fsanitize=address -D_GLIBCXX_DEBUG -o prog2 hp.cpp"); 58 | 59 | srand(time(nullptr)); 60 | 61 | fr(ii,1000){ 62 | prin(ii); 63 | 64 | n = 5 + rand()%2; 65 | 66 | pfile = fopen("input","w"); 67 | fprintf(pfile,"%lld\n", n); 68 | 69 | fr(i,n){ 70 | v[i] = rand()%10+1; //aumentar %10 se quiser numeros maiores 71 | //v[i] = f(); 72 | 73 | fprintf(pfile, "%lld ", v[i]); 74 | //fprintf(pfile, "%s ", v[i].c_str()); 75 | } 76 | fprintf(pfile,"\n"); 77 | 78 | fclose(pfile); 79 | 80 | system("./prog1 < input > a1"); 81 | system("./prog2 < input > a2"); 82 | 83 | pfile1 = fopen("a1","r"); 84 | pfile2 = fopen("a2","r"); 85 | 86 | ll ans1, ans2; 87 | //vector ans1(n), ans2(n); 88 | 89 | ///* 90 | fscanf(pfile1,"%lld", &ans1); 91 | fscanf(pfile2,"%lld", &ans2); 92 | //*/ 93 | 94 | 95 | /* 96 | //fscanf(pfile1,"%*d"); //caso no output seja printado n 97 | //fscanf(pfile2,"%*d"); 98 | 99 | fr(i,n){ 100 | fscanf(pfile1,"%lld", &ans1[i]); 101 | } 102 | 103 | fr(i,n){ 104 | fscanf(pfile2,"%lld", &ans2[i]); 105 | } 106 | //*/ 107 | 108 | fclose(pfile1); 109 | fclose(pfile2); 110 | 111 | ///* 112 | //ABRIR input !!!!!!!!!!!!!!!!!!!!!!! 113 | 114 | //CHECAR SE input ESTA CORRETO E SE É 115 | //O MSM DOQ PRINTADO NO TERMINAL 116 | if(ans1!=ans2 or 1){ 117 | prin(ii); 118 | cout << n << endl; 119 | fr(i,n){ 120 | cout << v[i] << " "; 121 | } 122 | cout << endl; 123 | goto end; 124 | } 125 | //*/ 126 | 127 | /* 128 | bool dif = 0; 129 | fr(i,n){ 130 | if(ans1[i]!=ans2[i]) dif = 1; 131 | } 132 | 133 | if(dif or 1){ 134 | cout << n << endl; 135 | fr(i,n){ 136 | cout << v[i] << " "; 137 | } 138 | cout << endl; 139 | goto end; 140 | } 141 | //*/ 142 | 143 | } 144 | 145 | end: 146 | return 0; 147 | } 148 | 149 | -------------------------------------------------------------------------------- /tecnico/old/mybashes/crjava: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | javac $1 && java ${1:0: ${#1}-5} < input #string $1 sem os 5 ultimos caracteres 4 | 5 | 6 | -------------------------------------------------------------------------------- /tecnico/old/mybashes/molde.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i pii; 17 | typedef pair pll; 18 | 19 | //-DBL_MAX 20 | //__builtin_popcountll(ll x) - __builtin_popcount(int x) 21 | // 32-__builtin_clz(ll)(x) = posicao do digito mais significativo = floor(log2(x)) 22 | 23 | //freopen("file_name.in", "r", stdin); 24 | //freopen("file_name.out", "w", stdout); 25 | 26 | const int N = -1; 27 | 28 | int main(){ 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /tecnico/old/mybashes/show_compila: -------------------------------------------------------------------------------- 1 | printf "Digitar: g++ -std=c++14 -Wall -O2 -Wshadow $1 && ./a.out\n" 2 | -------------------------------------------------------------------------------- /tecnico/optimizations/array_de_vector_otimizado_ae.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i v; 23 | 24 | comp_str(){} 25 | 26 | comp_str(vector &_v){ 27 | v = _v; 28 | sort(all(v)); 29 | v.resize(unique(all(v))-v.begin()); 30 | } 31 | 32 | int str_to_int(string &s){ 33 | return lower_bound(all(v),s)-v.begin(); 34 | } 35 | 36 | string int_to_str(int i){ 37 | return v[i]; 38 | } 39 | 40 | }; 41 | 42 | const int N = 1e5+10; 43 | vector pergs[N]; 44 | string resps[N]; 45 | vector perg[N]; 46 | int resp[N]; 47 | 48 | int sz = 0; 49 | map prox[N]; 50 | vector resp_in_trie[N]; 51 | int n_tot[N], max_freq[N]; 52 | 53 | int n, T; 54 | 55 | vector> vis; 56 | vector> dp; 57 | 58 | long double rec(int i, int t){ 59 | if(t==T) return 0; 60 | long double &ans = dp[i][t]; 61 | if(vis[i][t]) return ans; 62 | vis[i][t] = 1; 63 | 64 | assert(fabs(ans-1e-6)<1e-5); 65 | 66 | if(n_tot[i]>1){ 67 | int soma = 0; 68 | for(auto &[ii,ip] : prox[i]){ 69 | ans += n_tot[ip]*rec(ip,t+1); 70 | soma+=n_tot[ip]; 71 | } 72 | assert(soma==n_tot[i]); 73 | ans/=n_tot[i]; 74 | } 75 | 76 | rmax(ans,(long double)max_freq[i]/n_tot[i] + rec(0,t+1)); 77 | 78 | return ans; 79 | } 80 | 81 | int main(){ 82 | 83 | vector vsaux; 84 | 85 | scanf("%d%d", &T, &n); 86 | scanf(" "); 87 | 88 | fr(i,n){ 89 | char c; 90 | string s; 91 | while(1){ 92 | if(scanf("%c",&c)==EOF or c=='\n'){ 93 | vsaux.emplace_back(s); 94 | resps[i] = s; 95 | s.clear(); 96 | break; 97 | } else if(c==' '){ 98 | vsaux.emplace_back(s); 99 | pergs[i].emplace_back(s); 100 | s.clear(); 101 | } else{ 102 | s+=c; 103 | } 104 | } 105 | } 106 | 107 | comp_str cs(vsaux); 108 | 109 | fr(i,n){ 110 | for(auto &s : pergs[i]) perg[i].emplace_back(cs.str_to_int(s)); 111 | resp[i] = cs.str_to_int(resps[i]); 112 | //prinv(perg[i]); 113 | //prin(resp[i]); 114 | //cout << endl; 115 | } 116 | 117 | fr(i,n){ 118 | int cur = 0; 119 | resp_in_trie[cur].push_back(resp[i]); 120 | for(auto &c : perg[i]){ 121 | if(!prox[cur].count(c)){ 122 | prox[cur][c] = ++sz; 123 | } 124 | cur = prox[cur][c]; 125 | resp_in_trie[cur].push_back(resp[i]); 126 | } 127 | } 128 | 129 | fr(cur,sz+1){ 130 | sort(all(resp_in_trie[cur])); 131 | assert(sz(resp_in_trie[cur])>=1); 132 | int i = 0; 133 | while(i0); 144 | } 145 | 146 | /* 147 | fr(cur,sz+1){ 148 | prin(cur); 149 | prin(n_tot[cur]); 150 | prin(max_freq[cur]); 151 | for(auto &[ii,ip] : prox[cur]){ 152 | prin(ip); 153 | } 154 | cout << endl; 155 | } 156 | */ 157 | 158 | dp.resize(sz+1,vector(T,0)); 159 | vis.resize(sz+1,vector(T,0)); 160 | 161 | printf("%.10lf\n", (double)rec(0,0)); 162 | } 163 | -------------------------------------------------------------------------------- /tecnico/optimizations/mymap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | //solves https://atcoder.jp/contests/abc191/tasks/abc191_f 19 | 20 | //use: mymap mp 21 | template 22 | struct mymap{ 23 | vector keys; 24 | vector values; 25 | 26 | mymap(vector _keys){ 27 | keys = _keys; 28 | sort(all(keys)); 29 | keys.resize(unique(all(keys))-keys.begin()); 30 | values = vector(sz(keys)); //valores iniciados como default (nulo) 31 | //fr(i,sz(values)) values[i] = i; 32 | } 33 | 34 | value_type &operator[](key_type key){ 35 | //assert(binary_search(all(keys),key)); 36 | int id = lower_bound(all(keys),key)-keys.begin(); 37 | return values[id]; 38 | } 39 | }; //end mymap 40 | 41 | /* 42 | //mymap without template, ll to ll 43 | 44 | struct mymap{ 45 | vector keys, values; 46 | 47 | mymap(vector _keys){ 48 | keys = _keys; 49 | sort(all(keys)); 50 | keys.resize(unique(all(keys))-keys.begin()); 51 | values = vector(sz(keys),0); //valores iniciados zerados 52 | } 53 | 54 | ll &operator[](ll key){ 55 | //assert(binary_search(all(keys),key)); 56 | int id = lower_bound(all(keys),key)-keys.begin(); 57 | return values[id]; 58 | } 59 | }; //end mymap 60 | */ 61 | 62 | const int N = 2e3+10; 63 | vector divisores[N]; 64 | 65 | int main(){ 66 | ios::sync_with_stdio(0); cin.tie(0); 67 | 68 | int n; cin >> n; 69 | vector v(n); fr(i,n) cin >> v[i]; 70 | sort(all(v)); 71 | v.resize(unique(all(v))-v.begin()); 72 | 73 | fr(i,n){ 74 | for(ll d = 1; d*d<=v[i]; d++){ 75 | if(v[i]%d==0){ 76 | divisores[i].push_back(d); 77 | if(d!=v[i]/d) divisores[i].push_back(v[i]/d); 78 | } 79 | } 80 | } 81 | vector chaves; 82 | fr(i,n) for(auto &d : divisores[i]) chaves.push_back(d); 83 | 84 | mymap mp(chaves); 85 | 86 | fr(i,n) for(auto &d : divisores[i]) mp[d] = gcd(mp[d],v[i]); 87 | 88 | int ans = 0; 89 | for(auto &d : mp.keys) if(mp[d]==d and d<=v[0]) ans++; 90 | cout << ans << "\n"; 91 | } 92 | -------------------------------------------------------------------------------- /tecnico/optimizations/pragma_otimizations/fast_short_loop.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #pragma GCC optimize ("unroll-loops") 3 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 4 | # include 5 | # include 6 | using namespace std; 7 | 8 | unsigned char a[200000]; 9 | 10 | /* 11 | Problema: https://codeforces.com/contest/911/problem/G 12 | 13 | Comentario: 14 | the first just tells the compiler to optimize the code for speed to make is as fast as possible (and not look for space) 15 | the second tells the compiler to unroll loops. normally if we have a loop there is a ++i instruction somewhere. We normally dont care because code inside the loop requires much more time but in this case there is only one instruction inside the loop so we want the compiler to optimize this. 16 | and last but not least we want to tell the compiler that our cpu has simd instructions and allow him to vectorize our code 17 | https://codeforces.com/blog/entry/56771#comment-404681 18 | */ 19 | 20 | int main() { 21 | ios_base::sync_with_stdio(false); 22 | cin.tie(nullptr); 23 | int n; 24 | scanf("%d", &n); 25 | for (int i = 0; i < n; ++i) { 26 | scanf("%hhu", &a[i]); 27 | } 28 | int q; 29 | scanf("%d", &q); 30 | while (q--) { 31 | int l, r; 32 | unsigned char x, y; 33 | scanf("%d%d%hhu%hhu", &l, &r, &x, &y); 34 | for (int i = l-1; i < r; i++) { 35 | a[i] = a[i] == x ? y : a[i]; 36 | } 37 | } 38 | for (int i = 0; i < n; ++i) { 39 | printf("%hhu ", a[i]); 40 | } 41 | } 42 | //O(n^2) test... it seems possible for this... but it shoudnt... 43 | -------------------------------------------------------------------------------- /tecnico/random.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i0); 30 | return l+rand()%tam; 31 | } 32 | 33 | int gera_mt(int l, int r){ 34 | return uniform_int_distribution(l, r)(rng); 35 | } 36 | 37 | int gera_rng(int l, int r){ 38 | int tam = r-l+1; 39 | assert(tam>0); 40 | return l+rng()%tam; 41 | } 42 | 43 | int main(){ 44 | srand(seed_seg); //srand normal precisa ser na main 45 | 46 | cout << "Gera rand normal:" << endl; 47 | fr(i,5) prin(gera_rand(1,5)); 48 | cout << endl; 49 | 50 | cout << "Gera mt13997:" << endl; 51 | fr(i,5) prin(gera_mt(1,5)); 52 | cout << endl; 53 | 54 | cout << "Gera rng:" << endl; 55 | fr(i,5) prin(gera_rng(1,5)); 56 | cout << endl; 57 | 58 | vector v = {1,2,3,4,5}; 59 | 60 | cout << "Shuffle normal: " << endl; 61 | random_shuffle(all(v)); 62 | prinv(v); 63 | cout << endl; 64 | 65 | cout << "Shuffle mt13997: " << endl; 66 | shuffle(all(v),rng); 67 | prinv(v); 68 | cout << endl; 69 | } 70 | -------------------------------------------------------------------------------- /tecnico/setup_maratona/cr: -------------------------------------------------------------------------------- 1 | g++ -std=c++14 -Wshadow -fsanitize=address -D_GLIBCXX_DEBUG $1 && ./a.out 2 | -------------------------------------------------------------------------------- /tecnico/setup_maratona/crf: -------------------------------------------------------------------------------- 1 | g++ -std=c++14 -O2 -w $1 && ./a.out 2 | -------------------------------------------------------------------------------- /tecnico/setup_maratona/temp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i = 0; i(a,b) 13 | #define rmax(a,b) a = max(a,b) 14 | 15 | #define fi first 16 | #define se second 17 | 18 | int main(){ 19 | //ios::sync_with_stdio(0); cin.tie(0); 20 | } 21 | -------------------------------------------------------------------------------- /tecnico/time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define fr(i,n) for(int i=0;i