By Mehrdad_Sohrabi, history, 18 months ago,

We want to mention BamiTorabi who we thank for helping us with translating the problems and writing the tutorials, and also we apologize for div1 A2 and B hopefully you will forgive us :( .

official solution

official solution

official solution

Prepared by AliShahali1382 Editorial by Amoo_Safar

official solution

official solution

official solution
O(n2) solution

Prepared by AliShahali1382 Editorial by AliShahali1382

official solution

• +94

 » 18 months ago, # |   +7 Auto comment: topic has been updated by Mehrdad_Sohrabi (previous revision, new revision, compare).
•  » » 18 months ago, # ^ |   +26 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[5]; 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, 0, 1, 1, 0}, p[1] = {0, 1, 0, 0, 1, 1}, p[2] = {1, 0, 1, 1, 0, 0}, p[3] = {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[3]; 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[0]) if (!p.first) vec.pb(p); for (pii p:v[1]) vec.pb({p.first, p.second|(1<<(n-1))}); for (pii p:v[0]) if (p.first) vec.pb(p); for (pii p:v[2]) 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[0])}; for (int i=1; i
•  » » » 18 months ago, # ^ | ← Rev. 2 →   -8 Hi, Can you tell me whats wrong in my code 98818397 for problem E (div 2). UPD: I have misread the question, mybad.
 » 18 months ago, # |   +5 can you please provide the codes?? :)
•  » » 18 months ago, # ^ | ← Rev. 2 →   +31 Now is 2 am In Tehran, We will done it tomorrow as soon as possible.
•  » » » 18 months ago, # ^ |   +11 ok no problem.thank you :)
•  » » 18 months ago, # ^ |   +4 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?
•  » » 18 months ago, # ^ | ← Rev. 19 →   -26 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[1][1]) x ^= 0, y ^= 1, z ^= 1, t ^= 1; if (a[2][1]) x ^= 1, y ^= 0, z ^= 1, t ^= 1; if (a[1][2]) x ^= 1, y ^= 1, z ^= 0, t ^= 1; if (a[2][2]) 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[1][1]) x ^= 0, y ^= 1, z ^= 1, t ^= 1; if (a[2][1]) x ^= 1, y ^= 0, z ^= 1, t ^= 1; if (a[1][2]) x ^= 1, y ^= 1, z ^= 0, t ^= 1; if (a[2][2]) 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 
•  » » » 18 months ago, # ^ |   +46 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.
•  » » » » 18 months ago, # ^ | ← Rev. 2 →   +6 Ok then, but actually the result is also ≈ $O(f(x))$, what should I write then
•  » » » » » 18 months ago, # ^ |   +5 try this Problem C2 <= (3nm/4) operations solution
•  » » » » » » 18 months ago, # ^ |   0 I updated and also add an extra solution
•  » » » » » » 18 months ago, # ^ |   +1 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$
•  » » » » » » » 18 months ago, # ^ | ← Rev. 3 →   +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[1][1]) x ^= 0, y ^= 1, z ^= 1, t ^= 1; if (a[2][1]) x ^= 1, y ^= 0, z ^= 1, t ^= 1; if (a[1][2]) x ^= 1, y ^= 1, z ^= 0, t ^= 1; if (a[2][2]) 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 
•  » » » » » » » » 18 months ago, # ^ |   0 Now that's what I call stress testing XD.
•  » » » 18 months ago, # ^ |   0 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?
•  » » » » 18 months ago, # ^ | ← Rev. 4 →   0 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$}
•  » » » » » 18 months ago, # ^ |   0 cool... thank you ... now I understood ... nice explaination
•  » » 18 months ago, # ^ |   +1 check out anyone's code in submission.
 » 18 months ago, # |   +2 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?
•  » » 18 months ago, # ^ | ← Rev. 2 →   0 111 110 101 What about this?
 » 18 months ago, # |   +1 Lodu contest
 » 18 months ago, # |   +17 I'd like to see an official Java solution for Div1B.There was one, right?
 » 18 months ago, # | ← Rev. 3 →   +5 [deleted]
•  » » 18 months ago, # ^ |   0 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.
 » 18 months ago, # | ← Rev. 2 →   +11 ㅤ
 » 18 months ago, # |   0 Please somebody help.I dont know why this solution 98723050 for 1440A - Купи строку is showing Time limit exceeded on pretest 3. Thank you!!!
•  » » 18 months ago, # ^ |   +4 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
•  » » 18 months ago, # ^ |   +1 The size of your DP if just 100, but the size of string can be up to 1000, so it might be undefined behavior.
 » 18 months ago, # |   +16 Codeforces Round #50000000 Div2 C — So just enumerate all $2^{256}$ cases and handle them individually...
•  » » 18 months ago, # ^ |   +2 But wait you have a overflow here.
•  » » » 18 months ago, # ^ |   +12 Can't even make a joke without getting Memory Limit Exceeded error! XD
 » 18 months ago, # |   0 can pls someone explain sum of medians
•  » » 18 months ago, # ^ |   0 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
•  » » » 18 months ago, # ^ | ← Rev. 2 →   0 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
 » 18 months ago, # |   0 I couldn't understand how to solve for the last nth row in binary table question. Can someone plssss help me out....
•  » » 18 months ago, # ^ | ← Rev. 2 →   0 ㅤ
•  » » » 18 months ago, # ^ |   0 how would you make the bottom right cell 0 in case of odd n and odd m?
•  » » » » 18 months ago, # ^ | ← Rev. 3 →   0 ㅤ
•  » » 18 months ago, # ^ |   0 you need 3 operations to remove a one
 » 18 months ago, # |   0 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.
•  » » 18 months ago, # ^ |   +23 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[0], a[i]) <= x + sum(a[0], a[l-1]). This binary search is also simple.
•  » » 18 months ago, # ^ | ← Rev. 2 →   +23 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.
•  » » » 18 months ago, # ^ |   0 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.
•  » » » » 18 months ago, # ^ | ← Rev. 2 →   +6 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.
•  » » » » » 18 months ago, # ^ |   0 Oh wow I misunderstood the statement. I thought the modification was for only one position. Again, thanks for ur help and patiences.
 » 18 months ago, # |   0 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.
 » 18 months ago, # |   -42 Div2 C1/C2 was the most complex implementation problem. Upvote if you agree!
•  » » 18 months ago, # ^ | ← Rev. 2 →   -9 Btw nice way of losing contribution.
 » 18 months ago, # |   +1 In the contest dashboard, there isn't the option of "Tutorials(en)" in the contest material section. please look into it.Thank you :)
 » 18 months ago, # | ← Rev. 2 →   0 Where is the editorial for div2 C1 (easy version of binary table)?
•  » » 18 months ago, # ^ |   -10 You can refer to this
•  » » 18 months ago, # ^ |   +8 The main idea is for every cell in a 2 × 2 you can change it without change other cells.
 » 18 months ago, # | ← Rev. 2 →   +10 Nice problemset.
•  » » 18 months ago, # ^ |   -30 LODU problemset.
 » 18 months ago, # | ← Rev. 2 →   0 ⠀
•  » » 18 months ago, # ^ | ← Rev. 2 →   0 ⠀
 » 18 months ago, # |   +10 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
•  » » 18 months ago, # ^ |   0 You can store neighbours of each vertice in an unordered_set.
•  » » » 18 months ago, # ^ |   0 got AC with unordered_set with 810ms and 530ms when eliminated duplicated checks
•  » » » » 18 months ago, # ^ |   0 I've tried unordered_set but I'm still getting TLE :-(
•  » » » » » 18 months ago, # ^ |   0 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
•  » » » » » » 18 months ago, # ^ |   0 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).
•  » » 18 months ago, # ^ | ← Rev. 4 →   0 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; } 
•  » » » 18 months ago, # ^ |   0 what does "— 1" part do (tried googling it but all i get is HTML blah ..)?
•  » » » » 18 months ago, # ^ |   -10 K-1 mean
•  » » » 18 months ago, # ^ | ← Rev. 2 →   -8 I've never seen an if statement like this if(nxt != k — 1), can you tell me what it does exactly? Thanks in advance.
•  » » » » 18 months ago, # ^ |   -9 Forgive me when such a bug probably occurred while copying
•  » » 13 months ago, # ^ |   0 This is the official code, with the clique check section fully commented to explain the logic: https://codeforces.com/blog/entry/84731#comment-785719That comment also contain a little more details on the method used by the editorial code.
 » 18 months ago, # |   +8 B formulars: Consider using \cdot multiplication sign instead of a simple dot.$n\cdot{k}$ vs $n.k$
 » 18 months ago, # |   +19 Could you please take a look at hack 678765? I'm getting "Unexpected Verdict".
•  » » 18 months ago, # ^ |   +3 Usually it means that the model solution is giving TLE or something.
 » 18 months ago, # | ← Rev. 2 →   0 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?
•  » » 18 months ago, # ^ |   0 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.
•  » » » 18 months ago, # ^ |   0 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.
 » 18 months ago, # |   +26 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.
•  » » 18 months ago, # ^ | ← Rev. 2 →   -17 We want to check cliques offline so we fix a vertex and check all the edges for that vertex
•  » » » 18 months ago, # ^ |   +5 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)?
•  » » » » 18 months ago, # ^ |   +1 Yep
•  » » » » » 18 months ago, # ^ | ← Rev. 2 →   0 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.
•  » » » 18 months ago, # ^ |   0 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?
•  » » » » 18 months ago, # ^ | ← Rev. 2 →   0 WrongA vertex is included in the candidate list at most twice, so it's $O(n)$ space complexity, I think.
•  » » » » 18 months ago, # ^ |   0 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?
•  » » » » » 18 months ago, # ^ |   0 Oh, you are right. Thanks.
 » 18 months ago, # |   0 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
•  » » 18 months ago, # ^ |   0 you might find this useful(for c1 and c2): https://www.youtube.com/watch?v=qHVt2ulsVxYresharing prev comment
•  » » » 18 months ago, # ^ |   0 Thanks much for this. Have you implemented this by any chance? I find it tricky to implement the n x 2 section.
•  » » 18 months ago, # ^ |   0 At which part you feel hard to understand ? I will add some other approachs later and extra comments & proves
•  » » » 18 months ago, # ^ |   0 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
•  » » » » 18 months ago, # ^ |   0 :( If not using XOR to implement, you will have to check 4 if-else
 » 18 months ago, # |   +106 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.
•  » » 18 months ago, # ^ |   +8 well ive change my vector to unordered_set and i got Ac, thats weird!
 » 18 months ago, # |   0 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?
•  » » 18 months ago, # ^ |   0 I just checked, the same has already been answered in the comments!
 » 18 months ago, # |   -21 LGM Errichto orz
 » 18 months ago, # |   +10 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.
 » 18 months ago, # | ← Rev. 2 →   0 An intuitive way and short clean code to solve Div2 C2 Hard version solution hope you understand 98807026 Thank you.
 » 18 months ago, # | ← Rev. 2 →   0 Anyone help me find whats wrong in my submission 98818397 for problem E (div 2).Thanks in advance!!
 » 18 months ago, # | ← Rev. 3 →   +10 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}$.
•  » » 18 months ago, # ^ |   0 Thanks. I updated the blog
 » 18 months ago, # |   0 how to solve div2 E if the array was unsorted?
 » 18 months ago, # |   0 why does my code run so slow in test 4? and it even gets WA in test5! here is the code.98820613
 » 18 months ago, # |   0 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) ?
•  » » 13 months ago, # ^ |   0 Div1 B /Div2 D explanation: Assuming that you already know how to solve the following problem Given a graph $G$ with $N$ vertices and $M$ edges, solve $Q$ queries offline in time $O(N+M+Q)$. Each query has the form: Given two vertices $u$ and $v$, determine if there's an edge between them. then this problem can be reduced to it (there are about $N \sqrt N$ edges to check), although you should be careful with the memory limit. This is the official code, with the clique check section fully commented to explain the logic: Code#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 vertex_process_order_of[N], degree[N], temp_adjacency_list_i_at_process_order[N]; bool form_clique_at_process_order[N], temp_bool_adjacent_to_i_at_process_order[N]; vector adjacency_list[N], A; vector potential_to_check_clique[N]; bool comparison_sort_by_increasing_process_order(int x, int y) { return vertex_process_order_of[x] < vertex_process_order_of[y]; } void solve() { int n, m, k; cin >> n >> m >> k; A.clear(); for(int i = 0; i <= n; i++) adjacency_list[i].clear(), potential_to_check_clique[i].clear(), vertex_process_order_of[i] = degree[i] = temp_adjacency_list_i_at_process_order[i] = 0, form_clique_at_process_order[i] = temp_bool_adjacent_to_i_at_process_order[i] = false; // read adjacency_list for(int i = 0; i < m; i++) { int x, y; cin >> x >> y; adjacency_list[x].pb(y); adjacency_list[y].pb(x); } if(k > 500) { cout << -1 << endl; return; } set priority_queue; for(int i = 1; i <= n; i++) priority_queue.insert({degree[i] = (int)adjacency_list[i].size(), i}); int vertex_process_order = 1; while((int)priority_queue.size()) { pii p = *priority_queue.begin(); if(p.ff >= k) { cout << 1 << " " << (int)priority_queue.size() << endl; for(auto u : priority_queue) cout << u.ss << " "; cout << endl; return; } else { priority_queue.erase(p); vertex_process_order_of[p.ss] = vertex_process_order; for(auto u : adjacency_list[p.ss]) if(!vertex_process_order_of[u]) priority_queue.erase({degree[u], u}), priority_queue.insert({--degree[u], u}); A.pb(p.ss); } vertex_process_order++; } // look back to find cliques for(auto i : A) { // the two arrays temp_adjacency_list_i_at_process_order and temp_bool_adjacent_to_i_at_process_order // can be recreated new for each loop iteration, however that will increase the time complexity // so they're reused (and cleaned properly) int adjacency_index = 0; // construct temp_adjacency_list_i_at_process_order & temp_bool_adjacent_to_i_at_process_order for(auto u : adjacency_list[i]) if(vertex_process_order_of[u] > vertex_process_order_of[i]) temp_adjacency_list_i_at_process_order[adjacency_index++] = u, temp_bool_adjacent_to_i_at_process_order[u] = true; int num_adjacent_i_at_process_time=adjacency_index; // check potential_to_check_clique[i] for(auto u : potential_to_check_clique[i]) if(!temp_bool_adjacent_to_i_at_process_order[u.ff]) form_clique_at_process_order[u.ss] = false; // reset temp_bool_adjacent_to_i_at_process_order (because it's temp, it should be reset for next iterations) for(int j = 0; j < num_adjacent_i_at_process_time; j++) temp_bool_adjacent_to_i_at_process_order[temp_adjacency_list_i_at_process_order[j]] = false; if(num_adjacent_i_at_process_time != k - 1) continue; form_clique_at_process_order[i] = true; // initialize, might be set to false later // sort (to improve memory localization in following nested loop?) sort(temp_adjacency_list_i_at_process_order, temp_adjacency_list_i_at_process_order + num_adjacent_i_at_process_time, comparison_sort_by_increasing_process_order); // push potential_to_check_clique for other vertices for G for(int j = 0; j < num_adjacent_i_at_process_time; j++) for(int k = j + 1; k < num_adjacent_i_at_process_time; k++) potential_to_check_clique[temp_adjacency_list_i_at_process_order[j]].pb({temp_adjacency_list_i_at_process_order[k], i}); // potential_to_check_clique: [J] = list of pair {K, i} // if J is not adjacent to K, then G isn't a clique // where G is the subgraph with vertices (i) and those adjacent to (i) at process order } // output possible found clique for(auto i : A) { if(!form_clique_at_process_order[i]) continue; cout << 2 << endl; cout << i << " "; for(auto u : adjacency_list[i]) if(vertex_process_order_of[u] > vertex_process_order_of[i]) cout << u << " "; cout << endl; return; } cout << -1 << endl; return; } int32_t main() { use_fast; int t; cin >> t; while(t--) solve(); return 0; } 
 » 18 months ago, # |   0 For div1A editorialWe can do this for the first n−2 cells in the rowIt should be m-2 cells.
 » 18 months ago, # | ← Rev. 2 →   +1 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?
 » 18 months ago, # |   +11 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)
•  » » 18 months ago, # ^ |   0 I think Shayan.P and AliShahali1382 know about it
•  » » 18 months ago, # ^ |   +34 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<
 » 18 months ago, # | ← Rev. 3 →   +6 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
 » 18 months ago, # |   +10 Div1E editorial when
 » 18 months ago, # |   0 Mehrdad_Sohrabi Please provide the implementations also.
 » 18 months ago, # |   +24 Auto comment: topic has been updated by AliShahali1382 (previous revision, new revision, compare).
 » 18 months ago, # |   0 I don't understand B's solution Sum of Medians.Let A = [0 24 34 58 62 64 69 78]. n = 2, k = 4. Length = n * k = 8. If you take ceil(n / 2) numbers from the end and floor(n / 2) numbers from the beginning for each group, you get this:n = 2. ceil( 2 / 2) = ceil(1) = 1.Pick 1 number from the end and 1 number from beginning. Group G = [[78 , 0] , [69, 24], [64, 34], [62, 58]].Now take median of each one and sum them up, Median = ceil( n / 2). sum = 0 + 24 + 34 + 58 = 116. This is not equal to the answer of 165.Let's assume you sorted each group though, G = [[0, 78], [24, 69], [34, 64], [58, 62]] Sum = 78 + 69 + 64 + 62 = 273. This is still not equal to answer of 165.How does the solution work?
•  » » 18 months ago, # ^ | ← Rev. 5 →   -10 please see the definition of median, median of [0, 78] = 0
•  » » » 18 months ago, # ^ | ← Rev. 2 →   0 Oh wait thanks let me see.
•  » » » 18 months ago, # ^ |   0 G = [[0, 78], [24, 69], [34, 64], [58, 62]]The sums would be 0 + 24 + 34 + 58 = 116.This isn't equal to 165 though.
 » 18 months ago, # |   0 This 98841172 my solution for problem "Binary Table", I think it's easier to understand it.
 » 18 months ago, # |   0 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); }can anybody explain this line of code what is logic behind this code in binary table solution
 » 18 months ago, # |   +20 Some of the problem were quite nice theoretically, but no fun at all to code. Or maybe it's just that I don't enjoy the coding as much now that I'm retired.On problem B I got TLE despite having the intended big-O, but the unordered_map constant factor was too high. Problem C I had to spend a lot of time integrating the binary search into the segment tree structure (because just putting it on top of the segment tree adds a log factor that made it too tight). And on problem E I really enjoyed working out the maths (I didn't think I would, but it turns out to be a very nice result), but trying to compute the union of the paths that get painted black was fiddly and my solution is still suffering from time and memory limit errors.
•  » » 17 months ago, # ^ |   0 We don't actually need binary search with the amount of info that we are maintaining it seems: https://codeforces.com/contest/1440/submission/100607410
 » 18 months ago, # |   0 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 - Шарти, 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 - Древляндия и вирусы 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.
 » 17 months ago, # |   0 Can anyone tell what's wrong with this code, it is giving wrong answer for test case 3. https://codeforces.com/contest/1440/submission/102106895
 » 12 months ago, # | ← Rev. 2 →   0 Hi, can you please tell me why this code is giving TLE on test 99? (problem DIV1C) 117855762 I have tried many things to optimize the code, but nothing works. The complexity is O(logn*log(maxn)*(n+q)). Thanks for your time.