#include<bits/stdc++.h> // #define SINGLE_INPUT #define ull unsigned long long #define ll long long #define N 500005 #define MOD 998244353 usingnamespace std;
template<classt,classu> ostream& operator<<(ostream& os,const pair<t,u>& p) { return os<<'['<<p.first<<", "<<p.second<<']'; } template<classt> ostream& operator<<(ostream& os,const vector<t>& v) { os<<'['; int s = 1; for(auto e:v) { if (s) s = 0; else os << ", "; os << e; } return os<<']'; } template<classt,classu> ostream& operator<<(ostream& os,const map<t,u>& mp){ os<<'{'; int s = 1; for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; } return os<<'}'; }
voidsol(){ int n, k; cin >> n >> k; if (k>=n-1) { cout << "1\n"; } else { cout << n << "\n"; } }
#include<bits/stdc++.h> // #define SINGLE_INPUT #define ull unsigned long long #define ll long long #define N 500005 #define MOD 998244353 usingnamespace std;
template<classt,classu> ostream& operator<<(ostream& os,const pair<t,u>& p) { return os<<'['<<p.first<<", "<<p.second<<']'; } template<classt> ostream& operator<<(ostream& os,const vector<t>& v) { os<<'['; int s = 1; for(auto e:v) { if (s) s = 0; else os << ", "; os << e; } return os<<']'; } template<classt,classu> ostream& operator<<(ostream& os,const map<t,u>& mp){ os<<'{'; int s = 1; for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; } return os<<'}'; }
voidsol(){ int n, k; cin >> n >> k; vector<int> a(2*n); for (auto& i:a) cin >> i; vector<int> c(n+1); for (int i=0; i<n; i++) { c[a[i]]++; } // cout << c << endl; int p = 0; vector<int> l, r; for (int i=1; i<=n; i++) { if (l.size() == 2*k) continue; if (c[i] == 2) { l.push_back(i); l.push_back(i); } } for (int i=1; i<=n; i++) { if (r.size() == 2*k) continue; if (c[i] == 0) { r.push_back(i); r.push_back(i); } } for (int i=1; i<=n; i++) { if (l.size() == 2*k) continue; if (c[i] == 1) { l.push_back(i); r.push_back(i); } } for (int i:l) { cout << i << " "; } cout << "\n"; for (int i:r) { cout << i << " "; } cout << "\n"; }
#include<bits/stdc++.h> // #define SINGLE_INPUT #define ull unsigned long long #define ll long long #define N 500005 #define MOD 998244353 usingnamespace std;
template<classt,classu> ostream& operator<<(ostream& os,const pair<t,u>& p) { return os<<'['<<p.first<<", "<<p.second<<']'; } template<classt> ostream& operator<<(ostream& os,const vector<t>& v) { os<<'['; int s = 1; for(auto e:v) { if (s) s = 0; else os << ", "; os << e; } return os<<']'; } template<classt,classu> ostream& operator<<(ostream& os,const map<t,u>& mp){ os<<'{'; int s = 1; for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; } return os<<'}'; }
voidsol(){ int n; cin >> n; vector<int> c(n); for(int i=0; i<n; i++) { int x; cin >> x; c[x]++; } int c1 = 0; for (int i=0; i<n; i++) { if (c[i] == 0) { cout << i << "\n"; return ; } if (c[i] == 1) c1++; if (c1 == 2) { cout << i << "\n"; return ; } } cout << n << "\n"; }
#include<bits/stdc++.h> // #define SINGLE_INPUT #define ull unsigned long long #define ll long long #define N 500005 #define MOD 998244353 usingnamespace std;
template<classt,classu> ostream& operator<<(ostream& os,const pair<t,u>& p) { return os<<'['<<p.first<<", "<<p.second<<']'; } template<classt> ostream& operator<<(ostream& os,const vector<t>& v) { os<<'['; int s = 1; for(auto e:v) { if (s) s = 0; else os << ", "; os << e; } return os<<']'; } template<classt,classu> ostream& operator<<(ostream& os,const map<t,u>& mp){ os<<'{'; int s = 1; for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; } return os<<'}'; }
structSHash { // string s[] index from 0 to n-1 // B[i] = BASE^i // s[i...j] = s[0...j] - s[0...i-1] // hash s[0...i-1] = H[i] = s[0]*B[i-1]+s[1]*B[i-2]+...+s[i-1]*B[0] // hash s[0...j] = H[j+1] = s[0]*B[j]+s[1]*B[j-1]+...+s[j]*B[0] // hash s[i...j] = H[j+1] - H[i]*B[j-i+1] // hash s[i...j-1] = H[j] - H[i]*B[j-i] vector<ull> H, B; ull len, base, mod; SHash(string& s, ull base = 173, ull mod = 998244353) : H(s.size() + 1, 0), B(s.size() + 1, 1), len(s.size()), base(base), mod(mod) { for (int i = 1; i <= len; i++) { H[i] = (H[i - 1] * base % mod + s[i - 1]) % mod; B[i] = B[i - 1] * base % mod; } } ull hash(int l, int r){ // hash of s[l...r-1] return (H[r] - H[l] * B[r - l] % mod + mod) % mod; }; };
voidsol(){ int n, m; cin >> n >> m; string s; cin >> s; s = '#'+s+'#'; auto rs = s; reverse(rs.begin(), rs.end()); SHash h1(s), h2(rs); // s[l,r) == rs[n-r+2, n-l+2) SHash h3(s, 191, 1e9+7), h4(rs, 191, 1e9+7); vector<vector<int>> p1(n+1, vector<int>(26)), p2(n+1, vector<int>(26)); for (int i=1; i<=n; i++) { int c = s[i]-'a'; p1[i] = p1[i-1]; p2[i] = p2[i-1]; if (i%2) p1[i][c]++; else p2[i][c]++; } // cout << p1 << "\n"; // cout << p2 << endl; for (int i=0; i<m; i++) { int x, y; cin >> x >> y; ll len = y-x+1; ll cnt1, cnt2; int c1 = s[y-1]-'a', c2 = s[y]-'a'; if (y%2) { cnt1 = p1[y][c2]-p1[x-1][c2]; cnt2 = p2[y-1][c1]-p2[x-1][c1]; } else { cnt1 = p2[y][c2]-p2[x-1][c2]; cnt2 = p1[y-1][c1]-p1[x-1][c1]; } // cout << cnt1 << " " << cnt2 << endl; if (cnt1+cnt2 == len) { if (s[y] == s[y-1]) cout << "0\n"; else { // 奇偶交替,不存在奇数长度子串贡献 ll b = len/2; // 对于2,4,6,8,,,等长度子串 存在非回文 cout << b*(b+1)/2*2 << "\n"; } } else { cout << (len-2)*(2+len-1)/2 + ( h1.hash(x, y+1) == h2.hash(n-y+1, n-x+2) && h3.hash(x, y+1) == h4.hash(n-y+1, n-x+2) ? 0 : len) << "\n"; } }
#include<bits/stdc++.h> // #define SINGLE_INPUT #define ull unsigned long long #define ll long long #define N 500005 #define MOD 998244353 usingnamespace std;
template<classt,classu> ostream& operator<<(ostream& os,const pair<t,u>& p) { return os<<'['<<p.first<<", "<<p.second<<']'; } template<classt> ostream& operator<<(ostream& os,const vector<t>& v) { os<<'['; int s = 1; for(auto e:v) { if (s) s = 0; else os << ", "; os << e; } return os<<']'; } template<classt,classu> ostream& operator<<(ostream& os,const map<t,u>& mp){ os<<'{'; int s = 1; for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; } return os<<'}'; }
voidsol(){ int n; cin >> n; vector<vector<int>> g(n+1); vector<vector<int>> d(3, vector<int>(n+1)); for (int i=1; i<n; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } int c = 0; function<void(int,int,int)> dfs = [&](int u, int fa, int o) { for (int v : g[u]) { if (v == fa) continue; d[o][v] = d[o][u] + 1; if (d[o][v] > d[o][c]) c = v; dfs(v, u, o); } }; dfs(1, 0, 0); dfs(c, 0, 1); dfs(c, 0, 2); // cout << d << endl; // d[2][c] 直径 vector<int> h; // 某条直径上的中心点,直径奇数一个,偶数两个 for (int i=1; i<=n; i++) { if (d[1][i]+d[2][i] == d[2][c] && abs(d[1][i]-d[2][i]) == d[2][c]%2) h.push_back(i); } if(h.size() == 1) { cout << d[2][c]/2+1 << "\n"; for (int i=0; i<=d[2][c]/2; i++) { cout << h[0] << " " << i << "\n"; } } else { vector<pair<int,int>> p; for (int i=d[2][c]/2; i>=0; i-=2) { p.emplace_back(h[0], i); p.emplace_back(h[1], i); } cout << p.size() << "\n"; for (auto [x, y]:p) { cout << x << " " << y << "\n"; } } }