SUST - Iupt1110
SUST - Iupt1110
ll x=y1, y=x1-(a/b)*y1; vector<ll> combine(vector<ll>st1, vector<ll>st2){ tree[id].merge (tree[id << 1], tree[id
return {x, y}; for(ll i=1; i<st1.size(); i++){ << 1 | 1]);
}
} st2[i]+=st1[i];
// gcd(a, m) = 1 is also the condition for the modular } 9.Building graph from matrix
inverse to exist. return st2; vector<ll>graph[25], val(25, 0);// 25=maximum number
//ax+my=1 mod m so ax=1 mod m. so here x is modular of nodes
}
vector<pair<ll,ll>>moves={{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
inverse of a.
void update(ll node, ll tl, ll tr, ll ind, ll val){ bool valid(ll x, ll y, ll row, ll col){
3.Eular totient return x<row and y<row and x>=0 and
if(tl==tr){ y>=0;
int phi(int n) { if(tree[node][v[ind]])tree[node][v[ind]]--; }
void build_graph(string s[], ll row, ll col){
int result = n; v[ind]=val; for(ll i=0; i<row; i++){
for (int i = 2; i * i <= n; i++) { tree[node][v[ind]]++; for(ll j=0; j<col; j++){
ll node= i*row + j;// representing each cell
if (n % i == 0) { return; as a node of a graph.
while (n % i == 0) } if(s[i][j]=='J'){//meet our necessery condition
then set the value of node
n /= i; ll mid=(tl+tr)>>1; val[node]=1;
result -= result / i; if(ind<=mid)update(node<<1, tl, mid, ind, val); }
for(auto &[x, y]:moves){
} else update(node<<1|1, mid+1, tr, ind, val); ll new_i=i+x, new_j=j+y;
} tree[node]=combine(tree[node<<1], tree[node<<1|1]);
if(valid(new_i, new_j, row, col)){//means cell
if (n > 1) } is adjacent to node's cell
result -= result / n; vector<ll> get(ll node, ll tl, ll tr, ll l, ll new_node=new_i*row+new_j;
graph[node].push_back(new_node);
ll r){
}
{ ll stable[LOG][N];
} if(color[child]==0) ll func(ll a, ll b){
} { return min(a, b);
} if(color[parent]==1)color[child]=2; }
} else color[child]=1; ll rangeMinQuery(ll l, ll r){
qu.push(child); ll len = r - l + 1, i = log2(len);
10.Floyed_warshal } return min(stable[i][l], stable[i][r - (1 << i) + 1]);
else if(color[child]==color[parent])return false; }
} ll rangeQuery(ll l, ll r){
ll INF=1e18, d[N][N], nxt[N][N] //initially next[i][j]=j, d[i][i]=0;
} ll sum = 0;
void floyed_warshal()
return true; for (int i = LOG; i >= 0; i--){
{
} if ((1 << i) <= r - l + 1){
for (int k = 0; k < v; ++k) {
sum += stable[i][l];
for (int i = 0; i < v; ++i) {
14.Topo sort l += 1 << i;
for (int j = 0; j < v; ++j) {
}
if (d[i][k] < INF && d[k][j] < INF)
int in_degree[N]; }
if(d[i][k] + d[k][j]<d[i][j]){
vector<int> ans; return sum;
d[i][j]=d[i][k] + d[k][j];
set<int> st; }
nxt[i][j]=nxt[i][k];
int cnt = 0; void spharseTable(vector<ll> &array)
}
void bfs() {
}
{ copy(array.begin(), array.end(), stable[0]);
}
while(!st.empty()) for (ll i = 1; i < LOG; i++){
}
{ for (ll j = 0; j + (1ll << i) < N; j++){
}
cnt++; stable[i][j] = func(stable[i - 1][j], stable[i - 1][j +
vector<ll> path;
int u = *st.begin(); (1 << (i - 1))]}}
void find_path(ll i, ll j){
path.push_back(i); st.erase(st.begin());
while(i!=j){ ans.pb(u);
i=nxt[i][j]; vector<int> temp; 17.cycle detection
path.push_back(i); for(auto &it: graph[u]) int n;
} { vector<vector<int>> adj;
} in_degree[it]--; vector<char> color;
if(!in_degree[it])
11.MST and DSU {
vector<int> parent;
int cycle_start, cycle_end;
parent[N], sz[N]; st.insert(it);}
void make_set(ll n){ } bool dfs(int v) {
} color[v] = 1;
for(ll i=1; i<=n; i++)parent[i]=i, ans[i]=0, ssz[i]=1;
} for (int u : adj[v]) {
}
if (color[u] == 0) {
ll find_par(ll u){
return parent[u]==u? u: parent[u]=find_par(parent[u]);
15. Spharse Table and Lca on tree parent[u] = v;
if (dfs(u))
} const int N=1e5+10, Log=21; return true;
ll ans=0; vector<vector<ll>>up(N, vector<ll>(Log, 1)); } else if (color[u] == 1) {
void Union(ll u, ll v, ll w){ vector<vector<ll>>stablemx(N, vector<ll>(Log, -1)), cycle_end = v;
stablemi(N, vector<ll>(Log, INT64_MAX)); cycle_start = u;
u=find_par(u), v=find_par(v);
vector<ll>depth(N, 0); return true;
if(u!=v){ }
if(sz[u]<sz[v])swap(u, v); vector<pair<ll,ll>>graph[N];
// ll stablemx[N][Log], stablemi[N][Log]; }
parent[v]=u; color[v] = 2;
void dfs(ll u, ll parent){
sz[u]+=sz[v]; for(auto &[v, w]: graph[u]){ return false;
ans+=w; if(v==parent)continue; }
} depth[v]=depth[u]+1;
up[v][0]=u; void find_cycle() {
}
stablemx[v][0]=w; color.assign(n, 0);
stablemi[v][0]=w; parent.assign(n, -1);
cycle_start = -1;
Mo
const int K = 350, mxN = 1e6+5; Ordered Set
BIT int a[mxN], l=0, r=0, res=0; #include<ext/pb_ds/assoc_container.hpp>
std::unordered_map<int,int> cnt; #include<ext/pb_ds/tree_policy.hpp>
template <class T>
using namespace __gnu_pbds;
struct BIT { //1-indexed
void add(int x) {
int n; vector<T> t;
BIT() {} x = a[x]; template <typename T> using oSet = tree<T,
BIT(int _n) { if (cnt[x]==x&&x) res--; null_type, std::less<T>, rb_tree_tag,
n = _n; t.assign(n + 1, 0);
cnt[x]++; tree_order_statistics_node_update>;
}
T query(int i) { if (cnt[x]==x&&x) res++; template <typename T, typename R> using
T ans = 0; }
for (; i >= 1; i -= (i & -i)) ans += t[i]; oMap = tree<T, R, std::less<T>,
return ans; rb_tree_tag,
} void rem(int x) { tree_order_statistics_node_update>;
void upd(int i, T val) { x = a[x];
if (i <= 0) return;
for (; i <= n; i += (i & -i)) t[i] += val; if (cnt[x]==x&&x) res--; // oSet<int> os, oMap<int,int> om
} cnt[x]--;
void upd(int l, int r, T val) { if (cnt[x]==x&&x) res++; *os.find_by_order(k) // k th element
upd(l, val);
upd(r + 1, -val); } os.order_of_key(2) // number of elements
} less than k
T query(int l, int r) { void que(int L, int R) { om.insert({1, 10});
return query(r) - query(l - 1);
} while (l>L) add(--l); om.insert({2, 20});
}; while (l<L) rem(l++); mp.find_by_order(0)->second //k th element
while (r>R) rem(r--); mp.order_of_key(2) //number of first
while (r<R) add(++r); elements less than k
LCA }
are divisible by n.
34. Involutions: permutations such that p2=
25. Kummer’s Theorem: For given integers identity permutation. a0=a1=1 and
n≥m≥0 and a prime number p, the largest an=an-1+(n−1)an−2 for n>1.
power of p dividing is equal to the 35. Let T(n,k) be the number of permutations
number of carries when m is added to n-m in of size n for which all cycles have length ≤k.
base p. For implementation take inspiration
from lucas theorem.
84. =
● 132. φ(lcm(m,n))⋅φ(gcd(m,n))=φ(m)⋅φ(n)
Where, u=√x Compare this to the formula
lcm(m,n)⋅gcd(m,n)=m⋅n
● D(n)= Number of increasing
arithmetic progressions where n+1 133. φ(n) is even for n≥3. Moreover, if if n
is the second or later term. (i.e. The has r distinct odd prime factors, 2r|φ(n)
last term, starting term can be any
positive integer ≤n. For example,
D(3)=5 and there are 5 such
arithmetic progressions:
(1,2,3,4);(2,3,4);(1,4);(2,4);(3,4).
Then,
120. if n is not a perfect square, nx mod m=n ϕ(m )+ x mod ϕ(m) mod
94. A number is Fibonacci if and only if one
or both of (5⋅n2+4) or (5⋅n2−4) is a perfect and if n is a perfect square.
square
Euler’s Totient function where d(n) is number of divisors.
95. Every third number of the sequence is Same equation for gcd(a⋅k−1,n) where a and
even and more generally, every kth number of 121. The function is multiplicative. This n are coprime.
the sequence is a multiple of Fk means that if gcd(m,n)=1, 141. For every n there is at least one other
ϕ(m⋅n)=ϕ(m)⋅ϕ(n). integer m≠n such that φ(m)=φ(n).
96. gcd(Fm,Fn)=Fgcd(m,n)