├── Data-Structure ├── 2d_fenwick.cpp ├── fenwick.cpp ├── lowest_common_ancestor.cpp ├── mo's.cpp ├── ost.cpp ├── segment_tree.cpp └── sliding_range_minimum_query.cpp ├── Graphs ├── Disjoint_set_union.cpp ├── Max-Flow │ ├── dinic_max_flow.cpp │ └── ford-fulkerson_max_flow.cpp ├── bellmanford.cpp ├── dijkstra.cpp ├── floyd_warshall.cpp ├── kruskal.cpp ├── prims.cpp └── scc.cpp ├── LICENSE ├── Miscellaneous ├── big-int.cpp ├── compiler_optimization.cpp ├── kotlin_template.kt └── max_histo.cpp ├── Number-Theory ├── chinese_remainder_theorem.cpp ├── ex_gcd.cpp ├── linear_diophantine_eq.cpp └── matrix_expo.cpp ├── README.md └── Strings ├── kmp.cpp ├── rollhash.cpp ├── trie.cpp └── z_function.cpp /Data-Structure/2d_fenwick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int siz=1007; 4 | #define type int 5 | type bit[4][siz][siz]; 6 | int n; 7 | 8 | void update(int x,int y,type val,int idx) 9 | { 10 | for(int i=x;i=0;i=(i&(i+1))-1) 23 | { 24 | for(int j=y;j>=0;j=(j&(j+1))-1) 25 | { 26 | ret+=bit[idx][i][j]; 27 | } 28 | } 29 | return ret; 30 | } 31 | 32 | 33 | void updt(int x1,int y1,int x2,int y2,type val) 34 | { 35 | update(x1,y1,val,0); 36 | update(x1,y2+1,-val,0); 37 | update(x2+1,y1,-val,0); 38 | update(x2+1,y2+1,val,0); 39 | 40 | update(x1,y1,val*(1-y1),1); 41 | update(x1,y2+1,val*y2,1); 42 | update(x2+1,y1,val*(y1-1),1); 43 | update(x2+1,y2+1,-val*y2,1); 44 | 45 | update(x1,y1,val*(1-x1),2); 46 | update(x1,y2+1,val*(x1-1),2); 47 | update(x2+1,y1,val*x2,2); 48 | update(x2+1,y2+1,-val*x2,2); 49 | 50 | update(x1,y1,(x1-1)*(y1-1)*val,3); 51 | update(x1,y2+1,-y2*(x1-1)*val,3); 52 | update(x2+1,y1,-x2*(y1-1)*val,3); 53 | update(x2+1,y2+1,x2*y2*val,3); 54 | } 55 | 56 | 57 | 58 | 59 | type get(int x,int y){ 60 | return query(x,y,0)*x*y+query(x,y,1)*x+query(x,y,2)*y+query(x,y,3); 61 | } 62 | 63 | type qry(int x1,int y1,int x2,int y2) 64 | { 65 | return get(x2,y2)-get(x1-1,y2)-get(x2,y1-1)+get(x1-1,y1-1); 66 | } 67 | 68 | 69 | int main() 70 | { 71 | 72 | printf("runn"); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Data-Structure/fenwick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define type int 4 | const int siz=2e5+7; 5 | 6 | // 0 based indexing query idx=(idx & (idx+1))-1 amd update idx = (idx | (idx+1) ) 7 | // 1 based indexing query idx-=(idx & -idx) and update idx+=(idx & -idx) 8 | // complexity O(logn) per query and update and space complexity O(n) 9 | type bit1[siz],bit2[siz]; 10 | type get(type *bit,int idx) 11 | { 12 | type ret = 0; 13 | for(int i=idx; i>=0; i=(i&(i+1))-1) ret+=bit[i]; 14 | return ret; 15 | } 16 | void updt(type *bit,int idx,int val) 17 | { 18 | for(int i=idx;i 2 | using namespace std; 3 | const int siz=1e5+7; 4 | // t = 1st parent; l = level , par = ancestors 5 | int n,t[siz],l[siz],par[siz][20]; 6 | vectorv[siz]; 7 | void dfs(int x,int from,int level=0) 8 | { 9 | l[x]=level; 10 | t[x]=from; 11 | for(int i=0;i=0;i--) 26 | { 27 | if(l[p]-(1<=l[q]) p=par[p][i]; 28 | } 29 | if(p==q) return p; 30 | for(int i=log;i>=0;i--) 31 | { 32 | if(par[p][i]!=-1&&par[p][i]!=par[q][i]) 33 | p=par[p][i],q=par[q][i]; 34 | } 35 | return t[p]; 36 | } 37 | int main() 38 | { 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Data-Structure/mo's.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int siz=1e5+7; 4 | int block; 5 | struct query{ 6 | int l,r,in; 7 | bool operator<(const query& x)const{ 8 | if(l/block!=x.l/block) return l/blockl){ 22 | sum+=a[currl-1]; currl--; 23 | } 24 | while(currr<=r){ 25 | sum+=a[currr]; currr++; 26 | } 27 | while(currlr+1){ 31 | sum-=a[currr-1]; currr--; 32 | } 33 | ans[q[i].in]= sum; 34 | } 35 | 36 | } 37 | int main() 38 | { 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Data-Structure/ost.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | #include 5 | using namespace __gnu_pbds; 6 | 7 | template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; 8 | int main() 9 | { 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Data-Structure/segment_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int siz=1e5+7; 4 | int arr[siz]; 5 | struct info 6 | { 7 | int prop,sum; 8 | }tree[4*siz]; 9 | void init(int node,int b,int e){ 10 | if(b==e){ 11 | tree[node].sum=arr[b]; tree[node].prop=0; 12 | return; 13 | } 14 | int mid=(b+e)>>1; 15 | int left=node<<1; 16 | int right=(node<<1)+1; 17 | init(left,b,mid); init(right,mid+1,e); 18 | tree[node].sum=tree[left].sum+tree[right].sum; tree[node].prop=0; 19 | } 20 | void update(int node,int b,int e,int i,int j,int x) 21 | { 22 | if(i>e||j=i&&e<=j){ 24 | tree[node].sum+=(((e-b)+1)*x); tree[node].prop+=x; 25 | return; 26 | } 27 | int mid=(b+e)>>1; 28 | int left=node<<1; 29 | int right=(node<<1)+1; 30 | update(left,b,mid,i,j,x); update(right,mid+1,e,i,j,x); 31 | tree[node].sum=tree[left].sum+tree[right].sum+(((e-b)+1)*tree[node].prop); 32 | } 33 | int query(int node,int b,int e,int i,int j,int x=0) 34 | { 35 | if(i>e||j=i&&e<=j) return tree[node].sum+(((e-b)+1)*x); 37 | int mid=(b+e)>>1; 38 | int left=node<<1; 39 | int right=(node<<1)+1; 40 | int a=query(left,b,mid,i,j,x+tree[node].prop); 41 | int b1=query(right,mid+1,e,i,j,x+tree[node].prop); 42 | return a+b1; 43 | } 44 | int main() 45 | { 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Data-Structure/sliding_range_minimum_query.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef pair pii; 4 | const int siz=1e5+7; 5 | dequem; 6 | int val,a[siz],n,q; 7 | void rmq(int q) 8 | { 9 | for(int i=0;i=val) 12 | m.pop_back(); 13 | m.push_back({val,i}); 14 | while(!m.empty()&&m.front().second<=i-q) 15 | m.pop_front(); 16 | if(i>=q-1) 17 | cout<>n; 23 | for(int i=0;i>a[i]; 25 | cin>>q; 26 | rmq(q); 27 | } 28 | -------------------------------------------------------------------------------- /Graphs/Disjoint_set_union.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int siz=1e5+7; 4 | int arr[siz],ran[siz]; 5 | int find_set(int x) 6 | { 7 | if(x!=arr[x]) 8 | arr[x]=find_set(arr[x]); 9 | return arr[x]; 10 | } 11 | void create_set(int x) 12 | { 13 | arr[x]=x; 14 | ran[x]=0; 15 | return; 16 | } 17 | void merge_set(int x,int y) 18 | { 19 | int px=find_set(x); 20 | int py=find_set(y); 21 | if(ran[px]>ran[py]) 22 | arr[py]=px; 23 | else 24 | arr[px]=py; 25 | if(ran[px]==ran[py]) 26 | ran[py]++; 27 | } 28 | int main() 29 | { 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Graphs/Max-Flow/dinic_max_flow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define type int 4 | const int inf = 1000000000; 5 | struct edge{ 6 | int u,v; 7 | type f,c; 8 | }; 9 | struct Dinic{ 10 | int n,m=0,scaling; 11 | vector >graph; 12 | vectored; // edges 13 | int s,t; 14 | vectorlevel,ptr; // ptr to next ptr in dfs 15 | Dinic(int n,int s,int t,int scaling): n(n),s(s),t(t),scaling(scaling){ 16 | graph.resize(n+1); 17 | level.resize(n+1); 18 | ptr.resize(n+1); 19 | } 20 | void add_edge(int u,int v,type f){ 21 | edge e; 22 | e.u=u,e.v=v,e.f=0,e.c=f; 23 | graph[u].push_back((int)ed.size()); 24 | ed.push_back(e); 25 | e.u=v,e.v=u,e.f=e.c=f; 26 | graph[v].push_back((int)ed.size()); 27 | ed.push_back(e); 28 | m+=2; 29 | } 30 | bool bfs(type lim){ 31 | queueq; 32 | q.push(s); 33 | for(int i=1;i<=n;i++) level[i]=inf; 34 | level[s]=0; 35 | while(!q.empty()&&level[t]==inf){ 36 | int u=q.front();q.pop(); 37 | for(int i=0;i=lim){ 41 | q.push(v); 42 | level[v]=level[u]+1; 43 | } 44 | } 45 | } 46 | return level[t]!=inf; 47 | } 48 | int dfs(int x,type flow,type minf=INT_MAX){ 49 | if(!flow) return 0; 50 | if(x==t) return flow; 51 | for(;ptr[x]=flow){ 55 | type pushed=dfs(v,flow); 56 | // int pushed=dfs(v,flow,min(minf,ed[id].c-ed[id].f)); 57 | if(pushed){ 58 | ed[id].f+=flow; 59 | ed[id^1].f-=flow; 60 | return flow; 61 | //ed[id].f+=pushed; 62 | // ed[id^1].f-=pushed; 63 | // return pushed; 64 | } 65 | } 66 | } 67 | return 0; 68 | } 69 | type dinic(){ 70 | type flow=0; 71 | for(type lim=(scaling)?(1<<30):1;lim>0;){ 72 | if(!bfs(lim)){ 73 | lim>>=1; 74 | continue; 75 | } 76 | for(int i=1;i<=n;i++) ptr[i]=0; 77 | int pushed; 78 | while(pushed=dfs(s,lim)){ 79 | flow+=pushed; 80 | } 81 | } 82 | return flow; 83 | } 84 | }; 85 | int main() 86 | { 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Graphs/Max-Flow/ford-fulkerson_max_flow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define pii pair 5 | #define siz 1000 6 | #define type int 7 | int edges,u,v,n=6; 8 | type rgraph[siz][siz], graph[siz][siz],parent[siz],flow; 9 | bool bfs(int s,int t){ 10 | bool vis[siz]; 11 | memset(vis,false,sizeof(vis)); 12 | queueq; 13 | q.push(s); 14 | parent[s]=-1; vis[s]=1; 15 | while(!q.empty()){ 16 | int u=q.front(); q.pop(); 17 | for(int i=1; i<=n; i++){ 18 | if(!vis[i]&&rgraph[u][i]){ 19 | vis[i]=true; 20 | parent[i]=u; 21 | q.push(i); 22 | if(i==t) break; 23 | } 24 | } 25 | if(vis[t]) break; 26 | } 27 | return (vis[t]==true); 28 | 29 | } 30 | type getmaxflow(int s,int t){ 31 | type max_flow=0; 32 | //creating residual graph 33 | for(int i=1; i<=n; i++) 34 | for(int j=1; j<=n; j++) 35 | rgraph[i][j]=graph[i][j]; 36 | // while no augment path found 37 | // can use dfs or bfs or disjktra in path finding section as required 38 | while(bfs(s,t)){ 39 | type path_cap=INT_MAX; 40 | for(int i=t; i!=s; i=parent[i]){ 41 | int j=parent[i]; 42 | path_cap=min(path_cap,rgraph[j][i]); // parent to child 43 | 44 | } 45 | for(int i=t; i!=s; i=parent[i]){ 46 | int j=parent[i]; 47 | rgraph[j][i]-=path_cap; // parent to child 48 | rgraph[i][j]+=path_cap; // child to parent 49 | } 50 | 51 | max_flow+=path_cap; 52 | } 53 | return max_flow; 54 | } 55 | int main() 56 | { 57 | cout< 2 | using namespace std; 3 | #define type int 4 | #define Maxx 1001 5 | const int inf=100000; 6 | struct edge{ 7 | int u,v; 8 | type w; 9 | }; 10 | vectorv; 11 | type dis[Maxx]; 12 | int n,m; 13 | bool bellmanford(int s){ 14 | // s is the source 15 | for(int i=1;i<=n;i++) dis[i]=inf; 16 | dis[s]=0; 17 | // will update shortest distances at most level n-1 18 | for(int i=1;idis[x]+z){ 25 | dis[y]=dis[x]+z; 26 | f=true; 27 | } 28 | } 29 | if(!f) return false; 30 | } 31 | // if it updates nth time then it must have a negative cycle 32 | for(int j=0; jdis[x]+z) return true; 37 | } 38 | return false; 39 | } 40 | int main() 41 | { 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Graphs/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int siz=1e5+7; 4 | const int inf=INT_MAX; 5 | struct node{ 6 | int a,w; 7 | bool operator<(const node& x)const{ 8 | return w>x.w; 9 | } 10 | }; 11 | int nodes,edges,dis[siz]; 12 | vectorcost[siz]; 13 | void solve(int x) 14 | { 15 | for(int i=1;i<=nodes;i++) dis[i]=inf; 16 | priority_queueq; 17 | dis[x]=0; 18 | q.push({x,0}); 19 | while(!q.empty()){ 20 | node u=q.top(); q.pop(); 21 | x=u.a; 22 | if(u.w!=dis[x]) continue; 23 | for(int i=0;icost[i][k]+cost[k][j]) 5 | cost[i][j]=cost[i][k]+cost[k][j]; 6 | 7 | -------------------------------------------------------------------------------- /Graphs/kruskal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define type int 4 | const int siz=1e5+7; 5 | struct node{ 6 | int u,v; 7 | type w; 8 | bool operator<(const node& x)const{ 9 | return wE; 13 | int p[siz]; 14 | int find(int x){ 15 | if(p[x]!=x) p[x]=find(p[x]); 16 | return p[x]; 17 | } 18 | int kruskal() 19 | { 20 | sort(E.begin(),E.end()); 21 | int sz=E.size(); 22 | type ans=0; 23 | for(int i=0;i 2 | using namespace std; 3 | const int siz=1e5+7; 4 | const int inf=INT_MAX; 5 | #define type int 6 | struct node{ 7 | int a,w; 8 | bool operator<(const node& x)const{ 9 | return w>x.w; 10 | } 11 | }; 12 | vectorcost[siz]; 13 | priority_queueq; 14 | int weight[siz]; 15 | bool vis[siz]; 16 | int n; 17 | type prims(int s){ 18 | for(int i=1;i<=n;i++) weight[i]=inf; 19 | weight[s]=0; 20 | q.push({s,0}); 21 | type ans=0; 22 | while(!q.empty()){ 23 | node u=q.top(); q.pop(); 24 | if(vis[u.a]) continue; 25 | vis[u.a]=1; 26 | ans+=u.w; 27 | for(int i=0;ix.w){ 30 | weight[x.a]=x.w; 31 | q.push(x); 32 | } 33 | } 34 | } 35 | return ans; 36 | } 37 | int main() 38 | { 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Graphs/scc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int siz=1e5+7; 4 | stacks; 5 | bool vis[siz]; 6 | int n; 7 | vectorv[siz],uv[siz],com[siz]; 8 | void dfs(int x){ 9 | vis[x]=true; 10 | for(int i=0;i 2 | using namespace std; 3 | 4 | const int BASE_DIGITS = 9; 5 | const int BASE = 1000000000; 6 | 7 | struct BigInt { 8 | int sign; 9 | vector a; 10 | 11 | // -------------------- Constructors -------------------- 12 | // Default constructor. 13 | BigInt() : sign(1) {} 14 | 15 | // Constructor from long long. 16 | BigInt(long long v) { 17 | *this = v; 18 | } 19 | BigInt& operator = (long long v) { 20 | sign = 1; 21 | if (v < 0) { 22 | sign = -1; 23 | v = -v; 24 | } 25 | a.clear(); 26 | for (; v > 0; v = v / BASE) 27 | a.push_back(v % BASE); 28 | return *this; 29 | } 30 | 31 | // Initialize from string. 32 | BigInt(const string& s) { 33 | read(s); 34 | } 35 | 36 | // -------------------- Input / Output -------------------- 37 | void read(const string& s) { 38 | sign = 1; 39 | a.clear(); 40 | int pos = 0; 41 | while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) { 42 | if (s[pos] == '-') 43 | sign = -sign; 44 | ++pos; 45 | } 46 | for (int i = s.size() - 1; i >= pos; i -= BASE_DIGITS) { 47 | int x = 0; 48 | for (int j = max(pos, i - BASE_DIGITS + 1); j <= i; j++) 49 | x = x * 10 + s[j] - '0'; 50 | a.push_back(x); 51 | } 52 | trim(); 53 | } 54 | friend istream& operator>>(istream &stream, BigInt &v) { 55 | string s; 56 | stream >> s; 57 | v.read(s); 58 | return stream; 59 | } 60 | 61 | friend ostream& operator<<(ostream &stream, const BigInt &v) { 62 | if (v.sign == -1 && !v.isZero()) 63 | stream << '-'; 64 | stream << (v.a.empty() ? 0 : v.a.back()); 65 | for (int i = (int) v.a.size() - 2; i >= 0; --i) 66 | stream << setw(BASE_DIGITS) << setfill('0') << v.a[i]; 67 | return stream; 68 | } 69 | 70 | // -------------------- Comparison -------------------- 71 | bool operator<(const BigInt &v) const { 72 | if (sign != v.sign) 73 | return sign < v.sign; 74 | if (a.size() != v.a.size()) 75 | return a.size() * sign < v.a.size() * v.sign; 76 | for (int i = ((int) a.size()) - 1; i >= 0; i--) 77 | if (a[i] != v.a[i]) 78 | return a[i] * sign < v.a[i] * sign; 79 | return false; 80 | } 81 | 82 | bool operator>(const BigInt &v) const { 83 | return v < *this; 84 | } 85 | bool operator<=(const BigInt &v) const { 86 | return !(v < *this); 87 | } 88 | bool operator>=(const BigInt &v) const { 89 | return !(*this < v); 90 | } 91 | bool operator==(const BigInt &v) const { 92 | return !(*this < v) && !(v < *this); 93 | } 94 | bool operator!=(const BigInt &v) const { 95 | return *this < v || v < *this; 96 | } 97 | 98 | // Returns: 99 | // 0 if |x| == |y| 100 | // -1 if |x| < |y| 101 | // 1 if |x| > |y| 102 | friend int __compare_abs(const BigInt& x, const BigInt& y) { 103 | if (x.a.size() != y.a.size()) { 104 | return x.a.size() < y.a.size() ? -1 : 1; 105 | } 106 | 107 | for (int i = ((int) x.a.size()) - 1; i >= 0; --i) { 108 | if (x.a[i] != y.a[i]) { 109 | return x.a[i] < y.a[i] ? -1 : 1; 110 | } 111 | } 112 | return 0; 113 | } 114 | 115 | // -------------------- Unary operator - and operators +- -------------------- 116 | BigInt operator-() const { 117 | BigInt res = *this; 118 | if (isZero()) return res; 119 | 120 | res.sign = -sign; 121 | return res; 122 | } 123 | 124 | // Note: sign ignored. 125 | void __internal_add(const BigInt& v) { 126 | if (a.size() < v.a.size()) { 127 | a.resize(v.a.size(), 0); 128 | } 129 | for (int i = 0, carry = 0; i < (int) max(a.size(), v.a.size()) || carry; ++i) { 130 | if (i == (int) a.size()) a.push_back(0); 131 | 132 | a[i] += carry + (i < (int) v.a.size() ? v.a[i] : 0); 133 | carry = a[i] >= BASE; 134 | if (carry) a[i] -= BASE; 135 | } 136 | } 137 | 138 | // Note: sign ignored. 139 | void __internal_sub(const BigInt& v) { 140 | for (int i = 0, carry = 0; i < (int) v.a.size() || carry; ++i) { 141 | a[i] -= carry + (i < (int) v.a.size() ? v.a[i] : 0); 142 | carry = a[i] < 0; 143 | if (carry) a[i] += BASE; 144 | } 145 | this->trim(); 146 | } 147 | 148 | BigInt operator += (const BigInt& v) { 149 | if (sign == v.sign) { 150 | __internal_add(v); 151 | } else { 152 | if (__compare_abs(*this, v) >= 0) { 153 | __internal_sub(v); 154 | } else { 155 | BigInt vv = v; 156 | swap(*this, vv); 157 | __internal_sub(vv); 158 | } 159 | } 160 | return *this; 161 | } 162 | 163 | BigInt operator -= (const BigInt& v) { 164 | if (sign == v.sign) { 165 | if (__compare_abs(*this, v) >= 0) { 166 | __internal_sub(v); 167 | } else { 168 | BigInt vv = v; 169 | swap(*this, vv); 170 | __internal_sub(vv); 171 | this->sign = -this->sign; 172 | } 173 | } else { 174 | __internal_add(v); 175 | } 176 | return *this; 177 | } 178 | 179 | // Optimize operators + and - according to 180 | // https://stackoverflow.com/questions/13166079/move-semantics-and-pass-by-rvalue-reference-in-overloaded-arithmetic 181 | template< typename L, typename R > 182 | typename std::enable_if< 183 | std::is_convertible::value && 184 | std::is_convertible::value && 185 | std::is_lvalue_reference::value, 186 | BigInt>::type friend operator + (L&& l, R&& r) { 187 | BigInt result(std::forward(l)); 188 | result += r; 189 | return result; 190 | } 191 | template< typename L, typename R > 192 | typename std::enable_if< 193 | std::is_convertible::value && 194 | std::is_convertible::value && 195 | std::is_rvalue_reference::value, 196 | BigInt>::type friend operator + (L&& l, R&& r) { 197 | BigInt result(std::move(r)); 198 | result += l; 199 | return result; 200 | } 201 | 202 | template< typename L, typename R > 203 | typename std::enable_if< 204 | std::is_convertible::value && 205 | std::is_convertible::value, 206 | BigInt>::type friend operator - (L&& l, R&& r) { 207 | BigInt result(std::forward(l)); 208 | result -= r; 209 | return result; 210 | } 211 | 212 | // -------------------- Operators * / % -------------------- 213 | friend pair divmod(const BigInt& a1, const BigInt& b1) { 214 | assert(b1 > 0); // divmod not well-defined for b < 0. 215 | 216 | long long norm = BASE / (b1.a.back() + 1); 217 | BigInt a = a1.abs() * norm; 218 | BigInt b = b1.abs() * norm; 219 | BigInt q = 0, r = 0; 220 | q.a.resize(a.a.size()); 221 | 222 | for (int i = a.a.size() - 1; i >= 0; i--) { 223 | r *= BASE; 224 | r += a.a[i]; 225 | long long s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()]; 226 | long long s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1]; 227 | long long d = ((long long) BASE * s1 + s2) / b.a.back(); 228 | r -= b * d; 229 | while (r < 0) { 230 | r += b, --d; 231 | } 232 | q.a[i] = d; 233 | } 234 | 235 | q.sign = a1.sign * b1.sign; 236 | r.sign = a1.sign; 237 | q.trim(); 238 | r.trim(); 239 | auto res = make_pair(q, r / norm); 240 | if (res.second < 0) res.second += b1; 241 | return res; 242 | } 243 | BigInt operator/(const BigInt &v) const { 244 | return divmod(*this, v).first; 245 | } 246 | 247 | BigInt operator%(const BigInt &v) const { 248 | return divmod(*this, v).second; 249 | } 250 | 251 | void operator/=(int v) { 252 | assert(v > 0); // operator / not well-defined for v <= 0. 253 | if (llabs(v) >= BASE) { 254 | *this /= BigInt(v); 255 | return ; 256 | } 257 | if (v < 0) 258 | sign = -sign, v = -v; 259 | for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) { 260 | long long cur = a[i] + rem * (long long) BASE; 261 | a[i] = (int) (cur / v); 262 | rem = (int) (cur % v); 263 | } 264 | trim(); 265 | } 266 | 267 | BigInt operator/(int v) const { 268 | assert(v > 0); // operator / not well-defined for v <= 0. 269 | 270 | if (llabs(v) >= BASE) { 271 | return *this / BigInt(v); 272 | } 273 | BigInt res = *this; 274 | res /= v; 275 | return res; 276 | } 277 | void operator/=(const BigInt &v) { 278 | *this = *this / v; 279 | } 280 | 281 | long long operator%(long long v) const { 282 | assert(v > 0); // operator / not well-defined for v <= 0. 283 | assert(v < BASE); 284 | int m = 0; 285 | for (int i = a.size() - 1; i >= 0; --i) 286 | m = (a[i] + m * (long long) BASE) % v; 287 | return m * sign; 288 | } 289 | 290 | void operator*=(int v) { 291 | if (llabs(v) >= BASE) { 292 | *this *= BigInt(v); 293 | return ; 294 | } 295 | if (v < 0) 296 | sign = -sign, v = -v; 297 | for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) { 298 | if (i == (int) a.size()) 299 | a.push_back(0); 300 | long long cur = a[i] * (long long) v + carry; 301 | carry = (int) (cur / BASE); 302 | a[i] = (int) (cur % BASE); 303 | //asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base)); 304 | /* 305 | int val; 306 | __asm { 307 | lea esi, cur 308 | mov eax, [esi] 309 | mov edx, [esi+4] 310 | mov ecx, base 311 | div ecx 312 | mov carry, eax 313 | mov val, edx; 314 | } 315 | a[i] = val; 316 | */ 317 | } 318 | trim(); 319 | } 320 | 321 | BigInt operator*(int v) const { 322 | if (llabs(v) >= BASE) { 323 | return *this * BigInt(v); 324 | } 325 | BigInt res = *this; 326 | res *= v; 327 | return res; 328 | } 329 | 330 | // Convert BASE 10^old --> 10^new. 331 | static vector convert_base(const vector &a, int old_digits, int new_digits) { 332 | vector p(max(old_digits, new_digits) + 1); 333 | p[0] = 1; 334 | for (int i = 1; i < (int) p.size(); i++) 335 | p[i] = p[i - 1] * 10; 336 | vector res; 337 | long long cur = 0; 338 | int cur_digits = 0; 339 | for (int i = 0; i < (int) a.size(); i++) { 340 | cur += a[i] * p[cur_digits]; 341 | cur_digits += old_digits; 342 | while (cur_digits >= new_digits) { 343 | res.push_back((long long)(cur % p[new_digits])); 344 | cur /= p[new_digits]; 345 | cur_digits -= new_digits; 346 | } 347 | } 348 | res.push_back((int) cur); 349 | while (!res.empty() && !res.back()) 350 | res.pop_back(); 351 | return res; 352 | } 353 | 354 | void fft(vector > & a, bool invert) const { 355 | int n = (int) a.size(); 356 | 357 | for (int i = 1, j = 0; i < n; ++i) { 358 | int bit = n >> 1; 359 | for (; j >= bit; bit >>= 1) 360 | j -= bit; 361 | j += bit; 362 | if (i < j) 363 | swap(a[i], a[j]); 364 | } 365 | 366 | for (int len = 2; len <= n; len <<= 1) { 367 | double ang = 2 * 3.14159265358979323846 / len * (invert ? -1 : 1); 368 | complex wlen(cos(ang), sin(ang)); 369 | for (int i = 0; i < n; i += len) { 370 | complex w(1); 371 | for (int j = 0; j < len / 2; ++j) { 372 | complex u = a[i + j]; 373 | complex v = a[i + j + len / 2] * w; 374 | a[i + j] = u + v; 375 | a[i + j + len / 2] = u - v; 376 | w *= wlen; 377 | } 378 | } 379 | } 380 | if (invert) 381 | for (int i = 0; i < n; ++i) 382 | a[i] /= n; 383 | } 384 | 385 | void multiply_fft(const vector &a, const vector &b, vector &res) const { 386 | vector > fa(a.begin(), a.end()); 387 | vector > fb(b.begin(), b.end()); 388 | int n = 1; 389 | while (n < (int) max(a.size(), b.size())) 390 | n <<= 1; 391 | n <<= 1; 392 | fa.resize(n); 393 | fb.resize(n); 394 | 395 | fft(fa, false); 396 | fft(fb, false); 397 | for (int i = 0; i < n; ++i) 398 | fa[i] *= fb[i]; 399 | fft(fa, true); 400 | 401 | res.resize(n); 402 | long long carry = 0; 403 | for (int i = 0; i < n; ++i) { 404 | long long t = (long long) (fa[i].real() + 0.5) + carry; 405 | carry = t / 1000; 406 | res[i] = t % 1000; 407 | } 408 | } 409 | 410 | BigInt mul_simple(const BigInt &v) const { 411 | BigInt res; 412 | res.sign = sign * v.sign; 413 | res.a.resize(a.size() + v.a.size()); 414 | for (int i = 0; i < (int) a.size(); ++i) 415 | if (a[i]) 416 | for (int j = 0, carry = 0; j < (int) v.a.size() || carry; ++j) { 417 | long long cur = res.a[i + j] + (long long) a[i] * (j < (int) v.a.size() ? v.a[j] : 0) + carry; 418 | carry = (int) (cur / BASE); 419 | res.a[i + j] = (int) (cur % BASE); 420 | } 421 | res.trim(); 422 | return res; 423 | } 424 | 425 | typedef vector vll; 426 | 427 | static vll karatsubaMultiply(const vll &a, const vll &b) { 428 | int n = a.size(); 429 | vll res(n + n); 430 | if (n <= 32) { 431 | for (int i = 0; i < n; i++) 432 | for (int j = 0; j < n; j++) 433 | res[i + j] += a[i] * b[j]; 434 | return res; 435 | } 436 | 437 | int k = n >> 1; 438 | vll a1(a.begin(), a.begin() + k); 439 | vll a2(a.begin() + k, a.end()); 440 | vll b1(b.begin(), b.begin() + k); 441 | vll b2(b.begin() + k, b.end()); 442 | 443 | vll a1b1 = karatsubaMultiply(a1, b1); 444 | vll a2b2 = karatsubaMultiply(a2, b2); 445 | 446 | for (int i = 0; i < k; i++) 447 | a2[i] += a1[i]; 448 | for (int i = 0; i < k; i++) 449 | b2[i] += b1[i]; 450 | 451 | vll r = karatsubaMultiply(a2, b2); 452 | for (int i = 0; i < (int) a1b1.size(); i++) 453 | r[i] -= a1b1[i]; 454 | for (int i = 0; i < (int) a2b2.size(); i++) 455 | r[i] -= a2b2[i]; 456 | 457 | for (int i = 0; i < (int) r.size(); i++) 458 | res[i + k] += r[i]; 459 | for (int i = 0; i < (int) a1b1.size(); i++) 460 | res[i] += a1b1[i]; 461 | for (int i = 0; i < (int) a2b2.size(); i++) 462 | res[i + n] += a2b2[i]; 463 | return res; 464 | } 465 | 466 | BigInt mul_karatsuba(const BigInt &v) const { 467 | vector a6 = convert_base(this->a, BASE_DIGITS, 6); 468 | vector b6 = convert_base(v.a, BASE_DIGITS, 6); 469 | vll a(a6.begin(), a6.end()); 470 | vll b(b6.begin(), b6.end()); 471 | while (a.size() < b.size()) 472 | a.push_back(0); 473 | while (b.size() < a.size()) 474 | b.push_back(0); 475 | while (a.size() & (a.size() - 1)) 476 | a.push_back(0), b.push_back(0); 477 | vll c = karatsubaMultiply(a, b); 478 | BigInt res; 479 | res.sign = sign * v.sign; 480 | long long carry = 0; 481 | for (int i = 0; i < (int) c.size(); i++) { 482 | long long cur = c[i] + carry; 483 | res.a.push_back((int) (cur % 1000000)); 484 | carry = cur / 1000000; 485 | } 486 | res.a = convert_base(res.a, 6, BASE_DIGITS); 487 | res.trim(); 488 | return res; 489 | } 490 | 491 | void operator*=(const BigInt &v) { 492 | *this = *this * v; 493 | } 494 | BigInt operator*(const BigInt &v) const { 495 | if (a.size() * v.a.size() <= 1000111) return mul_simple(v); 496 | if (a.size() > 500111 || v.a.size() > 500111) return mul_fft(v); 497 | return mul_karatsuba(v); 498 | } 499 | 500 | BigInt mul_fft(const BigInt& v) const { 501 | BigInt res; 502 | res.sign = sign * v.sign; 503 | multiply_fft(convert_base(a, BASE_DIGITS, 3), convert_base(v.a, BASE_DIGITS, 3), res.a); 504 | res.a = convert_base(res.a, 3, BASE_DIGITS); 505 | res.trim(); 506 | return res; 507 | } 508 | 509 | // -------------------- Misc -------------------- 510 | BigInt abs() const { 511 | BigInt res = *this; 512 | res.sign *= res.sign; 513 | return res; 514 | } 515 | void trim() { 516 | while (!a.empty() && !a.back()) 517 | a.pop_back(); 518 | if (a.empty()) 519 | sign = 1; 520 | } 521 | 522 | bool isZero() const { 523 | return a.empty() || (a.size() == 1 && !a[0]); 524 | } 525 | 526 | friend BigInt gcd(const BigInt &a, const BigInt &b) { 527 | return b.isZero() ? a : gcd(b, a % b); 528 | } 529 | friend BigInt lcm(const BigInt &a, const BigInt &b) { 530 | return a / gcd(a, b) * b; 531 | } 532 | 533 | friend BigInt sqrt(const BigInt &a1) { 534 | BigInt a = a1; 535 | while (a.a.empty() || a.a.size() % 2 == 1) 536 | a.a.push_back(0); 537 | 538 | int n = a.a.size(); 539 | 540 | int firstDigit = (int) sqrt((double) a.a[n - 1] * BASE + a.a[n - 2]); 541 | int norm = BASE / (firstDigit + 1); 542 | a *= norm; 543 | a *= norm; 544 | while (a.a.empty() || a.a.size() % 2 == 1) 545 | a.a.push_back(0); 546 | 547 | BigInt r = (long long) a.a[n - 1] * BASE + a.a[n - 2]; 548 | firstDigit = (int) sqrt((double) a.a[n - 1] * BASE + a.a[n - 2]); 549 | int q = firstDigit; 550 | BigInt res; 551 | 552 | for(int j = n / 2 - 1; j >= 0; j--) { 553 | for(; ; --q) { 554 | BigInt r1 = (r - (res * 2 * BigInt(BASE) + q) * q) * BigInt(BASE) * BigInt(BASE) + (j > 0 ? (long long) a.a[2 * j - 1] * BASE + a.a[2 * j - 2] : 0); 555 | if (r1 >= 0) { 556 | r = r1; 557 | break; 558 | } 559 | } 560 | res *= BASE; 561 | res += q; 562 | 563 | if (j > 0) { 564 | int d1 = res.a.size() + 2 < r.a.size() ? r.a[res.a.size() + 2] : 0; 565 | int d2 = res.a.size() + 1 < r.a.size() ? r.a[res.a.size() + 1] : 0; 566 | int d3 = res.a.size() < r.a.size() ? r.a[res.a.size()] : 0; 567 | q = ((long long) d1 * BASE * BASE + (long long) d2 * BASE + d3) / (firstDigit * 2); 568 | } 569 | } 570 | 571 | res.trim(); 572 | return res / norm; 573 | } 574 | }; 575 | 576 | int main(){ 577 | return 0; 578 | } -------------------------------------------------------------------------------- /Miscellaneous/compiler_optimization.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 3 | #pragma GCC optimize("unroll-loops") 4 | -------------------------------------------------------------------------------- /Miscellaneous/kotlin_template.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | fun next() = readLine()!! 3 | fun nextInt() = readLine()!!.toInt() 4 | fun nextLong() = readLine()!!.toLong() 5 | fun nextInts() = readLine()!!.split(" ").map { it.toInt() } 6 | fun nextStrings() = readLine()!!.split(" ") 7 | fun nextLongs() = readLine()!!.split(" ").map { it.toLong() } 8 | fun main(){ 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Miscellaneous/max_histo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int max_area(int ar[],int m) 4 | { 5 | int a,ma_area,i=0; 6 | a=ma_area=0; 7 | stackarea; 8 | while(i>n>>m) 38 | { 39 | int a[100]={0}; 40 | m_area=0; 41 | for(int i=0;i>in; 46 | if(in) 47 | a[j]+=in; 48 | else 49 | a[j]=0; 50 | } 51 | m_area=max(max_area(a,m),m_area); 52 | } 53 | cout< 2 | using namespace std; 3 | #define type int 4 | type exgcd(type a,type b,type &x,type &y){ 5 | if(b==0){ 6 | x=1; 7 | y=0; 8 | return a; 9 | } 10 | type x1,y1; 11 | type d=exgcd(b,a%b,x1,y1); 12 | x=y1; 13 | y=x1-(a/b)*y1; 14 | return d; 15 | } 16 | type Lcm(type a,type b){ 17 | return (a/__gcd(a,b))*b; 18 | } 19 | struct crt{ 20 | type n; 21 | type *a,*b; 22 | crt(type x,type *aa,type *bb):n(x),a(aa),b(bb){} 23 | type solve(){ 24 | type ans=a[0]; 25 | type lcm= b[0]; 26 | for(int i=1;i 2 | using namespace std; 3 | struct type{int x,y,d;}; 4 | type exgcd(int a,int b){ 5 | if(b==0) return {1,0,a}; 6 | type xx=exgcd(b,a%b); 7 | return {xx.y,xx.x-((a/b)*xx.y),xx.d}; 8 | } 9 | int main() 10 | { 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Number-Theory/linear_diophantine_eq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int gcd(int a,int b){ 4 | return !b? a:gcd(b,a%b); 5 | } 6 | int exgcd(int a,int b,int &x,int &y){ 7 | if(b==0){ 8 | x=1;y=0; 9 | return a; 10 | } 11 | int x1,y1; 12 | int g= exgcd(b,a%b,x1,y1); 13 | x=y1; 14 | y=x1-y1*(a/b); 15 | return g; 16 | } 17 | bool find_any_sol(int a,int b,int c,int &x,int &y,int &g){ 18 | g= exgcd(abs(a),abs(b),x,y); 19 | if(c%g) return false; 20 | x*=c/g; 21 | y*=c/g; 22 | if(a<0) x=-x; 23 | if(b<0) y=-y; 24 | return true; 25 | } 26 | void shift_solution(int &x,int &y,int a,int b,int cnt){ 27 | x+=cnt*b; 28 | y-=cnt*a; 29 | } 30 | int find_all_sol(int a,int b,int c,int minx,int maxx,int miny,int maxy){ 31 | if(a==0&&b==0) return (maxx-minx+1)*(maxy-miny+1)*(c==0); 32 | if(a==0) return (maxx-minx+1)*(c%b==0)*(c/b>=miny&&c/b<=maxy); 33 | if(b==0) return (maxy-miny+1)*(c%a==0)*(c/a>=minx&&c/a<=maxx); 34 | int x,y,g; 35 | if(!find_any_sol(a,b,c,x,y,g)) return 0; 36 | a/=g; 37 | b/=g; 38 | int sign_a= (a>0)?1:-1; 39 | int sign_b= (b>0)?1:-1; 40 | shift_solution(x,y,a,b,(minx-x)/b); 41 | if(xmaxx) 44 | return 0; 45 | int lx1=x; 46 | 47 | shift_solution(x,y,a,b,(maxx-x)/b); 48 | if(x>maxx) 49 | shift_solution(x,y,a,b,-sign_b); 50 | int rx1=x; 51 | shift_solution(x,y,a,b,-(miny-y)/a); 52 | if(ymaxy) return 0; 55 | int lx2=x; 56 | shift_solution(x,y,a,b,-(maxy-y)/a); 57 | if(y>maxy) 58 | shift_solution(x,y,a,b,sign_a); 59 | int rx2=x; 60 | if(lx2>rx2) swap(lx2,rx2); 61 | int lx=max(lx1,lx2); 62 | int rx=min(rx1,rx2); 63 | if(lx>rx) return 0; 64 | return (rx-lx)/abs(b)+1; 65 | 66 | } 67 | int main(){ 68 | // if a =0 swap (a,b) 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Number-Theory/matrix_expo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll mod = 1e9+7; 5 | ll mod2=mod*mod; 6 | struct matrix 7 | { 8 | vector< vector >mat; 9 | int rows,cols; 10 | matrix() {} 11 | matrix(vector< vector >v): mat(v),rows((int)v.size()),cols((int)v[0].size()) {} 12 | static matrix indentity(int nn) 13 | { 14 | vector >ind(nn,vector(nn,0)); 15 | for(int i=0; i > res(cols,vector(x.cols,0)); 24 | for(int i=0; i=mod2) 33 | { 34 | tmp-=mod2; 35 | } 36 | } 37 | res[i][j]=tmp%mod; 38 | } 39 | } 40 | return matrix(res); 41 | } 42 | }; 43 | 44 | matrix power[107]; 45 | matrix M; 46 | void save() 47 | { 48 | power[0]=M; 49 | for(int i=1; i<=100; i++) 50 | power[i]=power[i-1]*power[i-1]; 51 | } 52 | 53 | matrix expo(int p) 54 | { 55 | matrix result=matrix::indentity((int)power[0].mat[0].size()); 56 | int cnt=0; 57 | while(p) 58 | { 59 | if(p&1) 60 | { 61 | result=result*power[cnt]; 62 | } 63 | cnt++; 64 | p>>=1; 65 | } 66 | return result; 67 | } 68 | 69 | vector< vector >v(2,(vector(2,0))); 70 | void solve() 71 | { 72 | v[0][0]=v[1][0]=v[0][1]=1; 73 | M.mat=v; 74 | M.rows=v.size(); 75 | M.cols=v[0].size(); 76 | matrix initial({{1ll},{0ll}}); 77 | save(); 78 | int n; 79 | while(cin>>n) 80 | { 81 | n-=1; 82 | cout<<(expo(n)*initial).mat[0][0]< ### Graphs 5 | > * Dijkstra 6 | > * Bellmanford 7 | > * Floyd Warshal 8 | > * Prims 9 | > * Kruskal 10 | > * Disjoint Set Union 11 | > * Strongly Connected Components 12 | > * Maximum Flow (Ford Fulkerson) 13 | >> * Edmond Karp 14 | >> * Dinic (scalling) 15 | > ### Data Structures 16 | > * Segment Tree 17 | > * Fenwick Tree 18 | >> * 1d & 2d with range update & range query 19 | > * Mo's Algorithm 20 | > * Sliding Range 21 | > * Lowest Common Ancestor (Sparse Table) 22 | > * Ordered Set (Policy Based Data Structure in G++) 23 | > ### String 24 | > * Trie prefix tree 25 | > * Rolling Hash 26 | > * KMP Algorithm 27 | > * Z Function 28 | > ### Number Theory 29 | > * Matrix Exponentiation 30 | > * Chinese Remainder Theorem 31 | > * Extended GCD 32 | > * Linear Diophantine Equation 33 | > ### Miscellaneous 34 | > * Big Integer C++ library 35 | > * GCC Compiler Optimization 36 | > * Maximum Histogram 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Strings/kmp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct kmp{ 5 | int n; 6 | vectorfailure; 7 | kmp(const string &s): failure((int)s.length()+1,0),n((int)s.length()){ 8 | // generating failure table for pattern 9 | for(int i=2;i<=n;i++) 10 | { 11 | int j=failure[i-1]; 12 | while(true){ 13 | if(s[i-1]==s[j]){ 14 | j++; 15 | break; 16 | } 17 | else if(j==0) break; 18 | else j=failure[j]; 19 | } 20 | failure[i]=j; 21 | } 22 | } 23 | 24 | bool match(const string &s,const string &text) 25 | { 26 | int m=(int)text.length(); 27 | int i=0,j=0; 28 | while(true){ 29 | if(j==m) return false; 30 | if(text[j]==s[i]){ 31 | i++; j++; 32 | if(i==n) return true; 33 | } 34 | else if(i==0) j++; 35 | else i=failure[i]; 36 | } 37 | return false; 38 | } 39 | }; 40 | 41 | 42 | 43 | int main() 44 | { 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Strings/rollhash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef unsigned long long ull; 5 | typedef long long ll; 6 | 7 | 8 | int gen_base(const int bef,const int aft) 9 | { 10 | auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); 11 | mt19937 mt_rand(seed); 12 | int base=uniform_int_distribution(bef+1,aft)(mt_rand); 13 | return base%2==0? base-1 : base; 14 | } 15 | 16 | 17 | struct rollhash 18 | { 19 | static const int mod = (int)1e9+123; // mod 20 | vectorpref1; // hash by mod 21 | vectorpref2; // hash by 2^64 22 | 23 | static vectorpow1; // pow of base by mod 24 | static vectorpow2; // pow of base by 2^64 25 | 26 | static int base; // base of hash 27 | 28 | inline void init(const string& s) 29 | { 30 | pref1.push_back(0); 31 | pref2.push_back(0); 32 | 33 | int n=s.length(); 34 | 35 | while((int)pow1.size()<=n) 36 | { 37 | pow1.push_back((1ll*pow1.back()*base)%mod); 38 | pow2.push_back(pow2.back()*base); 39 | } 40 | 41 | for(int i=0; is[i]); 44 | 45 | pref1.push_back((pref1[i]+1ll*(s[i]*pow1[i]))%mod); 46 | pref2.push_back(pref2[i]+(s[i]*pow2[i])); 47 | } 48 | 49 | } 50 | 51 | inline pair operator()(const int pos,const int len,const int mxpow=0)const{ 52 | 53 | ll hash1=pref1[pos+len]-pref1[pos]; 54 | ull hash2=pref2[pos+len]-pref2[pos]; 55 | 56 | 57 | if(hash1<0) hash1+=mod; 58 | 59 | if(mxpow) 60 | { 61 | hash1=(1ll*hash1*pow1[mxpow-pos-len+1])%mod; 62 | hash2=hash2*pow2[mxpow-pos-len+1]; 63 | } 64 | return make_pair(hash1,hash2); 65 | 66 | } 67 | 68 | }; 69 | 70 | int rollhash::base((int)1e9+7); 71 | vector rollhash::pow1{1}; 72 | vector rollhash::pow2{1u}; 73 | 74 | 75 | int main() 76 | { 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Strings/trie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct node 4 | { 5 | int words,prefixs; 6 | node* edges[26]; 7 | node() 8 | { 9 | words=prefixs=0; 10 | for(int i=0;i<26;i++) 11 | edges[i]=NULL; 12 | } 13 | }*root; 14 | void insert_(string s){ 15 | node* curr=root; 16 | for(int i=0;iedges[a]==NULL) 19 | curr->edges[a]=new node(); 20 | curr=curr->edges[a]; 21 | curr->prefixs++; 22 | } 23 | curr->words++; 24 | } 25 | int countwords(string s){ 26 | node* curr=root; 27 | for(int i=0;iedges[a]==NULL) 30 | return 0; 31 | curr=curr->edges[a]; 32 | } 33 | return curr->words; 34 | } 35 | int countprefix(string s){ 36 | node* curr=root; 37 | for(int i=0;iedges[a]==NULL) 40 | return 0; 41 | curr=curr->edges[a]; 42 | } 43 | return curr->prefixs; 44 | } 45 | void del(node* curr){ 46 | for(int i=0;i<26;i++) 47 | if(curr->edges[i]) 48 | del(curr->edges[i]); 49 | delete(curr); 50 | } 51 | int main() 52 | { 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Strings/z_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector z_func(const string &s){ 4 | int n=s.length(); 5 | vectorz(n,0); 6 | 7 | for(int i=1,l=0,r=0;ir) 12 | l=i,r=i+z[i]-1; 13 | } 14 | return move(z); 15 | } 16 | int main() 17 | { 18 | 19 | return 0; 20 | } 21 | --------------------------------------------------------------------------------