By Mehrdad_Sohrabi, history, 9 days ago, We want to mention BamiTorabi and amoo_safar who we thank for helping us with translating the problems and writing the tutorials.and we want to apologize for div1 A2 and B hopefully you will forgive us :( .

official solution

official solution

official solution

Prepared by Alishahali1382

official solution

official solution

official solution
O(n2) solution

Prepared by Alishahali1382

official solution Tutorial of Codeforces Round #684 (Div. 1) Tutorial of Codeforces Round #684 (Div. 2) Comments (119)
 » Auto comment: topic has been updated by Mehrdad_Sohrabi (previous revision, new revision, compare).
•  » » Codes of Problems 1440A// In The Name Of Allah #include #define ss second #define ff first #define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define ret(n) return cout << n, 0 #define se(n) cout << setprecision(n) << fixed #define pb push_back #define ll long long #define ld long double //#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int N = 3e5 + 100, OO = 1e9 + 7, T = 50, M = 1e9 + 7, P = 6151, SQ = 280, lg = 20; typedef pair pii; void solve() { int n, c0, c1, t; string s; cin >> n >> c0 >> c1 >> t >> s; int ans = 0; for(auto u : s) { if(u == '0') ans += min(c0, c1 + t); else ans += min(c1, c0 + t); } cout << ans << endl; } int32_t main() { int t; cin >> t; while(t--) solve(); return 0; }  1440B#include typedef long long int ll; typedef long double ld; #define pb push_back #define pii pair < int , int > #define F first #define S second #define endl '\n' #define int long long #define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #define kill(x) return cout<> k >> n; for (int i=1;i<=n*k;i++){ cin >> a[i]; } ll x=(k+1)/2 - 1; x = k - x; ll z=n*k+1; ll ans=0; while(n--){ z-=x; if (z<=0) break; ans+=a[z]; } cout << ans << endl; } int32_t main(){ ll t; cin >> t; while(t--){ Main(); } }  1440C, 1439A// In The Name Of Allah #include #define ss second #define ff first #define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define ret(n) return cout << n, 0 #define se(n) cout << setprecision(n) << fixed #define pb push_back #define ll long long #define ld long double #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int N = 200, OO = 1e9 + 7, T = 50, M = 1e9 + 7, P = 6151, SQ = 280, lg = 20; typedef pair pii; char c[N][N]; bool cnt[N][N]; struct node {int x1, y1, x2, y2, x3, y3;} p; vector v; void upd(int x, int y, int tp, bool is) { if(is) v.pb({x + p[tp].x1, y + p[tp].y1, x + p[tp].x2, y + p[tp].y2, x + p[tp].x3, y + p[tp].y3}); else cnt[x + p[tp].x1][y + p[tp].y1] ^= 1, cnt[x + p[tp].x2][y + p[tp].y2] ^= 1, cnt[x + p[tp].x3][y + p[tp].y3] ^= 1; } void solve() { int n, m, od = 0; v.clear(); cin >> n >> m; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cin >> c[i][j]; if(c[i][j] == '1') od++, cnt[i][j] = true; else cnt[i][j] = false; } } if(od == 0) { cout << 0 << endl; return; } if(n == 1 || m == 1) { cout << -1 << endl; return; } for(int i = 1; i <= n - 2; i++) { for(int j = 1; j <= m; j++) { if(cnt[i][j]) { if(j != m) { v.pb({i, j, i + 1, j, i + 1, j + 1}); cnt[i][j] ^= 1, cnt[i + 1][j] ^= 1, cnt[i + 1][j + 1] ^= 1; } else { v.pb({i, j, i + 1, j, i + 1, j - 1}); cnt[i][j] ^= 1, cnt[i + 1][j] ^= 1, cnt[i + 1][j - 1] ^= 1; } } } } for(int i = 1; i <= m - 2; i++) { if(cnt[n - 1][i]) { v.pb({n - 1, i, n - 1, i + 1, n, i + 1}); cnt[n - 1][i] ^= 1, cnt[n - 1][i + 1] ^= 1, cnt[n][i + 1] ^= 1; } if(cnt[n][i]) { v.pb({n, i, n - 1, i + 1, n, i + 1}); cnt[n][i] ^= 1, cnt[n - 1][i + 1] ^= 1, cnt[n][i + 1] ^= 1; } } for(int msk = 0; msk < (1 << 4); msk++) { for(int j = 0; j < 4; j++) if(msk & (1 << j)) upd(n - 1, m - 1, j, 0); if(!cnt[n - 1][m - 1] && !cnt[n - 1][m] && !cnt[n][m - 1] && !cnt[n][m]) { for(int j = 0; j < 4; j++) if(msk & (1 << j)) upd(n - 1, m - 1, j, 1); break; } for(int j = 0; j < 4; j++) if(msk & (1 << j)) upd(n - 1, m - 1, j, 0); } cout << (int)v.size() << endl; for(auto u : v) cout << u.x1 << " " << u.y1 << " " << u.x2 << " " << u.y2 << " " << u.x3 << " " << u.y3 << endl; } int32_t main(){ use_fast; p = {0, 0, 0, 1, 1, 0}, p = {0, 1, 0, 0, 1, 1}, p = {1, 0, 1, 1, 0, 0}, p = {1, 1, 0, 1, 1, 0}; int t; cin >> t; while(t--) solve(); return 0; }  1440D, 1439B// In The Name Of Allah #include #define ss second #define ff first #define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define se(n) cout << setprecision(n) << fixed #define pb push_back //#define int long long #define ld long double #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int N = 1e5 + 100, OO = 1e9 + 7, T = 22, M = 1e9 + 7, P = 6151, SQ = 1300, lg = 22; typedef pair pii; int mark[N], deg[N], ct[N]; bool ans[N], can[N]; vector v[N], A; vector ch[N]; bool cmp(int x, int y) { return mark[x] < mark[y]; } void solve() { int n, m, k; cin >> n >> m >> k; A.clear(); for(int i = 0; i <= n; i++) v[i].clear(), ch[i].clear(), mark[i] = deg[i] = ct[i] = 0, ans[i] = can[i] = false; for(int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[x].pb(y); v[y].pb(x); } if(k > 500) { cout << -1 << endl; return; } set st; for(int i = 1; i <= n; i++) st.insert({deg[i] = (int)v[i].size(), i}); int cnt = 1; while((int)st.size()) { pii p = *st.begin(); if(p.ff >= k) { cout << 1 << " " << (int)st.size() << endl; for(auto u : st) cout << u.ss << " "; cout << endl; return; } else { st.erase(p); mark[p.ss] = cnt; for(auto u : v[p.ss]) if(!mark[u]) st.erase({deg[u], u}), st.insert({--deg[u], u}); A.pb(p.ss); } cnt++; } for(auto i : A) { int nxt = 0; for(auto u : v[i]) if(mark[u] > mark[i]) ct[nxt++] = u, can[u] = true; for(auto u : ch[i]) if(!can[u.ff]) ans[u.ss] = false; for(int j = 0; j < nxt; j++) can[ct[j]] = false; if(nxt != k - 1) continue; ans[i] = true; sort(ct, ct + nxt, cmp); for(int j = 0; j < nxt; j++) for(int k = j + 1; k < nxt; k++) ch[ct[j]].pb({ct[k], i}); } for(auto i : A) { if(!ans[i]) continue; cout << 2 << endl; cout << i << " "; for(auto u : v[i]) if(mark[u] > mark[i]) cout << u << " "; cout << endl; return; } cout << -1 << endl; return; } int32_t main() { use_fast; int t; cin >> t; while(t--) solve(); return 0; }  1440E, 1439C#include #pragma GCC optimize ("O2,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair pii; typedef pair piii; typedef pair pll; #define debug(x) cerr<<#x<<'='<<(x)<>1; Build(id<<1, tl, mid); Build(id<<1 | 1, mid, tr); Mn[id]=min(Mn[id<<1], Mn[id<<1 | 1]); Mx[id]=max(Mx[id<<1], Mx[id<<1 | 1]); seg[id]=seg[id<<1] + seg[id<<1 | 1]; } inline void add_lazy(int id, int len, ll val){ Mn[id]=val; Mx[id]=val; lazy[id]=val; seg[id]=len*val; } inline void shift(int id, int tl, int tr){ if (!lazy[id]) return ; int mid=(tl+tr)>>1; add_lazy(id<<1, mid-tl, lazy[id]); add_lazy(id<<1 | 1, tr-mid, lazy[id]); lazy[id]=0; } void Maximize(int id, int tl, int tr, int pos, ll val){ if (pos<=tl || val<=Mn[id]) return ; if (tr<=pos && Mx[id]<=val){ add_lazy(id, tr-tl, val); return ; } shift(id, tl, tr); int mid=(tl+tr)>>1; Maximize(id<<1, tl, mid, pos, val); Maximize(id<<1 | 1, mid, tr, pos, val); Mn[id]=min(Mn[id<<1], Mn[id<<1 | 1]); Mx[id]=max(Mx[id<<1], Mx[id<<1 | 1]); seg[id]=seg[id<<1] + seg[id<<1 | 1]; } int BS1(int id, int tl, int tr, int pos, ll val){ if (tr<=pos || val>1, tmp=BS1(id<<1, tl, mid, pos, val); if (tmp==mid) return BS1(id<<1 | 1, mid, tr, pos, val); return tmp; } int BS2(int id, int tl, int tr, ll val){ if (seg[id]<=val) return tr; if (tr-tl==1) return tl; shift(id, tl, tr); int mid=(tl+tr)>>1, tmp=BS2(id<<1, tl, mid, val); if (tmp>1; return Get(id<<1, tl, mid, l, r) + Get(id<<1 | 1, mid, tr, l, r); } int main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin>>n>>m; for (int i=1; i<=n; i++) cin>>A[i]; Build(1, 1, n+1); while (m--){ cin>>t>>x>>y; if (t==1) Maximize(1, 1, n+1, x+1, y); else{ ans=0; while (1){ x=BS1(1, 1, n+1, x, y); if (x==n+1) break ; ll val=y+Get(1, 1, n+1, 1, x); int xx=BS2(1, 1, n+1, val); // buy [x, xx) ans+=xx-x; y-=Get(1, 1, n+1, x, xx); x=xx; } cout< #pragma GCC optimize ("O2,unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair pii; typedef pair piii; typedef pair pll; #define debug(x) cerr<<#x<<'='<<(x)<>n>>m>>mod; for (int i=0; i #pragma GCC optimize ("O2,unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair pii; typedef pair piii; typedef pair pll; #define debug(x) cerr<<#x<<'='<<(x)< G[MAXN], grundy; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int GetH(pii p){ return p.first+p.second;} inline pii GetPar(pii p, int k){ int x=p.first, y=p.second; while (k){ int xx=(x&-x), yy=(y&-y); if (!xx) xx=2*inf; if (!yy) yy=2*inf; int tmp=min(k, min(xx, yy)); k-=tmp; if (xx &vec, int n=LOG) { if (n==0 || vec.size()==0) return ; vector v; for (pii p:vec) { int z=Zone(p, n); p.first&=(1<<(n-1))-1; p.second&=(1<<(n-1))-1; v[z].pb(p); } vec.clear(); for (int i:{0, 1, 2}) dfs_order(v[i], n-1); for (pii p:v) if (!p.first) vec.pb(p); for (pii p:v) vec.pb({p.first, p.second|(1<<(n-1))}); for (pii p:v) if (p.first) vec.pb(p); for (pii p:v) vec.pb({p.first|(1<<(n-1)), p.second}); } pii Lca(pii u, pii v, int n=LOG) { if (n==0) return {0, 0}; int zu = Zone(u, n), zv = Zone(v, n); if (zu > zv) swap(u, v), swap(zu, zv); u.first&=(1<<(n-1))-1; u.second&=(1<<(n-1))-1; v.first&=(1<<(n-1))-1; v.second&=(1<<(n-1))-1; if (zu == 1 && zv == 2) return {0, 0}; if (zu == 2 && zv == 2){ pii A = Lca(u, v, n-1); return {A.first+(1<<(n-1)), A.second}; } if (zu == 1 && zv == 1) { pii A = Lca(u, v, n-1); return {A.first, A.second+(1<<(n-1))}; } if (zv == 1) return Lca(u, {0, (1<<(n-1))-1}, n-1); if (zv == 2) return Lca(u, {(1<<(n-1))-1, 0}, n-1); return Lca(u, v, n-1); } inline int GetId(pii p){ return lower_bound(V, V+n, p)-V; } inline bool IsPar(pii u, pii v){ if (GetH(u)>GetH(v)) return 0; return GetPar(v, GetH(v)-GetH(u))==u; } int dfs(int node){ for (int v:G[node]) sum[node]+=dfs(v); return sum[node]; } int main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); vector vec; cin>>m; for (int i=0; i<2*m; i++) cin>>A[i].first>>A[i].second, vec.pb(A[i]); sort(all(vec)); vec.resize(unique(all(vec))-vec.begin()); dfs_order(vec); for (int i=vec.size()-1; i; i--) vec.pb(Lca(vec[i], vec[i-1])); sort(all(vec)); vec.resize(unique(all(vec))-vec.begin()); dfs_order(vec); n=vec.size(); debug("sorted") for (int i=0; i stk={root=GetId(vec)}; for (int i=1; i
•  » » » 8 days ago, # ^ | ← Rev. 2 →   Hi, Can you tell me whats wrong in my code 98818397 for problem E (div 2). UPD: I have misread the question, mybad.
 » can you please provide the codes?? :)
•  » » 9 days ago, # ^ | ← Rev. 2 →   Now is 2 am In Tehran, We will done it tomorrow as soon as possible.
•  » » » ok no problem.thank you :)
•  » » I've honestly never found much value in problem setter's code. I always just try to implement the ideas described in the editorial.Anyways why not look at contestant's code?
•  » » 8 days ago, # ^ | ← Rev. 19 →   Here are some implementations, I will update these more Problem A - O(n * k) solution - Bruteforces#include #include using namespace std; template void maximize(T &res, T val) { if (res < val) res = val; } template void minimize(T &res, T val) { if (res > val) res = val; } int main() { int q; cin >> q; while (q-->0) // For each query { int n, c0, c1, h; cin >> n >> c0 >> c1 >> h; string s; cin >> s; int n0 = count(s.begin(), s.end(), '0'); /// n0 = number of zeros int n1 = count(s.begin(), s.end(), '1'); /// n1 = number of ones int res = 1e9; /// +INF for (int t = 0; t <= n0; ++t) minimize(res, t * h + (n0 - t) * c0 + (n1 + t) * c1); /// Change t times (0->1) for (int t = 0; t <= n1; ++t) minimize(res, t * h + (n0 + t) * c0 + (n1 - t) * c1); /// Change t times (1->0) cout << res << '\n'; } return 0; }  Problem A - O(n) solution - Math#include #include using namespace std; int main() { int q; cin >> q; while (q-->0) // For each query { int n, c0, c1, h; cin >> n >> c0 >> c1 >> h; string s; cin >> s; int n0 = count(s.begin(), s.end(), '0'); /// n0 = number of zeros int n1 = count(s.begin(), s.end(), '1'); /// n1 = number of ones int none_change = n0 * (c0 + 0) + n1 * (c1 + 0); /// Current cost int change_to_0 = n0 * (c0 + 0) + n1 * (c0 + h); /// Cost when change all to zeros int change_to_1 = n0 * (c1 + h) + n1 * (c1 + 0); /// Cost when change all to ones cout << min({none_change, change_to_0, change_to_1}) << '\n'; } return 0; }  Problem B - O(n*k) Offline solution#include #include #include using namespace std; int main() { int q; cin >> q; while (q-->0) { /// Input int n, k; cin >> n >> k; vector a(n * k); for (int &x : a) cin >> x; /// Calculation int shift = n / 2 + 1; /// Median long long res = 0; for (int i = 1; i <= k; ++i) res += a[a.size() - i * shift]; /// Find median of ith group cout << res << '\n'; } return 0; }  Problem B - O(n*k) Online Solution#include #include #include #include using namespace std; int main() { int q; cin >> q; while (q-->0) /// For each query { int n, k; cin >> n >> k; int length = n * k; int median = n / 2 + 1; /// We dont need array, but it only work if the array have been already sorted long long res = 0; int pos = k * (n - median); /// Next median for (int index = 0; index < length; ++index) { int value; cin >> value; if (index == pos) { res += value; /// Sumation of medians pos += median; /// Find next median } } cout << res << '\n'; } return 0; }  Problem C1 - O(nm) solution - operations <= 3nm - Simple Greedy#include #include using namespace std; #define all(x) (x).begin(), (x).end() int main() { int q; cin >> q; while (q-->0) /// For each query { /// Input int n, m; cin >> n >> m; string s[n]; for (int i = 0; i < n; ++i) cin >> s[i]; /// Calculation int cnt = 0; for (int i = 0; i < n; ++i) /// For each '1' appear cnt += 3 * count(all(s[i]), '1'); /// We use 3 operations to turn off it /// These operations are independent /// Output the result cout << cnt << '\n'; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i][j] == '1') { int cx = i + 1; /// +1 because we use 0-based loop int cy = j + 1; /// +1 because we use 0-based loop int px = (cx < n) ? cx + 1 : cx - 1; /// Next row int py = (cy < m) ? cy + 1 : cy - 1; /// Next col /// Notice that these operation are independent /// They wont affect others cells, just turn off [i][j] cout << px << ' ' << cy << ' '; /// X O | + + | O X cout << cx << ' ' << py << ' '; /// O O | + _ | X O cout << cx << ' ' << cy << '\n'; /// Operation: 1 -> 2 cout << px << ' ' << cy << ' '; /// O X | + _ | X X cout << px << ' ' << py << ' '; /// X O | + + | O X cout << cx << ' ' << cy << '\n'; /// Operation: 2 -> 3 cout << cx << ' ' << py << ' '; /// X X | + + | O O cout << px << ' ' << py << ' '; /// O X | _ + | O O cout << cx << ' ' << cy << '\n'; /// Operation: 3 -> 0 } } } } return 0; }  Problem C2 - O(nm) solution - operations <= nm - Greedy#include #include #include using namespace std; #define all(x) (x).begin(), (x).end() /// Each line of the answer struct node { int a, b, c, d, e, f; node (int a, int b, int c, int d, int e, int f) : a(a), b(b), c(c), d(d), e(e), f(f) {} }; vector res; vector > a; void flip(const node &x) { a[x.a][x.b] ^= 1; a[x.c][x.d] ^= 1; a[x.e][x.f] ^= 1; res.push_back(x); } int main() { int q; cin >> q; while (q-->0) /// For each query { /// Input int n, m; cin >> n >> m; a.assign(n + 1, vector(m + 1, 0)); for (int i = 1; i <= n; ++i) { string s; cin >> s; for (int j = 1; j <= m; ++j) a[i][j] = s[j - 1] - '0'; /// Convert string to array } res.clear(); /// Odd row elimination if (n & 1) { for (int j = 1; j <= m; ++j) /// Eliminate 2x1 cells by once { int p = (j == m) ? (j - 1) : (j + 1); if (a[n][j]) flip(node(n-0,j , n-1,j , n-1,p)); } --n; /// <- Last row eliminated } /// Odd column elimination if (m & 1) { for (int i = 1; i <= n; ++i) /// Eliminate 1x2 cells by once { int p = (i == n) ? (i - 1) : (i + 1); if (a[i][m]) flip(node(i,m-0 , i,m-1 , p,m-1)); } --m; /// <- last col eliminated } /// Eliminate 2x2 cells by once for (int i = 1; i <= n; i += 2) { for (int j = 1; j <= m; j += 2) { int x = 0, y = 0, z = 0, t = 0; if (a[i + 0][j + 0]) x ^= 0, y ^= 1, z ^= 1, t ^= 1; if (a[i + 1][j + 0]) x ^= 1, y ^= 0, z ^= 1, t ^= 1; if (a[i + 0][j + 1]) x ^= 1, y ^= 1, z ^= 0, t ^= 1; if (a[i + 1][j + 1]) x ^= 1, y ^= 1, z ^= 1, t ^= 0; if (x) flip(node(i+1,j+0 , i+0,j+1 , i+1,j+1)); if (y) flip(node(i+0,j+0 , i+0,j+1 , i+1,j+1)); if (z) flip(node(i+0,j+0 , i+1,j+0 , i+1,j+1)); if (t) flip(node(i+0,j+0 , i+1,j+0 , i+0,j+1)); } } /// Output the result cout << res.size() << '\n'; for (const node &x : res) { cout << x.a << ' ' << x.b << ' '; cout << x.c << ' ' << x.d << ' '; cout << x.e << ' ' << x.f << '\n'; } } return 0; }  Problem C2 - O(nm) solution - operations <= nm - Greedy#include #include #include using namespace std; #define all(x) (x).begin(), (x).end() /// Each line of the answer struct node { int a, b, c, d, e, f; node (int a, int b, int c, int d, int e, int f) : a(a), b(b), c(c), d(d), e(e), f(f) {} }; vector res; vector > a; void flip(const node &x) { a[x.a][x.b] ^= 1; a[x.c][x.d] ^= 1; a[x.e][x.f] ^= 1; res.push_back(x); } int main() { int q; cin >> q; while (q-->0) /// For each query { /// Input int n, m; cin >> n >> m; /// Initalization res.clear(); a.assign(n + 1, vector(m + 1, 0)); for (int i = 1; i <= n; ++i) { string s; cin >> s; for (int j = 1; j <= m; ++j) a[i][j] = s[j - 1] - '0'; /// Convert string to array } /// Odd column elimination if (m & 1) { for (int i0 = 1; i0 <= n; ++i0) /// Eliminate 1x2 cells by once { if (a[i0][m] == 0) continue; int i1 = (i0 == n) ? (i0 - 1) : (i0 + 1); if (a[i1][m]) flip(node(i0,m-0 , i1,m-0 , i0,m-1)); else flip(node(i0,m-0 , i0,m-1 , i1,m-1)); } --m; /// <- Last col eliminated } /// Row Elimination: n * m -> 2 * m remaining while (n > 2) { for (int j0 = 1; j0 <= m; ++j0) /// Eliminate 2x1 cells by once { if (a[n][j0] == 0) continue; int j1 = (j0 == m) ? (j0 - 1) : (j0 + 1); if (a[n][j1]) flip(node(n-0,j0 , n-0,j1 , n-1,j0)); else flip(node(n-0,j0 , n-1,j0 , n-1,j1)); } --n; /// <- Last row eliminated } /// Col Elimination: 2 * m -> 2 * 2 remaining while (m > 2) { for (int i0 = 1; i0 <= n; ++i0) /// Eliminate 1x2 cells by once { if (a[i0][m] == 0) continue; int i1 = (i0 == n) ? (i0 - 1) : (i0 + 1); if (a[i1][m]) flip(node(i0,m-0 , i1,m-0 , i0,m-1)); else flip(node(i0,m-0 , i0,m-1 , i1,m-1)); } --m; /// <- Last col eliminated } /// Cells Elimination: 2 * 2 -> 0 * 0 remaining int x = 0, y = 0, z = 0, t = 0; if (a) x ^= 0, y ^= 1, z ^= 1, t ^= 1; if (a) x ^= 1, y ^= 0, z ^= 1, t ^= 1; if (a) x ^= 1, y ^= 1, z ^= 0, t ^= 1; if (a) x ^= 1, y ^= 1, z ^= 1, t ^= 0; if (x) flip(node(2,1 , 1,2 , 2,2)); if (y) flip(node(1,1 , 1,2 , 2,2)); if (z) flip(node(1,1 , 2,1 , 2,2)); if (t) flip(node(1,1 , 2,1 , 1,2)); /// Output the result cout << res.size() << '\n'; for (const node &x : res) { cout << x.a << ' ' << x.b << ' '; cout << x.c << ' ' << x.d << ' '; cout << x.e << ' ' << x.f << '\n'; } } return 0; }  Problem C2 analizer for above AC code#include #include #include using namespace std; #define all(x) (x).begin(), (x).end() /// Each line of the answer struct node { int a, b, c, d, e, f; node (int a, int b, int c, int d, int e, int f) : a(a), b(b), c(c), d(d), e(e), f(f) {} }; int counter = 0; vector > a; void flip(const node &x) { a[x.a][x.b] ^= 1; a[x.c][x.d] ^= 1; a[x.e][x.f] ^= 1; ++counter; } int main() { int QN, QM; cin >> QN >> QM; int total = 0, res = 0, bestgen = 0; int lim = 1 << min(20, QN * QM); for (int mask = 0; mask < lim; ++mask) { /// Initalization int n = QN; int m = QM; counter = 0; a.assign(n + 1, vector(m + 1, 0)); int gen = mask; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { a[i][j] = gen & 1; gen >>= 1; } } /// Odd column elimination if (m & 1) { for (int i0 = 1; i0 <= n; ++i0) /// Eliminate 1x2 cells by once { if (a[i0][m] == 0) continue; int i1 = (i0 == n) ? (i0 - 1) : (i0 + 1); if (a[i1][m]) flip(node(i0,m-0 , i1,m-0 , i0,m-1)); else flip(node(i0,m-0 , i0,m-1 , i1,m-1)); } --m; /// <- Last col eliminated } /// Row Elimination: n * m -> 2 * m remaining while (n > 2) { for (int j0 = 1; j0 <= m; ++j0) /// Eliminate 2x1 cells by once { if (a[n][j0] == 0) continue; int j1 = (j0 == m) ? (j0 - 1) : (j0 + 1); if (a[n][j1]) flip(node(n-0,j0 , n-0,j1 , n-1,j0)); else flip(node(n-0,j0 , n-1,j0 , n-1,j1)); } --n; /// <- Last row eliminated } /// Col Elimination: 2 * m -> 2 * 2 remaining while (m > 2) { for (int i0 = 1; i0 <= n; ++i0) /// Eliminate 1x2 cells by once { if (a[i0][m] == 0) continue; int i1 = (i0 == n) ? (i0 - 1) : (i0 + 1); if (a[i1][m]) flip(node(i0,m-0 , i1,m-0 , i0,m-1)); else flip(node(i0,m-0 , i0,m-1 , i1,m-1)); } --m; /// <- Last col eliminated } /// Cells Elimination: 2 * 2 -> 0 * 0 remaining int x = 0, y = 0, z = 0, t = 0; if (a) x ^= 0, y ^= 1, z ^= 1, t ^= 1; if (a) x ^= 1, y ^= 0, z ^= 1, t ^= 1; if (a) x ^= 1, y ^= 1, z ^= 0, t ^= 1; if (a) x ^= 1, y ^= 1, z ^= 1, t ^= 0; if (x) flip(node(2,1 , 1,2 , 2,2)); if (y) flip(node(1,1 , 1,2 , 2,2)); if (z) flip(node(1,1 , 2,1 , 2,2)); if (t) flip(node(1,1 , 2,1 , 1,2)); /// Checker for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (a[i][j] == 1) { cout << "Program failed at this gen:\n"; for (int i = 1; i <= QN; ++i) { for (int j = 1; j <= QM; ++j) { cout << (mask & 1); mask >>= 1; } cout << '\n'; } return 0; } } } /// Analizer total += counter; if (res < counter) { res = counter; bestgen = mask; } } /// Analizer cout << "You used " << total << " times of flipping !\n"; cout << "It need " << res << " times for this gen:\n"; for (int i = 1; i <= QN; ++i) { for (int j = 1; j <= QM; ++j) { cout << (bestgen & 1); bestgen >>= 1; } cout << '\n'; } cout << "Compared to expected number = " << res << " / " << QN * QM; return 0; }  Problem C2 analizer report for all cases with (n = 5, m = 4)You used 8192000 times of flipping ! It need 12 times for this gen: 1001 0000 0000 0000 1010 Compared to expected number = 12 / 20 
•  » » » I may get downvotes, but O(3nm) and O(3nm/4) are the same. If you want to compare constants don't use big O notation.
•  » » » » 8 days ago, # ^ | ← Rev. 2 →   Ok then, but actually the result is also ≈ $O(f(x))$, what should I write then
•  » » » » » try this Problem C2 <= (3nm/4) operations solution
•  » » » » » » I updated and also add an extra solution
•  » » » » » » this is an overkill but we can in fact solve each row in $\frac{m+1}{2}$ steps, idea is to consider in each row as many columns of size 2 and solve each block of size 2 as follows11 : chose block with left most point at i,j and dont flip i+1,j01 : chose block with left most point at i,j and dont flip i,j10 : chose block with left most point at i,j and dont flip i,j+100 : chill code for (size_t i = 0; i + 2 < n; i += 1) { for (size_t j = 0; j + 1 < m; j += 2) { if (vecc[i][j] == vecc[i][j + 1]) { if (vecc[i][j] == '1') perform({i, j}, {i + 1, j}); } else if (vecc[i][j] == '1') perform({i, j}, {i, j + 1}); else if (vecc[i][j + 1] == '1') perform({i, j}, {i, j}); } if (m & 1) { if (vecc[i][m - 1] == '1') perform({i, m - 2}, {i, m - 2}); } db(vecc[i], res.size()); } Overkilled solution 98789649So over all number of operations will be atmost $\frac{m+1}{2}*(n-2) + (m-2)+4$
•  » » » » » » » 8 days ago, # ^ | ← Rev. 3 →   I used your code and make this checker, it seems my code is overkill too Checker of your solution#include #include #include using namespace std; #define all(x) (x).begin(), (x).end() /// Each line of the answer struct node { int a, b, c, d, e, f; node (int a, int b, int c, int d, int e, int f) : a(a), b(b), c(c), d(d), e(e), f(f) {} }; int counter = 0; vector > a; void flip(int x, int y, int u, int v) { a[x][y] ^= 1; a[u][v] ^= 1; counter += 2; } int main() { int QN, QM; cin >> QN >> QM; int total = 0, res = 0, bestgen = 0; int lim = 1 << min(20, QN * QM); for (int mask = 0; mask < lim; ++mask) { /// Initalization int n = QN; int m = QM; counter = 0; a.assign(n + 1, vector(m + 1, 0)); int gen = mask; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { a[i][j] = gen & 1; gen >>= 1; } } int counter = 0; auto perform = [&](pair start, pair leave) { for (auto x : {start.first, start.first + 1}) for (auto y : {start.second, start.second + 1}) { if (x == leave.first && y == leave.second) continue; a[x][y] = 1 + 0 - a[x][y]; } ++counter; }; // if (n == 2 && m == 2) auto pick_and_perform = [&](int x, int y, int c) { for (auto dx : {0, 1}) for (auto dy : {0, 1}) if (a[x + dx][y + dy] == c) { perform({x, y}, {x + dx, y + dy}); return; } }; auto solveblock = [&](int x, int y) { vector> ones; for (auto dx : {0, 1}) for (auto dy : {0, 1}) if (a[x + dx][y + dy] == 1) ones.push_back({x + dx, y + dy}); int i = x, j = y; if (ones.size() == 1) { pick_and_perform(i, j, 0); pick_and_perform(i, j, 1); pick_and_perform(i, j, 0); } else if (ones.size() == 2) { pick_and_perform(i, j, 1); pick_and_perform(i, j, 0); } else if (ones.size() == 3) pick_and_perform(i, j, 0); else if (ones.size() == 4) { pick_and_perform(i, j, 1); pick_and_perform(i, j, 0); pick_and_perform(i, j, 1); pick_and_perform(i, j, 0); } }; for (size_t i = 0; i + 2 < n; i += 1) { for (size_t j = 0; j + 1 < m; j += 2) { if (a[i][j] == a[i][j + 1]) { if (a[i][j] == 1) perform({i, j}, {i + 1, j}); } else if (a[i][j] == 1) perform({i, j}, {i, j + 1}); else if (a[i][j + 1] == 1) perform({i, j}, {i, j}); } if (m & 1) { if (a[i][m - 1] == 1) perform({i, m - 2}, {i, m - 2}); } } int i = n - 2; for (size_t j = 0; j + 2 < m; j += 1) { if (a[i][j] == a[i + 1][j]) { if (a[i][j] == 1) perform({i, j}, {i, j + 1}); } else if (a[i][j] == 1) perform({i, j}, {i + 1, j}); else if (a[i + 1][j] == 1) perform({i, j}, {i, j}); } solveblock(n - 2, m - 2); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] == 1) { cout << "Program failed at this gen:\n"; for (int i = 0; i < QN; ++i) { for (int j = 0; j < QM; ++j) { cout << (mask & 1); mask >>= 1; } cout << '\n'; } return 0; } } } total += counter; if (res < counter) { res = counter; bestgen = mask; } } cout << "You used " << total << " times of flipping !\n"; cout << "It need " << res << " times for this gen:\n"; for (int i = 0; i < QN; ++i) { for (int j = 0; j < QM; ++j) { cout << (bestgen & 1); bestgen >>= 1; } cout << '\n'; } cout << "Compared to expected number = " << res << " / " << QN * QM; return 0; }  Checker of my solution#include #include #include using namespace std; #define all(x) (x).begin(), (x).end() /// Each line of the answer struct node { int a, b, c, d, e, f; node (int a, int b, int c, int d, int e, int f) : a(a), b(b), c(c), d(d), e(e), f(f) {} }; int counter = 0; vector > a; void flip(const node &x) { a[x.a][x.b] ^= 1; a[x.c][x.d] ^= 1; a[x.e][x.f] ^= 1; ++counter; } int main() { int QN, QM; cin >> QN >> QM; int total = 0, res = 0, bestgen = 0; int lim = 1 << min(20, QN * QM); for (int mask = 0; mask < lim; ++mask) { /// Initalization int n = QN; int m = QM; counter = 0; a.assign(n + 1, vector(m + 1, 0)); int gen = mask; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { a[i][j] = gen & 1; gen >>= 1; } } /// Odd column elimination if (m & 1) { for (int i0 = 1; i0 <= n; ++i0) /// Eliminate 1x2 cells by once { if (a[i0][m] == 0) continue; int i1 = (i0 == n) ? (i0 - 1) : (i0 + 1); if (a[i1][m]) flip(node(i0,m-0 , i1,m-0 , i0,m-1)); else flip(node(i0,m-0 , i0,m-1 , i1,m-1)); } --m; /// <- Last col eliminated } /// Row Elimination: n * m -> 2 * m remaining while (n > 2) { for (int j0 = 1; j0 <= m; ++j0) /// Eliminate 2x1 cells by once { if (a[n][j0] == 0) continue; int j1 = (j0 == m) ? (j0 - 1) : (j0 + 1); if (a[n][j1]) flip(node(n-0,j0 , n-0,j1 , n-1,j0)); else flip(node(n-0,j0 , n-1,j0 , n-1,j1)); } --n; /// <- Last row eliminated } /// Col Elimination: 2 * m -> 2 * 2 remaining while (m > 2) { for (int i0 = 1; i0 <= n; ++i0) /// Eliminate 1x2 cells by once { if (a[i0][m] == 0) continue; int i1 = (i0 == n) ? (i0 - 1) : (i0 + 1); if (a[i1][m]) flip(node(i0,m-0 , i1,m-0 , i0,m-1)); else flip(node(i0,m-0 , i0,m-1 , i1,m-1)); } --m; /// <- Last col eliminated } /// Cells Elimination: 2 * 2 -> 0 * 0 remaining int x = 0, y = 0, z = 0, t = 0; if (a) x ^= 0, y ^= 1, z ^= 1, t ^= 1; if (a) x ^= 1, y ^= 0, z ^= 1, t ^= 1; if (a) x ^= 1, y ^= 1, z ^= 0, t ^= 1; if (a) x ^= 1, y ^= 1, z ^= 1, t ^= 0; if (x) flip(node(2,1 , 1,2 , 2,2)); if (y) flip(node(1,1 , 1,2 , 2,2)); if (z) flip(node(1,1 , 2,1 , 2,2)); if (t) flip(node(1,1 , 2,1 , 1,2)); /// Checker for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (a[i][j] == 1) { cout << "Program failed at this gen:\n"; for (int i = 1; i <= QN; ++i) { for (int j = 1; j <= QM; ++j) { cout << (mask & 1); mask >>= 1; } cout << '\n'; } return 0; } } } /// Analizer total += counter; if (res < counter) { res = counter; bestgen = mask; } } /// Analizer cout << "You used " << total << " times of flipping !\n"; cout << "It need " << res << " times for this gen:\n"; for (int i = 1; i <= QN; ++i) { for (int j = 1; j <= QM; ++j) { cout << (bestgen & 1); bestgen >>= 1; } cout << '\n'; } cout << "Compared to expected number = " << res << " / " << QN * QM; return 0; }  Checker of your code report for (5, 4)You used 8388608 times of flipping ! It need 12 times for this gen: 1011 0000 0000 0000 0001 Compared to expected number = 12 / 20  My checker report for (5, 4)You used 8192000 times of flipping ! It need 12 times for this gen: 1001 0000 0000 0000 1010 Compared to expected number = 12 / 20 
•  » » » » » » » » Now that's what I call stress testing XD.
•  » » » SPyofgame can you please explain use of x,y,z, and t in the 2*2 block code ... are you checking for the number of moves through them?
•  » » » » 8 days ago, # ^ | ← Rev. 4 →   In C1, we know that we can turn off only one cell by $3$ operationsThen in $2x2$ cells, we have to use $3x4$ operationsHowever, by using xor operator, you will reduce the number of calls upto 4 times ! Since the same cells are called will be ignoredFor simpler thinking (ignore the complex math part), here is an example, you need to flip {$o2, o3, o4$} to turn off {$p1$} and flip {$o1, o2, o4$} to turn off {$p2$}, then you actually need to flip {$o1, o3$} in order to turn off both {$p1, p2$}
•  » » » » » cool... thank you ... now I understood ... nice explaination
•  » » check out anyone's code in submission.
 » Does this work for C2 Hard version , let's go to each element of our matrix except the last row and last column and try to fix them by flipping , Requies only one move so we have exhausted nm-n-m moves till now lets try to fix our bottom most row and rightmost column by the brute force way we used for C1 easy Version,It works? If anyone has a counter example?
•  » » 8 days ago, # ^ | ← Rev. 2 →   111 110 101 What about this?
 » Lodu contest
 » I'd like to see an official Java solution for Div1B.There was one, right?
 » 9 days ago, # | ← Rev. 3 →   [deleted]
•  » » Don't worry about downvotes, random shit from noobs and pupils like me........i didn't check your video, but you can share these things until and unless they are not related to programming.
 » 9 days ago, # | ← Rev. 2 →   ㅤ
 » Please somebody help.I dont know why this solution 98723050 for 1440A - Buy the String is showing Time limit exceeded on pretest 3. Thank you!!!
•  » » I'm not sure whether this's the problem but the size of the string might be 1000 which is greater than ur dp's size
•  » » The size of your DP if just 100, but the size of string can be up to 1000, so it might be undefined behavior.
 » Codeforces Round #50000000 Div2 C — So just enumerate all $2^{256}$ cases and handle them individually...
•  » » But wait you have a overflow here.
•  » » » Can't even make a joke without getting Memory Limit Exceeded error! XD
 » can pls someone explain sum of medians
•  » » For each of the $k$ partitions, greedily fill up the part from the end to the median first, using the largest available numbers. Then calculate the sum of the medians.See this submission here — 98706701
•  » » » 8 days ago, # ^ | ← Rev. 2 →   my solution can you please check my solution what i did is that i have created a k x n matrixand for each row I have stored the first element as the first element of the matrix. I mean first element of each k row is first k element of the arrayand the rest n-1 element I have taken from the back of the arrayand to get the ans I have added all the elements of n/2 column of the matrixis the concept even correct? for n=2 I have handeled it separately
 » I couldn't understand how to solve for the last nth row in binary table question. Can someone plssss help me out....
•  » » 9 days ago, # ^ | ← Rev. 2 →   ㅤ
•  » » » how would you make the bottom right cell 0 in case of odd n and odd m?
•  » » » » 8 days ago, # ^ | ← Rev. 3 →   ㅤ
•  » » you need 3 operations to remove a one
 » Problem with the Div.2 taskE, my approach with the editorial: When the man wants to eat:First the man only eats for log(maxY) consecutive things so the time complexity for this is O(log(maxY))Second we binary search for the place he starts to eat which takes at most O(log(n))Third we find how much he will eat and this also takes O(log(n)) And to implement the above things I used a segment tree to maintain range_max, range_min, range_sum, so each range-query takes O(log(n))So for each query of the second type will take O( log(maxY) * ( log(n) + log(n) ) * log(n) ) So it leads to TLECan someone please tell me how to boost my algorithm, thanks in advance.
•  » » You can binary search on a segment tree for O(log n). Start at the root, if the left subtree is not good, go to the right subtree, else go to the left subtree.More specifically, in this problem, you have 2 different binary searches, one for starting place and one for ending place.The first binary search is for min i so that a[i] <= x. This is simple.The second binary search is for max i so that sum(a[l], a[i]) <= x. This is the same as finding max i so that sum(a, a[i]) <= x + sum(a, a[l-1]). This binary search is also simple.
•  » » 9 days ago, # ^ | ← Rev. 2 →   You can get rid of binary search in both operations. For example you want to find leftmost position p, such that a[p]<=y. You can store minimum on segments in your segment tree. Imagine that you are standing in the root. You know that minimum in left subtree is larger or equal than maximum in right subtree. So if minimum from left subtree is smaller or equal to y you can go left, otherwise go right. You can do similar thing to remove second binary search.
•  » » » But I also want my p to be greater than the current begin point, so going left might not be able to take a usable minimum.So I'll have to iterate almost every vertex of my seg tree which again makes me TLE.Thanks for ur time and patience BTW.
•  » » » » 8 days ago, # ^ | ← Rev. 2 →   What’s the problem of taking p=max(p,x)? Because our array is non-increasing. If p is good, then every index after p is good too.
•  » » » » » Oh wow I misunderstood the statement. I thought the modification was for only one position. Again, thanks for ur help and patiences.
 » Can anybody please explain B? Why we are taking the biggest n/2 numbers from the end and smallest n/2 numbers from the beginning? I was not able to understand the proof.
 » Div2 C1/C2 was the most complex implementation problem. Upvote if you agree!
•  » » 9 days ago, # ^ | ← Rev. 2 →   Btw nice way of losing contribution.
 » In the contest dashboard, there isn't the option of "Tutorials(en)" in the contest material section. please look into it.Thank you :)
 » 8 days ago, # | ← Rev. 2 →   Where is the editorial for div2 C1 (easy version of binary table)?
•  » » You can refer to this
•  » » The main idea is for every cell in a 2 × 2 you can change it without change other cells.
 » 8 days ago, # | ← Rev. 2 →   Nice problemset.
•  » » LODU problemset.
 » In problem A we can also count zeros and ones in the string and then take the minimum from zeros*h + c1*n, ones*h + c0*n, ones*c1 + zeros*c0. So the total complexity will be O(n/w) (because we use bitset.count() method)
•  » » Oh, i think i have made a little mistake here. I haven't calculate the compexitiy of string to bitset operation. I guess, compexity of it is O(n) so the to total compexity is O(n + n/w) = O(n)
 » Mohammad.H915 In div1B How do we check if it is a clique or not in O(k^2). I know one way which takes O(k^2*log(n)) but I am not able to understand editorial's approach. can you please elaborate a bit more. Thanks
•  » » You can store neighbours of each vertice in an unordered_set.
•  » » » got AC with unordered_set with 810ms and 530ms when eliminated duplicated checks
•  » » » » I've tried unordered_set but I'm still getting TLE :-(
•  » » » » » Make sure to break from both cycles as early as you found it is not clique. https://codeforces.com/contest/1440/submission/99096912 811ms https://codeforces.com/contest/1440/submission/99097347 529ms
•  » » » » » » Already doing that: https://codeforces.com/contest/1439/submission/99119972And if anything it should be faster than what you're doing because my iteration is over a vector rather than an unordered_set (I do some complicated things to maintain vectors of neighbours as edges are deleted).
•  » » 8 days ago, # ^ | ← Rev. 4 →   Check it ofline for each vertex that have more than or equal to k-1 we see thier Neighbors, now if you want to know v and u is adjacen just set a new vector for v or u then when you want see neighbors of v just check it adjance with u or not, My code // In The Name Of Allah // Mohammad Hosseini #include #define ss second #define ff first #define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define se(n) cout << setprecision(n) << fixed #define pb push_back //#define int long long #define ld long double #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int N = 1e5 + 100, OO = 1e9 + 7, T = 22, M = 1e9 + 7, P = 6151, SQ = 1300, lg = 22; typedef pair pii; int mark[N], deg[N], ct[N]; bool ans[N], can[N]; vector v[N], A; vector ch[N]; bool cmp(int x, int y) { return mark[x] < mark[y]; } void solve() { int n, m, k; cin >> n >> m >> k; A.clear(); for(int i = 0; i <= n; i++) v[i].clear(), ch[i].clear(), mark[i] = deg[i] = ct[i] = 0, ans[i] = can[i] = false; for(int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[x].pb(y); v[y].pb(x); } if(k > 500) { cout << -1 << endl; return; } set st; for(int i = 1; i <= n; i++) st.insert({deg[i] = (int)v[i].size(), i}); int cnt = 1; while((int)st.size()) { pii p = *st.begin(); if(p.ff >= k) { cout << 1 << " " << (int)st.size() << endl; for(auto u : st) cout << u.ss << " "; cout << endl; return; } else { st.erase(p); mark[p.ss] = cnt; for(auto u : v[p.ss]) if(!mark[u]) st.erase({deg[u], u}), st.insert({--deg[u], u}); A.pb(p.ss); } cnt++; } // start of finding clique for(auto i : A) { int nxt = 0; for(auto u : v[i]) if(mark[u] > mark[i]) ct[nxt++] = u, can[u] = true; for(auto u : ch[i]) if(!can[u.ff]) ans[u.ss] = false; for(int j = 0; j < nxt; j++) can[ct[j]] = false; if(nxt != k-1) continue; ans[i] = true; sort(ct, ct + nxt, cmp); for(int j = 0; j < nxt; j++) for(int k = j + 1; k < nxt; k++) ch[ct[j]].pb({ct[k], i}); } // end for(auto i : A) { if(!ans[i]) continue; cout << 2 << endl; cout << i << " "; for(auto u : v[i]) if(mark[u] > mark[i]) cout << u << " "; cout << endl; return; } cout << -1 << endl; return; } int32_t main() { use_fast; int t; cin >> t; while(t--) solve(); return 0; } 
•  » » » what does "— 1" part do (tried googling it but all i get is HTML blah ..)?
•  » » » » K-1 mean
•  » » » 8 days ago, # ^ | ← Rev. 2 →   I've never seen an if statement like this if(nxt != k — 1), can you tell me what it does exactly? Thanks in advance.
•  » » » » Forgive me when such a bug probably occurred while copying
 » B formulars: Consider using \cdot multiplication sign instead of a simple dot.$n\cdot{k}$ vs $n.k$
 » Could you please take a look at hack 678765? I'm getting "Unexpected Verdict".
•  » » Usually it means that the model solution is giving TLE or something.
 » 8 days ago, # | ← Rev. 2 →   for Div2D/Div1B in the editorial they have put random full stop in sentences and I an not able to understand for checking clique part. How they are assigning 0 and 1 to vertices?Can anyone please tell how to check for the clique after checking the other one(non empty subset) does not exist?
•  » » You can just store neighbours of each vertice in an unordered_set. If you have a vertice with $k-1$ edges, you iterate over all of it's neighbours to check if they form a clique.
•  » » » but what guarantees that once we have checked a vertex with $k-1$ edge and it's neighbours then we don't have to repeat the same for it's neighbours too? because if we have to repeat then time complexity will be $O(nk^2)$ which is not feasible. so can you tell the reason behind the previous claim.
 » For Div1 B, I don't understand this part: for checking clique candidates fast, iterate over vertices and name current vertex $v$. then for neighbors of $v$ set $nei_v$ to 1 and 0 otherwise. for each clique candidate that contains $v$ like $C$, we check edge between $v$ and $u \in C$ in $O(1)$ using array $nei$. Especially, what does "name current vertex" mean, and what does $nei$ do?I tried implementing similar solution but I got TLE.
•  » » 8 days ago, # ^ | ← Rev. 2 →   We want to check cliques offline so we fix a vertex and check all the edges for that vertex
•  » » » Does "check cliques offline" means that we check for cliques after removing vertices as much as possible, while recording the vertices with $k-1$ outgoing edges, and check for cliques after reaching to the end (when we could not find any $k$-neighbor graph)?
•  » » » » Yep
•  » » » » » 8 days ago, # ^ | ← Rev. 2 →   Oh yes, then that way we can make sure that we inspect only once each for all edges right? Now I understood, thanks a lot.
•  » » » So do you gather all candidates for cliques? Then it's $O(n^{1.5})$ space complexity, seems very dangerous. Can I see your code?
•  » » » » 8 days ago, # ^ | ← Rev. 2 →   WrongA vertex is included in the candidate list at most twice, so it's $O(n)$ space complexity, I think.
•  » » » » Oops, I mean, the number of vertices added to the list is at most twice as large as the number of edges, so the spacial complexity is $O(m)$, perhaps?
•  » » » » » Oh, you are right. Thanks.
 » In Binary Table (hard version) editorial, I am not able to understand how you are performing operations. Like you have written we can use one operation on it, its left neighbour and the two cells above to fix this cell , can someone please explain this. I am also not able to comprehend solution posted by SPyofgame
•  » » you might find this useful(for c1 and c2): https://www.youtube.com/watch?v=qHVt2ulsVxYresharing prev comment
•  » » » Thanks much for this. Have you implemented this by any chance? I find it tricky to implement the n x 2 section.
•  » » At which part you feel hard to understand ? I will add some other approachs later and extra comments & proves
•  » » » I appreciate your solution, but I am finding difficulty in understanding the logic like why you are eliminating the last row, last column. And also how using bitwise XOR solves 2*2 boxes, being a tyro I am not able to grasp the logic, if you can add some explanations it would be very useful for everyone
•  » » » » :( If not using XOR to implement, you will have to check 4 if-else
 » I forgot to sort the adjacency list to perform binary search after i switched from set to vector (because the amazing tle) and it somewhat passed even the system test, amazing tests too ;)https://codeforces.com/contest/1439/hacks/678778To be honest seems more hard to create tests where my solution is correct than where it's wrong, but you guys did it, unbelievable.
•  » » well ive change my vector to unordered_set and i got Ac, thats weird!
 » In div1 C if we binary search on the segment tree and there are logY segments then the complexity becomes O(n*logY*logn*logn) , I came up with this solution but it should give TLE. Is there a faster way to binary search on the segment tree?
 » LGM Errichto orz
 » Maybe the Div1 D tutorial has any problem? dp1_i=(n+1)\sum_{j=0}^i dp1_{j-1}dp1_{i-j}\binom{n-1,i-1} thx.
 » 8 days ago, # | ← Rev. 2 →   An intuitive way and short clean code to solve Div2 C2 Hard version solution hope you understand 98807026 Thank you.
 » 8 days ago, # | ← Rev. 2 →   Anyone help me find whats wrong in my submission 98818397 for problem E (div 2).Thanks in advance!!
 » 8 days ago, # | ← Rev. 3 →   There are some mistakes in the second paragraph of tutorial of 1439D.$dp2_j\times dp1_{i-j-1}+dp2_{i-j-1}\times dp1_j$ should be $dp2_{j-1}\times dp1_{i-j}+dp2_{i-j}\times dp1_{j-1}$.$(\binom j2+\binom{i-j}2)dp1_j\times dp1_{i-j-1}$ should be $(\binom j2+\binom{i-j+1}2)dp1_{j-1}\times dp1_{i-j}$.
•  » » Thanks. I updated the blog
 » how to solve div2 E if the array was unsorted?
 » why does my code run so slow in test 4? and it even gets WA in test5! here is the code.98820613
 » In Div1 B i dont understand how to implement the nei array . i used map and i got TLE which makes sence . however doesnt array nei have size of N * N ? doesnt it get MLE ? or if you only consider the candidates how can can you find out that there is an edge between to vertices that both are not candidates in O(1) ?
 » For div1A editorialWe can do this for the first n−2 cells in the rowIt should be m-2 cells.
 » 8 days ago, # | ← Rev. 2 →   In the analysis of problem 1439B is it is written thatIf d(u) > k, remaining vertices will form a subset that every vertex have at least k neighbors in the subset, so we'll print this subset as answer.If d(u) = k − 1, we consider u and all neighbors of u as candidate for clique of size k. then we erase u and all edges attached to it.So, what we do when d(u) = k? Maybe there is typo here and first condition should be d(u) ≥ k?
 » Could someone explain $O(N\log N)$ solution for 1D? I came up with $O(N^2)$ solution myself (98827591), and it seems to be similar to the code in this comment, but I still don't see the way to reduce it to FFT. (the $(n-i)^j$ term is troublesome)
•  » » I think shayan.p and alishahali1382 know about it
•  » » I dont know the O(n.log(n)) solution but I had a O(n.log(n)^2) with FFT and divide and conquer. here dp1 and dp2 have same definitions as the editorial. Spoiler#include #pragma GCC optimize ("O2,unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair pii; typedef pair piii; typedef pair pll; #define debug(x) cerr<<#x<<'='<<(x)< Poly; ll n, m, k, u, v, x, y, t, a, b, ans; ll dp1[MAXN], dp2[MAXN]; ll F[MAXN], I[MAXN]; int rev[N]; Poly A, B, C, constant; ll powmod(ll a, ll b){ if (a==1) return 1; if (b<0) b+=mod-1; ll res=1; for (; b; b>>=1, a=a*a%mod) if (b&1) res=res*a%mod; return res; } ll nCr(ll n, ll r){ if (r<0 || r>n) return 0; return F[n]*I[r]%mod*I[n-r]%mod; } ll Solve(ll n, ll m){ return powmod(2, m)*powmod(n+1, m-1)%mod*(n+1-m)%mod; } void NTT(Poly &A, bool inv){ int n=A.size(), lg=0; while ((1<>1]>>1)|((i&1)<<(lg-1)); if (rev[i]>1; divide(tl, mid); A.resize(tr-tl+1, 0); B.resize(mid-tl); for (int i=0; i>n>>m; for (int len=1; len<=m; len++){ ll res=nCr(m, len)*dp2[len]%mod; // res=res*(n+1)%mod; res=res*Solve(n+1-len-2, m-len)%mod; ans=(ans + res)%mod; } ans=ans*(n-m+1)%mod; if (ans<0) ans+=mod; cout<
 » 8 days ago, # | ← Rev. 3 →   My solution to $C$was slightly different. I divided the matrix into $2 \times 2$matrices. And solved it independently for each $2 \times 2$matrix. If the matrix has 4 ones, do a flip and make it have 1 one. If the matrix has 1 one, do a flip and make it have 2 ones. If the matrix has 2 ones, do a flip and make it have 3 ones If the matrix has 3 ones, do a flip and make it have 0 ones Here is my code and explanation
 » Div1E editorial when
 » Solution for D1EFirstly, it can be shown that the game is equivalent to the following game:Instead of coloring squares, imagine there are tokens on them. Initially, let all black squares contain 1 token and all white squares contain no tokens. In one move, one removes a token, but may add 1 token to any subset of the ancestors of the square from which the token was removed. There can be multiple tokens within the same square. A player loses when in his turn, there are no tokens remaining.See the editorial of 494E - Sharti, a very similar problem, for a detailed explanation.Now, in general, we can calculate Grundy values for a game by breaking it down into independent games and XOR'ing these values together. For this problem, we consider each initial token to be an independent game.How do we find the Grundy number for a game whose only starting token is at $(x, y)$? Turns out, the Grundy number is $2^{x+y}$. This can be proven using induction on $x+y$, with the root as a base case.We can now see that when we perform the cheating operation, we flip an arbitrary number of bottommost bits in the Grundy number of the overall game. As the second player, we want the Grundy number to become 0. It is not hard to see that the number of cheating operations necessary to do so is equal to the number of pairs of adjacent bits in the Grundy number that differ from each other, as each cheating operation reduces this value by at most 1, and it is easy to construct a sequence of cheating operations that are exactly this long.We build a tree out of the nodes that are endpoints of paths given in the input, in addition to any LCAs of any pair of these nodes. There is a well-known algorithm to solve this problem using a stack after sorting the nodes by DFS order; see 1320E - Treeland and Viruses for an example. After building the tree, which contains at most O(m) nodes, we can find the edges of the tree that are covered by at least one input path using a simple DFS, and use this to express the Grundy number of the game as the XOR of intervals of 1 bits, and thus solve the problem.The most annoying part: how to find LCAs and to sort nodes in DFS order, given that the tree is prohibitively large and cannot be preprocessed in full (which is necessary for the usual LCA algorithms, and the normal way of finding DFS order). It is possible to do both, by looking at the recursive structure of the tree and the binary representations of nodes' positions. The details, however, are extremely complicated. I spent several hours debugging due to a number of bugs in these two functions, and ended up having to rewrite the DFS ordering several times.My code can be found in my submissions list, but bear in mind it is filled with debugging statements, massive commented out sections, and whole functions that have been abandoned. It is not nice to look at.