func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; long long n, m, k, mod = 1e9 + 7; map<long long, long long> mp; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand(time(0)); cin >> n >> k; vector<long long> a(n + 2); vector<long long> b(k + 2); for (long long i = 1; i <= n; ++i) cin >> a[i], a[i] -= i; a[0] = -2e9; a[n + 1] = 2e9; b[k + 1] = n + 1; for (long long i = 1; i <= k; ++i) cin >> b[i]; long long ans = 0; for (long long i = 0; i <= k; ++i) { long long l = b[i]; long long r = b[i + 1]; if (a[l] > a[r]) { cout << -1; exit(0); } vector<long long> v; for (long long j = l + 1; j < r; ++j) { if (a[j] > a[r] || a[j] < a[l]) continue; auto x = upper_bound(v.begin(), v.end(), a[j]); if (x == v.end()) v.push_back(a[j]); else *x = a[j]; } ans += r - l - 1 - v.size(); } cout << ans; } |
#include <bits/stdc++.h> using namespace std; int N, K, M, S; int arr[500000]; int raito[500005]; int seen[500005]; int bucket[500005]; int shift[500005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> M >> K >> N >> S; for (int i = 0; i < M; i++) { cin >> arr[i]; } int t; for (int i = 0; i < S; i++) { cin >> t; bucket[t]++; } for (int i = 0; i < 500005; i++) seen[i] = -1; for (int i = M - 1; i >= 0; i--) { raito[i] = seen[arr[i]]; seen[arr[i]] = i; } int maxima = 0; for (int i = 0; i < 500005; i++) { if (bucket[i] == 0) continue; int cur = seen[i]; int p = 1; while (cur != -1 && p < bucket[i]) { cur = raito[cur]; p++; } if (cur == -1) { cout << -1; return 0; } shift[i] = cur; maxima = max(maxima, cur); } vector<int> adj; for (int i = 0; i < M;) { int total = i / K + 1 + ((M - 1) - (maxima + 1) + 1) / K; if (total >= N) { int extra = K - S; for (int j = i; j <= maxima; j++) { if (extra <= 0 && bucket[arr[j]] == 0) { adj.push_back(j + 1); } else if (bucket[arr[j]] == 0) { extra--; } else { bucket[arr[j]]--; } } cout << adj.size() << n ; for (int j = 0; j < adj.size(); j++) { cout << adj[j] << ; } return 0; } if (i + 1 == M) break; int pp = 0; if (bucket[arr[i]] != 0) { shift[arr[i]] = raito[shift[arr[i]]]; maxima = max(maxima, shift[arr[i]]); if (shift[arr[i]] == -1) { cout << -1; return 0; } } i++; } cout << -1; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 400010; int n; int fa[MAXN][21]; int dep[MAXN]; int LCA(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = 20; i >= 0; i--) { if (dep[x] - (1 << i) >= dep[y]) { x = fa[x][i]; } } if (x == y) return x; for (int i = 20; i >= 0; i--) { if (fa[x][i] != fa[y][i]) { x = fa[x][i]; y = fa[y][i]; } } return fa[x][0]; } int main() { scanf( %d , &n); int L, R; L = R = 1; int mx = 0; int p; for (int i = 2; i <= n; i++) { scanf( %d , &p); fa[i][0] = p; dep[i] = dep[p] + 1; for (int j = 1; j <= 20; j++) { fa[i][j] = fa[fa[i][j - 1]][j - 1]; } int z = LCA(i, L); int ans1 = dep[i] + dep[L] - 2 * dep[z]; z = LCA(i, R); int ans2 = dep[i] + dep[R] - 2 * dep[z]; if (ans1 > ans2 && ans1 > mx) { mx = ans1; R = i; } if (ans2 >= ans1 && ans2 > mx) { L = i; mx = ans2; } if (i == n) printf( %d n , mx); else printf( %d , mx); } } |
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize( Ofast,no-stack-protector ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) #pragma GCC optimize( unroll-loops ) long long fun(long long n, long long m, long long mid) { long long val = 0; for (long long i = 1; i <= n; i++) val += min(m, (mid - 1) / i); return val; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long tst = 1; for (long long tt = 1; tt <= tst; tt++) { long long n, m, k; cin >> n >> m >> k; long long l = 1, r = (n * m) + 1; while (l < r) { long long mid = (l + r) / 2; if (fun(n, m, mid) < k) l = mid + 1; else r = mid; } cout << l - 1 << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int main() { long long a, b; while (cin >> a >> b) { long long suma = 0, sumb = 0; long long ca = 0, cb = 0; long long limit = lcm(a, b); long long now = 0, prev = 0; while (now <= limit) { if (now % b != 0 || (now % a == 0 && a > b)) { suma += now - prev; } else { sumb += now - prev; } prev = now; now = min(a * (ca + 1), b * (cb + 1)); if (now % a == 0) ca++; if (now % b == 0) cb++; } cout << (suma == sumb ? Equal : suma > sumb ? Dasha : Masha ) << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, k, dp[2000 + 10][2000 + 10]; bool v[2000 + 10][2000 + 10]; vector<int> vec[2000 + 10]; int solve(int curr, int kk) { if (kk > k) return 0; if (kk == k) return 1; if (v[curr][kk]) return dp[curr][kk]; for (int i = 0; i < vec[curr].size(); i++) { dp[curr][kk] += (solve(vec[curr][i], kk + 1)) % 1000000007; dp[curr][kk] %= 1000000007; } v[curr][kk] = true; return dp[curr][kk]; } int main() { ios::sync_with_stdio(false), cin.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += i) { if (!(j % i)) vec[i].push_back(j); } } cout << solve(1, 0) << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int a[100005]; int dp[302][100005]; int main() { ios::sync_with_stdio(0); int n, q; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; cin >> q; memset(dp, 0, sizeof(dp)); for (int k = min(300, n); k >= 1; --k) { for (int i = n; i >= 1; --i) { if (i + a[i] + k > n) dp[k][i] = 1; else dp[k][i] = 1 + dp[k][i + a[i] + k]; } } while (q--) { int p, k; cin >> p >> k; if (k <= 300) cout << dp[k][p] << endl; else { int ans = 0; while (p <= n) { p = p + a[p] + k; ans++; } cout << ans << endl; } } } |
#include <bits/stdc++.h> using namespace std; long long n, ans = 0, minm; long long A[10 * 100000 + 6]; unordered_map<long long, long long> M; long long pos, neg, cc = 0, k = 0; int main() { scanf( %lld , &n); for (int i = 1; i <= n; ++i) { scanf( %lld , &A[i]); M[A[i] - i]++; ans += abs(A[i] - i); if (A[i] <= i) neg++; else pos++; } minm = ans; for (long long i = 1, j = n; i <= n - 1; ++i, --j) { if (A[j] - j - cc <= 0) --neg; else --pos; ans -= abs(A[j] - j - cc); ans += A[j]; ++pos; M[A[j] + cc]++; M[A[j] - j]--; ++cc; ans -= pos; ans += neg; pos -= M[cc]; neg += M[cc]; if (ans <= minm) { minm = ans; k = i; } } printf( %lld %lld n , minm, k); return 0; } |
#include <bits/stdc++.h> using namespace std; int a[200005], ans1 = 1, ans2 = 0; int main() { int n, b, b1; scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %d , &a[i]); } sort(a + 1, a + 1 + n); b1 = 0; for (int i = 2; i <= n; ++i) { b = a[i] - a[i - 1]; if (b == 0) { continue; } else if (b == 1) { if (b1 == 2) { ans1++; b1 = 0; } else if (b1 == 1) { b1 = 2; } else if (b1 == 0) { b1 = 1; } } else if (b == 2) { if (b1 == 0) { b1 = 2; } else if (b1 >= 1) { b1 = 0; ans1++; } } else { b1 = 0; ans1++; } } for (int i = 1; i <= n; ++i) { b = a[i] - a[i - 1]; if (i == 1) { a[i]--; ans2++; continue; } if (b >= 1) ans2++; if (b == 0 && a[i] != a[i + 1]) { ans2++; a[i]++; } else if (b == 1 && a[i] == a[i + 1] - 1) { continue; } else if (b == 1 && a[i] == a[i + 1] - 2) { continue; } else if (b == 1 && a[i] != a[i + 1]) { a[i]++; } else if (b >= 2) { a[i]--; } } printf( %d %d n , ans1, ans2); return 0; } |
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int cases; cin >> cases; while (cases--) { int num; scanf( %d , &num); vector<pair<int, int> > coords; pair<int, int> z(0, 0); coords.push_back(z); for (int i = 0; i < num; ++i) { int x, y; scanf( %d%d , &x, &y); coords.push_back({x, y}); } sort(coords.begin(), coords.end()); string out; bool ok = true; for (int i = 1; i <= num; ++i) { int diffx = coords[i].first - coords[i - 1].first; int diffy = coords[i].second - coords[i - 1].second; if (diffx < 0 || diffy < 0) { ok = false; break; } while (diffx--) out += R ; while (diffy--) out += U ; } if (ok) { cout << YES << endl; cout << out << endl; } else { cout << NO << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int M = 110; int a[M]; int main() { int n, cnt = 0; scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , a + i); if (a[i] % 2 == 0) cnt++; } if (n % 2 == 0) { printf( No n ); return 0; } if (a[1] % 2 == 0 || a[n] % 2 == 0) { printf( No n ); return 0; } else { printf( Yes n ); return 0; } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> T mulmod(T a, T b) { if (b == 0) return 0; if (b == 1) return a; T x = mulmod(a, b >> 1); x = (x + x) % 1000000007; if (b & 1) x = (x + a) % 1000000007; return x; } template <class T> T larger(T a, T b) { return (a > b ? a : b); } template <class T> T smaller(T a, T b) { return (a < b ? a : b); } template <class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> T lcm(T a, T b) { return (a * b) / gcd(a, b); } template <class T> string converter(T n) { stringstream x; x << n; return x.str(); } template <class T> T pw(T a, T b) { if (b == 0) return 1; T x = pw(a, b >> 1); x = mulmod(x, x); if (b & 1) x = mulmod(x, a); return x; } template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << << e2 << << e3 << endl; } template <class T1, class T2, class T3, class T4> void deb(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << << e2 << << e3 << << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << << e2 << << e3 << << e4 << << e5 << endl; } template <class T1, class T2, class T3, class T4, class T5, class T6> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << << e2 << << e3 << << e4 << << e5 << << e6 << endl; } void letsgo() { long long int a, b, x, n; cin >> a >> b >> n >> x; if (a == 1) { cout << (x + (n % 1000000007 * b) % 1000000007) % 1000000007; return; } long long int aN = pw(a, n); long long int ans = (aN * x) % 1000000007; long long int z = ((aN - 1 + 1000000007) % 1000000007 * b) % 1000000007; z = (z * pw(a - 1, 1000000007 - 2ll)) % 1000000007; cout << (ans + z) % 1000000007; } int main() { letsgo(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const double PI = acos(-1.0); const int maxn = 200 + 10; char str[maxn]; struct TwoSat { int n; vector<int> G[maxn * 2]; bool mark[maxn * 2]; int S[maxn * 2], c; bool isv[maxn]; int sz; bool dfs(int x) { if (mark[x ^ 1]) return false; if (mark[x]) return true; if (x / 2 < xi) { int id = str[x / 2] - a ; if (isv[id] != (x & 1)) return false; tmp[x / 2] = str[x / 2]; } else if (x / 2 == xi) { int t = get_min(str[xi] - a , (x & 1) ? true : false); if (t == -1) return false; tmp[x / 2] = t + a ; } else { int t = get_min(-1, (x & 1) ? true : false); if (t == -1) return false; tmp[x / 2] = t + a ; } mark[x] = true; S[c++] = x; for (int i = 0; i < G[x].size(); i++) if (!dfs(G[x][i])) return false; return true; } void init(int n, char *s) { this->n = n; for (int i = 0; i < n * 2; i++) G[i].clear(); memset(mark, false, sizeof(mark)); sz = strlen(s); for (int i = 0; i < sz; i++) isv[i] = s[i] == V ? true : false; } void add_clause(int x, int xval, int y, int yval) { x = x * 2 + xval; y = y * 2 + yval; G[x ^ 1].push_back(y); G[y ^ 1].push_back(x); } int get_min(int pos, bool c) { for (int i = pos + 1; i < sz; i++) { if (c == isv[i]) return i; } return -1; } char ans[maxn], tmp[maxn]; int xi; void solve() { bool OK = false; for (int pos = 0; pos <= n; pos++) { for (int x = 0; x <= 1; x++) { xi = pos; bool ok = false; memset(mark, false, sizeof(mark)); strcpy(tmp, str); if (pos < n) { int t = get_min(str[pos] - a , (x & 1) ? true : false); if (t == -1) continue; tmp[pos] = t + a ; } for (int i = pos + 1; i < n; i++) tmp[i] = a ; c = 0; for (int i = 0; i < n; i++) { int x1 = (i << 1) | (isv[tmp[i] - a ]); if (!dfs(x1)) { ok = true; break; } } if (n == 200) { } if (ok) continue; OK = true; tmp[n] = 0; if (ans[0] == 0) strcpy(ans, tmp); else if (strcmp(tmp, ans) < 0) strcpy(ans, tmp); } } if (OK) printf( %s n , ans); else printf( -1 n ); } } sat; int main() { int n, m; while (~scanf( %s , str)) { scanf( %d%d , &n, &m); sat.init(n, str); for (int i = 0, x, y, xval, yval; i < m; i++) { char t1[5], t2[5]; scanf( %d%s%d%s , &x, t1, &y, t2); xval = t1[0] == V ? 1 : 0; yval = t2[0] == V ? 1 : 0; --x; --y; sat.add_clause(x, xval ^ 1, y, yval); } scanf( %s , str); sat.solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int thien, n, tt, h; string s; int main() { cin >> tt; for (int ii = 1; ii <= tt; ii++) { cin >> n; thien = 0; h = 0; for (int i = 1; i <= n; i++) { cin >> s; if (s.length() % 2 != 0) thien++; for (int j = 0; j < s.length(); j++) if (s[j] == 1 ) h++; } if (h % 2 != 0 && thien == 0) cout << n - 1 << endl; else cout << n << endl; } } |
#include <bits/stdc++.h> using namespace std; const int maxn = 300010; int p1[maxn], p2[maxn], q[maxn]; int main() { int n, m; scanf( %d%d , &n, &m); for (int i = 0; i < n; i++) { scanf( %d , &p1[i]); p1[i]--; p2[p1[i]] = i; q[i] = n; } for (int i = 0; i < m; i++) { int x, y; scanf( %d%d , &x, &y); x--; y--; x = p2[x]; y = p2[y]; if (x > y) swap(x, y); q[x] = min(q[x], y); } int r = n - 1; long long ans = 0; for (int i = n - 1; i >= 0; i--) { r = min(q[i] - 1, r); ans += r - i + 1; } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, a[200001]; const double esl = 1e-11; double cal(double x) { double t = 0; double ans = 0; for (int i = 1; i <= n; i++) { if (t + a[i] - x < 0) { ans = max(ans, t); t = 0; } else { t += ((double)a[i] - x); ans = max(ans, t); } } ans = max(ans, t); double t1 = 0; for (int i = 1; i <= n; i++) { if (t1 + a[i] - x > 0) { ans = max(ans, -t1); t1 = 0; } else { t1 += ((double)a[i] - x); ans = max(ans, -t1); } } ans = max(ans, -t1); return ans; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); } double l = -(1e5); double r = 1e5; for (int i = 1; i <= 500; i++) { double d = (2.0 * l + r) / 3; double c = (2.0 * r + l) / 3; if (cal(d) > cal(c)) { l = d; } else r = c; } printf( %.10f , cal(l)); return 0; } |
#include <bits/stdc++.h> const int N = 5e4 + 10; int n, m; std::vector<char> a[N]; int sx[N], sy[N]; int main() { int T; scanf( %d , &T); while (T--) { scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) { a[i].resize(m + 2); getchar(); for (int j = 1; j <= m; ++j) scanf( %c , &a[i][j]); } int cnt_x = 0, cnt_y = 0, p_x = 1, p_y = 1; for (int i = 1; i <= n; ++i) { int sum = 0; for (int j = 1; j <= m; ++j) sum += (a[i][j] == * ); sx[i] = sum; } for (int i = 1; i <= m; ++i) { int sum = 0; for (int j = 1; j <= n; ++j) sum += (a[j][i] == * ); sy[i] = sum; } int ans = 0x3f3f3f3f; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { int now = n + m - sx[i] - sy[j]; if (a[i][j] == . ) --now; ans = std::min(ans, now); } printf( %d n , ans); for (int i = 1; i <= n; ++i) a[i].clear(), sx[i] = 0; for (int i = 1; i <= m; ++i) sy[i] = 0; } return 0; } |
#include <bits/stdc++.h> using namespace std; auto clk = clock(); mt19937_64 rang( chrono::high_resolution_clock::now().time_since_epoch().count()); void test_case() { long long int n; cin >> n; long long int a[n + 1]; long long int t = -1, b = -1, c = -1; for (long long int i = 1; i <= n; i++) { cin >> a[i]; } for (long long int i = 2; i <= n - 1; i++) { for (long long int j = i - 1; j >= 1; j--) { if (a[j] < a[i]) { t = j; break; } } for (long long int j = i + 1; j <= n; j++) { if (a[j] < a[i]) { c = j; break; } } if (t != -1 && c != -1) { b = i; break; } t = -1, c = -1; } if (b != -1) { cout << YES << endl; cout << t << << b << << c << endl; } else cout << NO << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); cout << fixed << setprecision(10); long long int t = 1; cin >> t; while (t--) { test_case(); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long nthRoot(long long A, long long N) { float xPre = rand() % 10; float eps = 1e-3; float delX = LLONG_MAX; float xK; while (delX > eps) { xK = ((N - 1.0) * xPre + (float)A / pow(xPre, N - 1)) / (float)N; delX = abs(xK - xPre); xPre = xK; } return ceil(xK); } int main() { long long n; cin >> n; char a[] = { c , o , d , e , f , o , r , c , e , s }; long long in = 0, num; num = nthRoot(n, 10); if (pow(num, 10) < n) num++; for (int i = 0; i <= 10; i++) { if (pow(num - 1, i) * pow(num, 10 - i) >= n) { in = i; } else break; } if (in * (num - 1) + (10 - in) * num - 10 < n - 1) { for (int i = 0; i < in; i++) { long long t = num - 1; while (t--) cout << a[i]; } for (int i = in; i < 10; i++) { long long t = num; while (t--) cout << a[i]; } } else { for (int i = 0; i < 10; i++) cout << a[i]; long long t = n - 1; while (t--) cout << a[9]; } } |
#include <bits/stdc++.h> using namespace std; template <class T> inline void amin(T& a, const T& b) { if (a > b) a = b; } template <class T> inline void amax(T& a, const T& b) { if (a < b) a = b; } template <class T> inline void rv(vector<T>& v, int n) { vector<T>(n).swap(v); for (int i = 0; i < (n); i++) cin >> v[i]; } inline void rv(int* v, int n) { for (int i = 0; i < (n); i++) scanf( %d , &v[i]); } inline void rv(long long* v, int n) { for (int i = 0; i < (n); i++) scanf( %lld , &v[i]); } inline vector<int> rvi(int n) { vector<int> v(n); for (int i = 0; i < (n); i++) scanf( %d , &v[i]); return v; } inline vector<long long> rvl(int n) { vector<long long> v(n); for (int i = 0; i < (n); i++) scanf( %lld , &v[i]); return v; } inline vector<string> rvs(int n) { vector<string> v(n); for (int i = 0; i < (n); i++) scanf( %s , &v[i]); return v; } template <class T> inline void readvv(vector<vector<T>>& v, int m, int n) { vector<vector<T>>(m).swap(v); for (int i = 0; i < (m); i++) readv(v[i], n); } inline void pvi(vector<int>& v) { for (int i = 0; i < (v.size()); i++) printf( %d , v[i]); printf( n ); } inline void pvl(vector<long long>& v) { for (int i = 0; i < (v.size()); i++) { printf( %lld , v[i]); } printf( n ); } inline void pvs(vector<string>& v) { for (int i = 0; i < (v.size()); i++) { printf( %s n , v[i].c_str()); } } int main() { long long n; long double s; cin >> n >> s; vector<vector<long long>> g(n); for (int i = 0; i < (n - 1); i++) { long long u, v; cin >> u >> v; g[u - 1].push_back(v - 1); g[v - 1].push_back(u - 1); } long double leaves = 0, two = 2; for (int i = 0; i < (n); i++) { if (g[i].size() == 1) leaves++; } cout << setprecision(20) << endl; cout << ((two * s) / leaves) << endl; return 0; } |
#include bits/stdc++.h using namespace std; #define all(x) x.begin(),x.end() #define pb push_back #define ff first #define ss second #define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define ar array #define int long long #define F(i,a,b) for(i=a;i<b;++i) #define RF(i,b,a) for(i=b;i>=a;--i) #define NL cout<< n ; #define INF LONG_LONG_MAX #define pii pair<int,int> #define vi vector<int> typedef long long ll; typedef long double ld; const ll mod=1e9+7; bool comp(ll a,ll b) { return a<b; } ll fpow(ll a, ll b) { ll res=1; while(b) { if(b&1) res=(res*a); a=(a*a); b>>=1; } return res; } ll minm(ll a,ll b){ if(a<b) return a; return b; } ll maxm(ll a,ll b){ if(a>b){ return a;} else return b; } int PHI(int k){ // This is Euler s Totient Function. //This function returns no. of coprime numbers to N in range [1,2,....,N] int ans=k; for(int i=2;i*i<=k;++i){ if(k%i==0){ ans-=ans/i; while(k%i==0){ k/=i; } } } if(k>1) ans-=ans/k; return ans; } void solve(){ int n,i; cin >> n; vector<pii>ecells; vector<pii>ocells; for(i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if((i+j)%2){ ocells.pb({i,j}); } else ecells.pb({i,j}); } } // 1 even // 2,3 odd // if not possible ie all odd are filled, fill 2,3 in even int cnt=0; while(cnt<n*n){ int a; cin >> a; if(a==1){ // try to fill 2 in odd cell // if not possible , fill 3 in even if(ocells.empty()){ cout<< 3 <<ecells.back().ff<< <<ecells.back().ss<<endl; ecells.pop_back(); } else{ cout<< 2 <<ocells.back().ff<< <<ocells.back().ss<<endl; ocells.pop_back(); } ++cnt; } else if(a==2){ // fill 1 in even , if possible if(!ecells.empty()){ cout<< 1 <<ecells.back().ff<< <<ecells.back().ss<<endl; ecells.pop_back(); } else{ // fill 3 in ocells cout<< 3 <<ocells.back().ff<< <<ocells.back().ss<<endl; ocells.pop_back(); } cnt++; } else{ // fill 1 in even if possible if(!ecells.empty()){ cout<< 1 <<ecells.back().ff<< <<ecells.back().ss<<endl; ecells.pop_back(); } else{ // fill 2 in odd cout<< 2 <<ocells.back().ff<< <<ocells.back().ss<<endl; ocells.pop_back(); } cnt++; } } } signed main(void) { fast; int _=1; //cin>>_; for(int test_case=1;test_case<=_;++test_case){ solve(); } } |
#include <bits/stdc++.h> int main() { char str[3][100]; int i; for (i = 0; i < 3; i++) { scanf( %s , &str[i][0]); } int r = 0, p = 0, s = 0; for (i = 0; i < 3; i++) { if (str[i][0] == r ) r++; else if (str[i][0] == p ) p++; else if (str[i][0] == s ) s++; } char m = a ; if (r == 1 && s == 2) m = r ; else if (s == 1 && p == 2) m = s ; else if (p == 1 && r == 2) m = p ; if (m != a ) { for (i = 0; i < 3; i++) { if (str[i][0] == m) break; } if (i == 0) printf( F ); else if (i == 1) printf( M ); else if (i == 2) printf( S ); } else printf( ? ); } |
#include <bits/stdc++.h> using namespace std; long long int powe(long long int d, long long int s) { if (s == 1) return d; else if (s == 0) return 1; else { long long int u, v = d; for (u = 1; u < s; u++) { d = d * v; } return d; } } bool isprime(long long int n) { long long int i; for (i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } long long int a[2000005], c[10000005]; int main() { map<int, int> b; long long int i, p, y, mx = -1, j, q; cin >> p >> y; for (i = y; i > p; i--) { q = 1; for (j = 2; j * j <= i && j <= p; j++) { if (i % j == 0) { q = 0; break; } } if (q == 1) { cout << i; return 0; } } cout << -1; } |
#include <bits/stdc++.h> using namespace std; using llint = long long; using par = pair<int, int>; const int MAXN = 200100; llint w[MAXN]; vector<par> edges[MAXN]; int p[MAXN], in[MAXN], out[MAXN], idx[MAXN], id; void dfs(int u) { in[u] = ++id; for (const auto& edge : edges[u]) { if (edge.first == p[u]) continue; p[edge.first] = u; idx[edge.first] = edge.second; dfs(edge.first); } out[u] = id; } int next(int u) { return w[idx[u]] == 1 ? p[u] = next(p[u]) : u; } void toLCA(int u, int v, llint& c) { if (!u || !c) return; if (in[u] <= in[v] && out[u] >= out[v]) return; c /= w[idx[u]]; toLCA(p[u] = next(p[u]), v, c); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); llint c; int n, m, u, v; cin >> n >> m; for (int i = 1; i < n; ++i) { cin >> u >> v >> w[i]; edges[u].emplace_back(v, i); edges[v].emplace_back(u, i); } dfs(1); while (m--) { cin >> u; if (u == 1) { cin >> u >> v >> c; toLCA(u, v, c); toLCA(v, u, c); cout << c << n ; } else { cin >> u >> c; w[u] = c; } } } |
#include <bits/stdc++.h> using namespace std; using ll = long long; ll BASE_NUM = 998244353; class UnionFindTree { map<ll, ll> union_tree_data; public: UnionFindTree() {} UnionFindTree(vector<ll> verticals) { for (auto v : verticals) { union_tree_data[v] = v; } } void add_vertical(ll v) { union_tree_data[v] = v; } void reset() { for (auto &pair : union_tree_data) { pair.second = pair.first; } } long long find(long long N) { if (union_tree_data[N] == N) { return N; } else { return union_tree_data[N] = find(union_tree_data[N]); } } bool same(long long x, long long y) { return find(x) == find(y); } void union_tree(long long x, long long y) { x = find(x); y = find(y); if (x == y) return; union_tree_data[x] = y; } }; class edge { public: ll from, to, cost; bool operator<(const edge &another) const { return cost < another.cost; }; bool operator>(const edge &another) const { return cost > another.cost; }; }; class MST { UnionFindTree uft; public: priority_queue<edge, vector<edge>, greater<edge>> que; vector<edge> result; ll cost = 0; MST(vector<ll> &nodes, vector<edge> &edges) : uft(nodes) { for (auto e : edges) { que.push(e); } while (!que.empty()) { edge e; e = que.top(); if (!uft.same(e.from, e.to)) { uft.union_tree(e.from, e.to); result.push_back(e); cost += e.cost; } que.pop(); } } }; int main() { ll n; vector<pair<ll, ll>> towns; vector<ll> costs; vector<ll> ks; vector<edge> edges; cin >> n; for (int i = 0; i < n; i++) { pair<ll, ll> p; cin >> p.first >> p.second; towns.push_back(p); } for (int i = 0; i < n; i++) { ll c; cin >> c; costs.push_back(c); } for (int i = 0; i < n; i++) { ll k; cin >> k; ks.push_back(k); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { edges.push_back({i, j, (abs(towns[i].first - towns[j].first) + abs(towns[i].second - towns[j].second)) * (ks[i] + ks[j])}); } } for (int i = 0; i < n; i++) { edges.push_back({i, n, costs[i]}); } vector<ll> nodes(n + 1); for (int i = 0; i < n + 1; i++) { nodes[i] = i; } MST mst(nodes, edges); vector<ll> stations; vector<pair<ll, ll>> connections; for (auto e : mst.result) { if (e.to == n) { stations.push_back(e.from); } else { connections.push_back(make_pair(e.from, e.to)); } } cout << mst.cost << n ; cout << stations.size() << n ; for (int i = 0; i < stations.size(); i++) { if (i == 0) { cout << stations[i] + 1; } else { cout << << stations[i] + 1; } } cout << n ; cout << connections.size() << n ; for (int i = 0; i < connections.size(); i++) { cout << connections[i].first + 1 << << connections[i].second + 1 << n ; } cout << flush; return 0; } |
#include <bits/stdc++.h> #define fi first #define se second #define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++) #define mk make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define IT iterator #define V vector #define TP template <class o> #define TPP template <typename t1, typename t2> #define SZ(a) ((int)a.size()) #define all(a) a.begin(), a.end() #define rep(i, a, b) for (int i = a; i <= b; i++) #define REP(i, a, b) for (int i = b; i >= a; i--) #define FOR(i,n) rep(i,1,n) #define debug(x) cerr << #x << = << x << endl using namespace std; typedef unsigned ui; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; const int N = 3010, size = 1 << 20, mod = 998244353, inf = 2e9; const ll INF = 1e15; // char buf[size],*p1=buf,*p2=buf; TP void qr(o& x) { char c = gc; x = 0; int f = 1; while (!isdigit(c)) { if (c == - ) f = -1; c = gc; } while (isdigit(c)) x = x * 10 + c - 0 , c = gc; x *= f; } template<class o,class ... O> void qr(o& x, O& ... y) {qr(x); qr(y...);} TP void qw(o x) { if (x / 10) qw(x / 10); putchar(x % 10 + 0 ); } TP void pr1(o x) { if (x < 0) x = -x, putchar( - ); qw(x); putchar( ); } template<class o,class ... O> void pr1(o x, O ... y) { pr1(x); pr1(y...);} TP void pr2(o x) { if (x < 0) x = -x, putchar( - ); qw(x); putchar( n ); } template<class o,class ... O> void pr2(o x, O ... y) { pr2(x); pr2(y...);} // math ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll power(ll a, ll b = mod - 2, ll p = mod) { ll c = 1; while (b) { if (b & 1) c = c * a % p; b /= 2; a = a * a % p; } return c; } ll exgcd(ll a,ll b,ll &x,ll &y) { if(!a) {x=0; y=1; return b;} ll d=exgcd(b%a,a,y,x); x -= b/a*y; return d; } ll INV(ll n,ll p=mod) {ll x,y; exgcd(n,p,x,y); return (x%p+p)%p;} TP void cmax(o& x, o y) { if (x < y) x = y; } TP void cmin(o& x, o y) { if (x > y) x = y; } TPP void ad(t1& x, t2 y) { x += y; if (x >= mod) x -= mod; } TPP void dl(t1& x, t2 y) { x -= y; if (x < 0) x += mod; } int n, m, X[N], Y[N]; V<int> e[N], g[N], E, P; bool v[N]; #define DEBUG void dfs(int x) { int i, y; while(SZ(g[x])) { i = g[x].back(); g[x].pop_back(); if(v[i]) continue; y = x ^ X[i] ^ Y[i]; v[i] = 1; dfs(y); } P.pb(x); } void check(int s, int w = 0) { FOR(i, n) g[i] = e[i]; FOR(i, m) v[i] = 0; int cnt = 0; FOR(i, n) if(i ^ s) cnt += SZ(e[i]) & 1; if(cnt > 1) return ; P.clear(); dfs(s); if(SZ(P) + SZ(E) - 1 - (w > 0) != m) return ; P.pb(-1); for(int i: E) if(i ^ w) P.pb(X[i] ^ Y[i] ^ s), P.pb(s); pr2(SZ(P)); for(int i: P) pr1(i); exit(0); } void solve() { qr(n, m); FOR(i, m) qr(X[i], Y[i]); FOR(s, n) { FOR(i, n) e[i].clear(); P.clear(); E.clear(); FOR(i, m) { int x = X[i], y = Y[i]; if(x == s || y == s) continue; e[x].pb(i); e[y].pb(i); } FOR(i, m) { int x = X[i], y = Y[i]; if(x == s || y == s) { int v = s ^ x ^ y; if(SZ(e[v]) & 1) e[s].pb(i), e[v].pb(i); else E.pb(i); } } check(s); for(int i: E) { e[X[i]].pb(i); e[Y[i]].pb(i); check(s, i); e[X[i]].pop_back(); e[Y[i]].pop_back(); } } puts( 0 ); } int main() { int T = 1; // qr(T); while (T--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; bool found = false; long long b, A[10000], n, orig_a; void bfs(long long a, long long k) { if (found) return; if (a > b) return; if (a == b) { found = true; n = k; a = orig_a; cout << YES << endl; cout << n + 1 << endl; cout << a; for (int i = 0; i < n; i++) { if (A[i] == 1) { a = a * 10 + 1; cout << << a; } else if (A[i] == 2) { a = a * 2; cout << << a; } } cout << endl; return; } A[k] = 2; bfs(a * 2, k + 1); A[k] = 1; bfs((a * 10) + 1, k + 1); } int main() { long long a, c, d, i, j, m; string s; cin >> a >> b; orig_a = a; bfs(a, 0); if (!found) cout << NO << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; class Compare { public: bool operator()(pair<long long, long long> a, pair<long long, long long> b) { return (a.first + a.second) > (b.first + b.second); } }; int main() { long long test, x, n, m, k, w, d, f, y; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, Compare> pqw; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, Compare> pqd; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, Compare> pqf; scanf( %lld%lld%lld%lld%lld%lld%lld , &x, &n, &m, &k, &w, &d, &f); for (long long i = 0; i < n; i++) { pqw.push(make_pair(0, w)); } for (long long i = 0; i < m; i++) { pqd.push(make_pair(0, d)); } for (long long i = 0; i < k; i++) { pqf.push(make_pair(0, f)); } long long ans = 0; for (long long i = 0; i < x; i++) { pair<long long, long long> cur = pqw.top(); pqw.pop(); pqw.push(make_pair(cur.first + cur.second, cur.second)); pair<long long, long long> curd = pqd.top(); pqd.pop(); curd.first = max(cur.first + cur.second, curd.first) + d; pqd.push(curd); pair<long long, long long> curf = pqf.top(); pqf.pop(); curf.first = max(curd.first, curf.first) + f; pqf.push(curf); ans = curf.first; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; vector<int> b[200005]; int L[200005], R[200005], u[200005], v[200005], opt[200005], size[200005]; int m, n, ql, qr, upd, tot, now, ans, i, x; struct data { int cnt, sum; data() { cnt = 1; } void multi(int x) { cnt = 1ll * cnt * x % P; sum = 1ll * sum * x % P; } int renew(int x) { cnt = 1; sum = x; return 0; } } a[200005 << 2]; void DFS(int x, int Fa) { L[x] = ++tot; for (int i = 0, y; i < b[x].size(); i++) if ((y = b[x][i]) != Fa) DFS(y, x); R[x] = tot; } int INV(int a) { int ret = 1, b = P - 2; for (; b; b >>= 1, a = 1ll * a * a % P) if (b & 1) ret = 1ll * ret * a % P; return ret; } inline void down(int x) { if (a[x].cnt == 1) return; a[x << 1].multi(a[x].cnt); a[x << 1 | 1].multi(a[x].cnt); a[x].cnt = 1; } inline void up(int x) { a[x].sum = (a[x << 1].sum + a[x << 1 | 1].sum) % P; } void Multi(int x, int l, int r) { if (ql <= l && r <= qr) { a[x].multi(now); return; } int mid = (l + r) >> 1; down(x); if (ql <= mid) Multi(x << 1, l, mid); if (qr > mid) Multi(x << 1 | 1, mid + 1, r); up(x); } int Update(int x, int l, int r, int pos) { if (l == r) return (!upd) ? a[x].sum : a[x].renew(now); int mid = (l + r) >> 1; down(x); int ret = (pos <= mid) ? Update(x << 1, l, mid, pos) : Update(x << 1 | 1, mid + 1, r, pos); return up(x), ret; } void Query(int x, int l, int r) { if (ql <= l && r <= qr) { ans = (ans + a[x].sum) % P; return; } int mid = (l + r) >> 1; down(x); if (ql <= mid) Query(x << 1, l, mid); if (qr > mid) Query(x << 1 | 1, mid + 1, r); } int main() { scanf( %d%d , &v[1], &m); n = 1; for (i = 1; i <= m; i++) { scanf( %d%d , &opt[i], &u[i]); if (opt[i] == 1) scanf( %d , &v[++n]), b[u[i]].push_back(n); } DFS(1, 0); size[n = 1] = 1; upd = 1; now = v[1]; Update(1, 1, tot, L[1]); for (i = 1; i <= m; i++) if (opt[i] == 1) { x = u[i]; size[++n] = 1; ql = L[x]; qr = R[x]; now = 1ll * (size[x] + 1) * INV(size[x]) % P; ++size[x]; Multi(1, 1, tot); upd = 0; now = Update(1, 1, tot, L[x]); now = 1ll * now * INV(v[x]) % P; now = 1ll * now * v[n] % P; upd = 1; Update(1, 1, tot, L[n]); } else { x = u[i]; ans = 0; ql = L[x]; qr = R[x]; Query(1, 1, tot); upd = 0; now = Update(1, 1, tot, L[x]); now = 1ll * now * INV(1ll * v[x] * size[x] % P) % P; ans = 1ll * ans * INV(now) % P; printf( %d n , ans); } } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long double PI = 3.141592653589793; const long long maxn = 100005; long long x, y, z, n, k; void _print(long long t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << { ; _print(p.first); cerr << , ; _print(p.second); cerr << } ; } template <class T> void _print(vector<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T> void _print(set<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T> void _print(multiset<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T, class V> void _print(map<T, V> v) { cerr << [ ; for (auto i : v) { _print(i); cerr << ; } cerr << ] ; } void solve() { string s, t; cin >> s >> t; long long f[26]; memset(f, 0, sizeof(f)); long long n = (long long)s.size(), m = (long long)t.size(); vector<vector<long long>> v(26); for (long long i = 0; i <= n - 1; i++) { f[s[i] - a ]++; v[s[i] - a ].push_back(i); } for (long long i = 0; i <= m - 1; i++) { if (f[t[i] - a ] == 0) { cout << -1 << n ; return; } } long long ans = 1, last = -1; for (long long i = 0; i <= m - 1; i++) { long long prev = last; long long val = t[i] - a ; long long pos = lower_bound(v[val].begin(), v[val].end(), last + 1) - v[val].begin(); if (pos == v[val].size()) { ans++; last = v[val][0]; } else { last = v[val][pos]; } } cout << ans << n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << setprecision(15) << fixed; long long t; t = 1; cin >> t; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> const double eps = 1e-8; const long long INF = 0x3f3f3f3f; const int mod = 20000311; const int maxn = 1e5 + 5; const int N = 1005; using namespace std; int a[maxn] = {0}; int dp[maxn][305] = {0}; int n; int dfs(int p, int k) { if (p > n) return 0; if (dp[p][k] != -1) return dp[p][k]; int num = p + a[p] + k; return dp[p][k] = dfs(num, k) + 1; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int j = 1; j <= 300; j++) { for (int i = n; i >= 1; i--) { int num = i + a[i] + j; if (num > n) dp[i][j] = 1; else dp[i][j] = dp[num][j] + 1; } } int q; scanf( %d , &q); while (q--) { int p, k; scanf( %d %d , &p, &k); if (k >= 300) { int res = 0; while (p <= n) { p = a[p] + k + p; res++; } cout << res << n ; } else cout << dp[p][k] << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; string Max(int n, int s) { string str = ; while (s >= 9 && n > 0) { str += 9 ; s -= 9; n--; } if (n == 0 && s > 0) return -1 ; if (n > 0) { str += char(s + 48); for (int i = 1; i < n; i++) str += 0 ; } return str; } int main() { int n, s; string str = ; cin >> n >> s; if (n * 9 < s) cout << -1 -1 ; else if (n > 1 and s == 0) cout << -1 -1 ; else if (s == 1) { cout << 1; for (int i = 1; i < n; i++) cout << 0; cout << 1 ; for (int i = 1; i < n; i++) cout << 0; } else { for (int i = Max(n, s).size() - 1; i >= 0; i--) str += Max(n, s)[i]; if (str[0] == 0 and str.size() > 1) { str[0] = 1 ; for (int i = 1; i < str.size(); i++) if (str[i] > 0 ) { str[i] = char(int(str[i]) - 1); break; } } cout << str << << Max(n, s); } } |
#include <bits/stdc++.h> using namespace std; inline int max3(int a, int b, int c) { return max(a, max(b, c)); } inline int min3(int a, int b, int c) { return min(a, min(b, c)); } const int INF = 1e9; const long long LINF = 1e18; const double EPS = 1e-6; const double PI = acos(-1.0); const int maxn = 150; int n, m, k; int a[maxn][maxn][maxn]; int ans; int main() { scanf( %d %d %d n , &n, &m, &k); char b[maxn]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { gets(b); for (int p = 0; p < k; ++p) a[i][j][p] = b[p] == 1 ; } if (i < n - 1) gets(b); } ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { for (int p = 0; p < k; ++p) { if (!a[i][j][p]) continue; if ((p > 0 && p < k - 1 && a[i][j][p - 1] && a[i][j][p + 1]) || (p > 0 && j < m - 1 && a[i][j][p - 1] && a[i][j + 1][p] && !a[i][j + 1][p - 1]) || (p > 0 && i < n - 1 && a[i][j][p - 1] && a[i + 1][j][p] && !a[i + 1][j][p - 1]) || (j > 0 && j < m - 1 && a[i][j - 1][p] && a[i][j + 1][p]) || (j > 0 && p < k - 1 && a[i][j - 1][p] && a[i][j][p + 1] && !a[i][j - 1][p + 1]) || (j > 0 && i < n - 1 && a[i][j - 1][p] && a[i + 1][j][p] && !a[i + 1][j - 1][p]) || (i > 0 && i < n - 1 && a[i - 1][j][p] && a[i + 1][j][p]) || (i > 0 && j < m - 1 && a[i - 1][j][p] && a[i][j + 1][p] && !a[i - 1][j + 1][p]) || (i > 0 && p < k - 1 && a[i - 1][j][p] && a[i][j][p + 1] && !a[i - 1][j][p + 1])) ++ans; } } } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; const int MAX_N = 1002; int dp_end[MAX_N][MAX_N]; int dp_not[MAX_N][MAX_N]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { dp_end[0][i] = (i + 1) / 2; dp_not[0][i] = i / 2; } for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { dp_end[i][j] = 1 + dp_not[i - 1][j] + dp_end[i][j - 1]; if (i - 1 >= j) { dp_end[i][j] = max(dp_end[i][j], 1 + dp_not[i][j - 1] + dp_end[i - 1][j]); } dp_end[i][j] %= M; dp_not[i][j] = dp_end[i][j - 1] + dp_end[i - 1][j]; dp_not[i][j] %= M; } } cout << dp_end[n][n] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int32_t main() { c_p_c(); long long n; string s[222222]; map<string, bool> check; cin >> n; for (long long i = 1; i <= n; i++) { cin >> ws; getline(cin, s[i]); } for (long long i = n; i >= 1; i--) { if (!check[s[i]]) { check[s[i]] = 1; cout << s[i] << n ; } } return 0; } |
#include <bits/stdc++.h> using std::max; using std::min; const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x3f3f3f3f3f3f3f3f; __inline__ __attribute__((always_inline)) unsigned int rnd() { static unsigned int seed = 416; return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; } template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void read(_Tp &x) { char ch(getchar()); bool f(false); while (!isdigit(ch)) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); f && (x = -x); } template <typename _Tp, typename... Args> __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { read(t); read(args...); } template <typename _Tp, typename... Args> __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b, const Args &...args) { return a < b ? min(a, args...) : min(b, args...); } template <typename _Tp, typename... Args> __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b, const Args &...args) { return a < b ? max(b, args...) : max(a, args...); } __inline__ __attribute__((always_inline)) int read_str(char *s) { char ch(getchar()); while (ch == || ch == r || ch == n ) ch = getchar(); char *tar = s; *tar = ch, ch = getchar(); while (ch != && ch != r && ch != n && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 100005; const int MAXN = 10000005; bool pr[MAXN]; int _p[MAXN >> 3], _pos; int md[MAXN]; void sieve() { for (int i = 2; i < MAXN; ++i) { if (!pr[i]) { _p[++_pos] = i; md[i] = i; } for (int j = 1; j <= _pos && i * _p[j] < MAXN; ++j) { pr[i * _p[j]] = true; md[i * _p[j]] = _p[j]; if (!(i % _p[j])) break; } } } std::vector<int> p[N], d[N]; int c[MAXN], cnt[1 << 8], mul[1 << 8]; int calc(int id) { int siz = ((int)p[id].size()), ans = 0; for (int i = 0; i < siz; ++i) mul[1 << i] = p[id][i]; for (int i = 1; i < 1 << siz; ++i) { cnt[i] = cnt[i >> 1] + (i & 1); mul[i] = mul[i ^ ((i) & (-(i)))] * mul[((i) & (-(i)))]; if (cnt[i] & 1) ans += c[mul[i]]; else ans -= c[mul[i]]; } return ans; } int a[N]; int _val[11], _cnt[11], pos, cur_id; void dfs(int dep, int val) { if (dep == pos + 1) { d[cur_id].push_back(val); return; } for (int i = 0; i <= _cnt[dep]; ++i) { dfs(dep + 1, val); if (i < _cnt[dep]) val *= _val[dep]; } } bool ban[N], vis[N]; int deg[N]; int f(int mid) { while (ban[mid]) --mid; if (!mid) return false; int qwq = 0; for (int i = 1; i <= mid; ++i) if (!ban[i]) { ++qwq; for (auto it : d[i]) ++c[it]; } int ans = 0; for (int i = 1; i <= mid; ++i) if (!ban[i] && calc(i) != qwq) ++ans; for (int i = 1; i <= mid; ++i) if (!ban[i]) for (auto it : d[i]) --c[it]; return ans; } std::vector<int> getnodes(int mid) { while (ban[mid]) --mid; std::vector<int> nd; int qwq = 0; for (int i = 1; i <= mid; ++i) if (!ban[i]) { ++qwq; for (auto it : d[i]) ++c[it]; } for (int i = 1; i <= mid; ++i) if (!ban[i] && calc(i) != qwq) nd.push_back(i); for (int i = 1; i <= mid; ++i) if (!ban[i]) for (auto it : d[i]) --c[it]; return nd; } int main() { mul[0] = 1; sieve(); int n, k; read(n, k); for (int i = 1; i <= n; ++i) { read(a[i]); ++c[a[i]]; int cur = a[i], last = 0; pos = 0; while (cur > 1) { if (md[cur] != last) p[i].push_back(md[cur]), _val[++pos] = md[cur], _cnt[pos] = 1; else ++_cnt[pos]; last = md[cur]; cur /= last; } cur_id = i; dfs(1, 1); } for (int j = 1; j <= _pos; ++j) for (int i = (MAXN - 1) / _p[j]; i >= 1; --i) c[i] += c[i * _p[j]]; std::vector<int> nd; for (int i = 1; i <= n; ++i) { if ((deg[i] = calc(i)) <= n - 2) { nd.push_back(i); for (int j = 1; j <= n && ((int)nd.size()) < 3; ++j) if (i != j && gcd(a[i], a[j]) == 1) nd.push_back(j); break; } } if (nd.empty()) { std::vector<int> clique, rest; for (int i = 1; i <= n; ++i) if (deg[i] == n) clique.push_back(i); else rest.push_back(i); if (((int)clique.size()) > k) clique.resize(k); for (int i = 1; i <= n && ((int)clique.size()) < k; ++i) { if (!vis[i] && deg[i] == n - 1) { clique.push_back(i); for (auto it : rest) if (gcd(a[it], a[i]) == 1) vis[it] = true; } } for (auto it : clique) printf( %d , it); printf( n ); return 0; } if (k == 3) { for (auto it : nd) printf( %d , it); printf( n ); return 0; } for (auto it : nd) ban[it] = true; for (auto id : nd) for (auto it : d[id]) --c[it]; std::vector<int> clique; for (int i = 1; i <= n; ++i) if (!ban[i] && ((deg[i] = calc(i)) == n - 3)) clique.push_back(i); if (((int)clique.size()) >= k) { clique.resize(k); for (auto it : clique) printf( %d , it); printf( n ); return 0; } memset(c, 0, sizeof(c)); int l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; if (f(mid) + 3 >= k) r = mid; else l = mid + 1; } std::vector<int> v1 = getnodes(l - 1); std::vector<int> v2 = getnodes(l); std::sort(v1.begin(), v1.end()); std::vector<int> qwq; for (auto it : v2) { auto _ = std::lower_bound(v1.begin(), v1.end(), it); if (_ == v1.end() || *_ != it) qwq.push_back(it); } std::sort(qwq.begin(), qwq.end()); if (((int)v1.size()) + 3 + 1 == k && ((int)qwq.size()) != 1) v1.push_back(nd[0]), v1.push_back(nd[1]); else v1.push_back(nd[0]), v1.push_back(nd[1]), v1.push_back(nd[2]); while (((int)v1.size()) < k) { v1.push_back(qwq.back()); qwq.pop_back(); } for (auto it : v1) printf( %d , it); printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; void fact(int n) { const int N = 1e5; vector<int> arr(N, 0); int q = 2; arr[0] = 1; int len = 1; int x = 0; int carry = 0; while (q <= n) { x = 0; carry = 0; while (x < len) { arr[x] = arr[x] * q + carry; carry = arr[x] / 10; arr[x] = arr[x] % 10; x++; } while (carry) { arr[len] = carry % 10; carry = carry / 10; len++; } q++; } len--; for (int i = len; i >= 0; i--) { cout << arr[i]; } return; } long long int binary_exp(long long int x, long long int n) { if (n == 0) { return 1; } else if (n == 1) { return x % 1000000007; } else { long long int temp = binary_exp(x, n / 2); temp = (temp * temp) % 1000000007; if (n % 2 == 0) { return temp; } else { return ((x % 1000000007) * temp) % 1000000007; } } } int main() { int k, r; cin >> k >> r; int i = 1; while (i * k % 10 != r and i * k % 10 != 0) { i++; } cout << i; return 0; } |
#include <bits/stdc++.h> using namespace std; bool vis[100006]; int main() { int n, k; while (cin >> n >> k) { memset(vis, false, sizeof(vis)); int st = 1, ed = 1 + k; int cnt = 0; while (cnt < k && st <= ed) { if (st != ed) vis[st] = vis[ed] = true, cout << st << << ed << ; else vis[st] = true, cout << st << ; st++, ed--; cnt++; } for (int i = 1; i <= n; i++) { if (!vis[i]) cout << i << ; } puts( ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 500023; bool vis[N]; vector<int> adj[N]; void solve() { long long int n, m; cin >> n >> m; map<long long int, long long int> cntA; map<long long int, long long int> cntB; map<long long int, long long int> cntC; map<long long int, long long int> cntD; map<long long int, long long int> cntE; for (long long int i = 0; i < n; i++) { string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == A ) cntA[i]++; else if (s[i] == B ) cntB[i]++; else if (s[i] == C ) cntC[i]++; else if (s[i] == D ) cntD[i]++; else if (s[i] == E ) cntE[i]++; } } long long int ans = 0; for (int i = 0; i < m; i++) { long long int c; cin >> c; long long int maxi = 0; maxi = max(max(cntA[i], cntB[i]), cntC[i]); maxi = max(max(maxi, cntD[i]), cntE[i]); long long int temp = maxi * c; ans += temp; } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; int t = 0; while (t++ < T) { solve(); } } |
#include <bits/stdc++.h> using namespace std; int n, x; int cnt[100], b[105][105], sol[105][105], c[105][105], a[105][105]; inline void Mult_Mat(int a[][105], int b[][105]) { int i, j, k, s; for (i = 0; i < x; ++i) for (j = 0; j < x; ++j) { s = 0; for (k = 0; k < x; ++k) s = (1LL * s + 1LL * a[i][k] * b[k][j]) % 1000000007; c[i][j] = s; } for (i = 0; i < x; ++i) for (j = 0; j < x; ++j) a[i][j] = c[i][j]; } inline void Pow_Log(int a[][105], int p) { int i, j; for (i = 0; i < x; ++i) sol[i][i] = 1; while (p) { if (p & 1) { --p; Mult_Mat(sol, a); } p >>= 1; Mult_Mat(a, a); } for (i = 0; i < x; ++i) for (j = 0; j < x; ++j) a[i][j] = sol[i][j]; } int main() { int i, j, B, k, xx; cin.sync_with_stdio(0); cin >> n >> B >> k >> x; for (i = 1; i <= n; ++i) { cin >> xx; ++cnt[xx]; } for (i = 0; i < x; ++i) for (j = 0; j < 10; ++j) b[i][(i * 10 + j) % x] = (b[i][(i * 10 + j) % x] + cnt[j]) % 1000000007; Pow_Log(b, B); a[1][0] = 1; Mult_Mat(a, b); cout << a[1][k] << n ; return 0; } |
#include<bits/stdc++.h> #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL); #define int long long #define ull unsigned long long #define FOR(i,a,b) for(int i=a;i<b;i++) #define all(x) (x).begin(), (x).end() #define pb push_back #define mp make_pair #define in insert #define ff first #define ss second #define endl n #define pi pair<int,int> #define deb1(a) cout<<a<< n ; #define deb2(a,b) cout<<a<< <<b<< n ; #define deb3(a,b,c) cout<<a<< <<b<< <<c<< n ; #define debp(a) cout<<a.ff<< <<a.ss<< n ; #define debvec(a) for(auto it: a)cout<<it<< ;cout<< ; cout<<endl; #define debvp(a) for(auto it: a)cout<<it.ff<< <<it.ss<<endl; #define debmap(a) for(auto it: a)cout<< { <<it.ff<< , <<it.ss<< }, ;newl; #define debarr(a,n) FOR(i,0,n)cout<<a[i]<< ;cout<<endl; #define inf ((int)1<<60) #define mod 1000000007 using namespace std; //int,char,float,double,bool,string,break,continue,endl,if,else,set,multiset,multimap,unordered_map,unordered_set,cout,cin,printf,scanf //strlen,strcmp,compar e,auto,while,return,true,false,sizeof,size,abs,INT_MAX,INT_MIN,itr,it,sort,YES,NO signed main() { #ifndef ONLINE_JUDGE freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif fast_io int test, i, j; // cin >> test; test = 1; while (test--) { int n, m; cin >> n >> m; string s, t; cin >> s >> t; int a[m], b[m]; j = 0; for (int i = 0; i < n; ++i) { if (s[i] == t[j]) { a[j] = i; j++; } } j = m - 1; for (int i = n - 1; i >= 0 ; i--) { if (s[i] == t[j]) { b[j] = i; j--; } } int ans = 0; for (int i = 0; i < m - 1; ++i) { ans = max(ans, b[i + 1] - a[i]); } cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; const int N = 110; const int mo = 1e9 + 7; int n, k, a[N], num[3], nn, f = 0; long long ans, now[N][N], dp[N][N], aa[N]; long long po(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; long long c = po(a, b / 2); if (b & 1) return c * c % mo * a % mo; else return c * c % mo; } long long tmp[N][N]; void multi(long long a[][N], long long b[][N], int n) { memset(tmp, 0, sizeof tmp); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j] % mo) % mo; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) a[i][j] = tmp[i][j]; } long long res[N][N]; void Pow(long long a[][N], long long n) { memset(res, 0, sizeof res); for (int i = 0; i < num[0] + 1; i++) res[i][i] = 1; while (n) { if (n & 1) multi(res, a, num[0] + 1); multi(a, a, num[0] + 1); n >>= 1; } } int main() { scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) scanf( %d , &a[i]), num[a[i]]++; if (num[0] < num[1]) { for (int i = 1; i <= num[0]; i++) if (!a[i]) nn++; } else { swap(num[0], num[1]); for (int i = n; i >= n - num[0] + 1; i--) if (a[i]) nn++; } for (int j = 0; j <= num[0]; j++) { now[j][j] += j * (num[0] - j); now[j][j] += (num[0] - j) * (num[1] - num[0] + j); now[j][j] += num[0] * (num[0] - 1) / 2ll; now[j][j] += num[1] * (num[1] - 1) / 2ll; if (j + 1 <= num[0]) now[j][j + 1] = (j + 1) * (num[1] - (num[0] - j - 1)); if (j) now[j][j - 1] = (num[0] - j + 1) * (num[0] - j + 1); } Pow(now, k); for (int i = 0; i <= num[0]; i++) ans = (ans + res[i][nn]) % mo; printf( %lld n , res[num[0]][nn] * po(ans, mo - 2) % mo); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MX = 1000000000; int n, m, x, i, j, a[5050], f[5050]; int main() { scanf( %d%d , &n, &m); for (i = 3; i <= n; i++) f[i] = f[i - 1] + (i - 1) / 2; if (f[n] < m) { puts( -1 ); return 0; } for (i = 3; i <= n; i++) if (f[i] > m) break; m -= f[--i]; for (j = 1; j <= i; j++) a[j] = j; if (m > 0) { for (j = 1; j < i; j++) { x = (i + j - 1) / 2 - j + 1; if (x == m) { a[i + 1] = i + j; break; } } if (j >= i) { puts( -1~ ); return 0; } ++i; } x = a[i] + 1; for (j = 2; j <= i; j++) if (a[j] != a[j - 1] + 1) { x = a[j - 1] + 1; break; } for (++i; i <= n; ++i) { a[i] = a[i - 1] + x; if (a[i] > MX) break; } if (i <= n) { puts( -1# ); return 0; } for (i = 1; i <= n; i++) printf( %d , a[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int INF = (int)1e9 + 7; const long long linf = (long long)1e16 + 7; struct pnt { double x, y; pnt(double x, double y) : y(y), x(x) {} pnt() = default; }; int cmp(double a, double b) { if (abs(a - b) < eps) return 0; if (a > b) return 1; return -1; } int main() { int n; cin >> n; int ans = 0; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; vector<int> cnt0(n + 1); vector<int> cnt1(n + 1); for (int i = 0; i < n; ++i) { if (a[i] == 0) cnt0[i]++; if (i) cnt0[i] += cnt0[i - 1]; } for (int i = n - 1; i >= 0; --i) { if (a[i] == 1) cnt1[i]++; if (i != n - 1) cnt1[i] += cnt1[i + 1]; } for (int i = 0; i < n; ++i) { ans = max(ans, cnt0[i] + cnt1[i + 1]); ans = max(ans, cnt1[i]); } cout << ans; return 0; } |
#include<bits/stdc++.h> using namespace std; #define pf printf #define PF(a) printf( %d n ,(a)) #define PF2(a,b) printf( %d %d n ,(a), (b)) #define PF3(a,b,c) printf( %d %d %d n ,(a), (b), (c)) #define PFL(a) printf( %lld n ,(a)) #define PFL2(a,b) printf( %lld %lld n ,(a), (b)) #define PFL3(a,b,c) printf( %lld %lld %lld n ,(a), (b), (c)) #define get(a) cin>>a #define sf scanf #define SF(a) scanf( %d ,&a) #define SF2(a,b) scanf( %d %d ,&a, &b) #define SF3(a,b,c) scanf( %d %d %d ,&a, &b, &c) #define SFL(a) scanf( %lld ,&a) #define SFL2(a,b) scanf( %lld %lld ,&a, &b) #define SFL3(a,b,c) scanf( %lld %lld %lld ,&a, &b, &c) #define gl(a) getline(cin,(a)) #define gc() getchar() #define pb push_back #define m_p make_pair #define pc() printf( Case %d: ,tt++) #define tc() cout<< Case <<tt++<< : #define dbg(x) cout << #x << -> << x << endl; #define SZ(a) (int)(a).size() #define all(a) a.begin(),a.end() #define FF first #define SS second #define MAX 2134567891 #define MOD 1000000007 #define MM 200005 #define PI 2*acos(0.0) #define cond(n,m) (xx>=0 && xx<(n) && yy>=0 && yy<(m)) #define mem(a) memset((a),0,sizeof (a)) #define SET(a) memset((a),-1,sizeof (a)) #define output freopen( output.txt , w ,stdout); #define input freopen( input.txt , r ,stdin); #define I_O ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0) #define rep(a) for(int i=0;i<(a);i++) #define FOR(a,b) for(int i=(a);i<(b);i++) #define REP(a) for(int j=0;j<(a);j++) #define rev(a) for(int i=(a);i>=0;i--) #define fr(i,n) for(i=0;i<n;i++) #define fr1(i,n,m) for(i=n;i<m;i++) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1}; int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2}; int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1}; typedef long long ll; typedef unsigned long long llu; typedef priority_queue < int > PQ; typedef pair < ll , ll > pi; typedef pair < ll, pi > pii; typedef vector < int > vi; typedef vector < ll > vl; typedef vector < string > vs; /* //FUNCTION// ll bigmod(ll a, ll b, ll c) { if(b==0)return 1%c;ll x=bigmod(a,b/2,c);x=(x*x)%c; if(b%2==1)x=(x*a)%c;return x; } ll mod_inverse(ll a, ll mod){return bigmod(a,mod-2,mod);} ll poww(ll a, ll b) { if(b==0)return 1;ll x=poww(a,b/2);x=x*x;if(b%2==1)x=(x*a);return x; } ll LCM(ll a, ll b){ return a*b/ __gcd(a,b);} int pr = 100005; vi primelist; bool a[MM]; void seive( ) { int i,j,k=sqrt(pr); a[1]=1; primelist.pb(2); for(i=4;i<=pr;i+=2)a[i]=1; for(i=3;i<=k;i+=2)if(!a[i])for(j=i*i;j<=pr;j+=2*i)a[j]=1; for(i=3;i<=pr;i+=2)if(!a[i])primelist.pb(i); } int phi[MM]; void calculatePhi() { for (int i = 1; i < MM; i++) { phi[i] = i; } for (int p = 2; p < M; p++) { if (phi[p] == p) { // p is a prime for (int k = p; k < M; k += p) { phi[k] -= phi[k] / p; } } } } ll fact_divs( ll n, ll p) { ll cnt=0;while(p<=n){cnt += n/p;n /= p;}return cnt; } struct point{double x, y;}; double area( point A, point B, point C){ return 0.5*(A.x*(B.y-C.y) - A.y*(B.x-C.x) + B.x*C.y - C.x*B.y); } int Set(int N,int pos){return N=N | (1<<pos);} int reset(int N,int pos){return N= N & ~(1<<pos);} bool check(int N,int pos){return (bool)(N & (1<<pos));} //*/ int a[MM]; int main(){ I_O; int t, tt = 1; cin>>t; while(t--){ int n; cin>>n; int id = 0, mn = MAX; for(int i=1; i<=n; i++){ cin>>a[i]; if(a[i] < mn){ mn = a[i]; id = i; } } cout<<n-1<<endl; for(int i=id+1, cnt=1; i<=n; i++, cnt++){ cout<<id<< <<i<< <<a[id]<< <<a[id]+cnt<<endl; } for(int i=id-1, cnt=1; i >=1; i--, cnt++){ cout<<id<< <<i<< <<a[id]<< <<a[id]+cnt<<endl; } } return 0; } /* 2 5 9 6 3 11 15 3 7 5 13 */ |
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; string s; map<char, int> m; for (i = 0; i < n; i++) { cin >> s; m[s[0]]++; } int ans = 0, f1, f2; for (auto it = m.begin(); it != m.end(); it++) { f1 = it->second / 2; f2 = it->second - f1; ans += f1 * (f1 - 1) / 2 + f2 * (f2 - 1) / 2; } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int Read() { int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - 0 , ch = getchar(); return x; } void Write(int x) { if (x < 0) putchar( - ), x = -x; if (x == 0) putchar( 0 ); int stk[55], tp = 0; while (x) stk[++tp] = x % 10, x /= 10; for (int i = tp; i; i--) putchar(stk[i] + 0 ); } int Add(int a, int b) { return (a + b >= 998244353) ? a + b - 998244353 : a + b; } int Dec(int a, int b) { return (a - b < 0) ? a - b + 998244353 : a - b; } int mul(int a, int b) { return 1ll * a * b % 998244353; } unsigned int qpow(unsigned int a, int b) { unsigned int res = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a); return res; } namespace NTT { int sz; unsigned int w[2500005], w_mf[2500005]; int mf(int x) { return (1ll * x << 32) / 998244353; } void init(int n) { for (sz = 2; sz < n; sz <<= 1) ; unsigned int pr = qpow(3, (998244353 - 1) / sz); w[sz / 2] = 1; w_mf[sz / 2] = mf(1); for (int i = 1; i <= sz / 2; i++) w[sz / 2 + i] = mul(w[sz / 2 + i - 1], pr), w_mf[sz / 2 + i] = mf(w[sz / 2 + i]); for (int i = sz / 2 - 1; i; i--) w[i] = w[i << 1], w_mf[i] = w_mf[i << 1]; } void ntt(vector<unsigned int>& A, int L) { for (int d = L >> 1; d; d >>= 1) for (int i = 0; i < L; i += (d << 1)) for (int j = 0; j < d; j++) { unsigned int x = A[i + j] + A[i + d + j]; if (x >= 998244353 * 2) x -= 998244353 * 2; long long t = A[i + j] + 998244353 * 2 - A[i + d + j], q = t * w_mf[d + j] >> 32, y = t * w[d + j] - q * 998244353; A[i + j] = x; A[i + d + j] = y; } for (int i = 0; i < L; i++) if (A[i] >= 998244353) A[i] -= 998244353; } void intt(vector<unsigned int>& A, int L) { for (int d = 1; d < L; d <<= 1) for (int i = 0; i < L; i += (d << 1)) for (int j = 0; j < d; j++) { unsigned int x = A[i + j]; if (x >= 998244353 * 2) x -= 998244353 * 2; long long t = A[i + d + j], q = t * w_mf[d + j] >> 32, y = t * w[d + j] - q * 998244353; A[i + j] = x + y, A[i + d + j] = x + 2 * 998244353 - y; } int k = __builtin_ctz(L); reverse(A.begin() + 1, A.end()); for (int i = 0; i < L; i++) { long long m = -A[i] & (L - 1); A[i] = (A[i] + m * 998244353) >> k; if (A[i] >= 998244353) A[i] -= 998244353; } } } // namespace NTT struct poly { vector<unsigned int> a; poly(int d = 0, int t = 0) { a.resize(d + 1); a[d] = t; } int deg() { return a.size() - 1; } unsigned int& operator[](const int& b) { return a[b]; } poly extend(int x) { poly c = *this; c.a.resize(x + 1); return c; } void resize(int x) { a.resize(x); } int size() { return a.size(); } void Get(int n) { a.resize(n + 1); for (int i = 0; i <= n; i++) a[i] = Read(); } void Print(int n) { for (int i = 0; i <= n; i++) Write(a[i]), putchar( ); puts( ); } friend poly operator*(poly A, poly B) { int n = A.deg() + B.deg() + 1, lim = 2; for (; lim < n; lim <<= 1) ; NTT::init(lim); A.resize(lim); B.resize(lim); NTT::ntt(A.a, lim); NTT::ntt(B.a, lim); for (int i = 0; i < lim; i++) A[i] = mul(A[i], B[i]); NTT::intt(A.a, lim); return A.extend(n - 1); } poly inv(poly A) { int n = A.a.size(), lim = 2; if (n == 1) return A[0] = qpow(A[0], 998244353 - 2), A; poly B = A; B.resize((n + 1) >> 1); B = inv(B); for (; lim < (n << 1); lim <<= 1) ; NTT::init(lim); A.resize(lim), B.resize(lim), NTT::ntt(A.a, lim); NTT::ntt(B.a, lim); for (int i = 0; i < lim; i++) A[i] = mul(Dec(2, mul(A[i], B[i])), B[i]); NTT::intt(A.a, lim); return A.extend(n - 1); } poly getinv() { return inv(*this); } poly getdao() { poly A = *this, B; int n = A.a.size(); B.resize(n); for (int i = 1; i < n; i++) B[i - 1] = mul(A[i], i); B[n - 1] = 0; return B; } poly getjf() { poly A = *this, B; int n = A.a.size(); B.resize(n); for (int i = 1; i < n; i++) B[i] = mul(A[i - 1], qpow(i, 998244353 - 2)); B[0] = 0; return B; } poly getln() { poly A = *this; return (A.getdao() * A.getinv()).extend(A.deg()).getjf(); } poly exp(poly A) { int n = A.a.size(); if (n == 1) return A[0] = 1, A; poly B = A; B.resize((n + 1) >> 1); B = exp(B); poly C = B.getln(); C.resize(n); for (int i = 0; i < n; i++) C[i] = Dec(A[i], C[i]); ++C[0]; return (B * C).extend(n - 1); } poly getexp() { return exp(*this); } poly getmi(int k) { poly A = *this; int n = A.a.size(); poly B = A.getln(); for (int i = 0; i < n; i++) B[i] = mul(B[i], k); return B.getexp(); } poly sqrt(poly A) { int n = A.size(), lim = 2; if (n == 1) return A[0] = 1, A; poly B = A; B.resize((n + 1) >> 1); B = sqrt(B); poly C = A.extend(n - 1), D = B.getinv(); for (; lim < (n << 1); lim <<= 1) ; NTT::init(lim); B.resize(lim), C.resize(lim), D.resize(lim), NTT::ntt(B.a, lim); NTT::ntt(C.a, lim); NTT::ntt(D.a, lim); for (int i = 0; i < lim; i++) B[i] = mul(Add(B[i], mul(C[i], D[i])), 499122177); NTT::intt(B.a, lim); return B.extend(n - 1); } poly getsqrt() { return sqrt(*this); } }; signed main() { int n = Read(), k = Read(); poly A, B; A.resize(k * 3), B.resize(k * 3); A[0] = 1; A[1] = 6; A[2] = 1; A = A.getsqrt(); B = A.getinv(); ++A[1]; ++A[0]; for (int i = 0; i <= k; i++) A[i] = 1ll * A[i] * 499122177 % 998244353; A = A.getmi(n + 1); A = (A * B).extend(k * 3); for (int i = 1; i <= k; i++) { if (i <= n) printf( %d , A[i]); else printf( %d , 0); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n, d, a, b; cin >> n >> d >> a >> b; vector<pair<long long, int> > v(n); for (int i = 0; i < n; i++) { long long x, y; cin >> x >> y; v[i].first = x * a + y * b; v[i].second = i + 1; } sort(v.begin(), v.end()); int ans = 0, sum = 0, pos = 0; for (int i = 0; i < n; i++) { if (sum + v[i].first <= d) { sum += v[i].first; ans++; pos = i; } } cout << ans << endl; if (ans) for (int i = 0; i <= pos; i++) cout << v[i].second << ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; long long x, y, p, q; cin >> t; while (t--) { cin >> x >> y >> p >> q; long long l = 0, h = 1e9, ans = -1, mid, a, b; while (l <= h) { mid = (l + h) / 2; a = p * mid; b = q * mid; if (b >= y && a >= x && b - a >= y - x) { ans = b - y; h = mid - 1; } else l = mid + 1; } cout << ans << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int N = 2 * (1e5); const long long int maxo = 1e14; const long long int inf = 1e17; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int x0, y0, ax, ay, bx, by; cin >> x0 >> y0 >> ax >> ay >> bx >> by; long long int xs, ys, t; cin >> xs >> ys >> t; vector<pair<long long int, long long int> > v; v.push_back({x0, y0}); for (long long int i = 0; i < 100; i++) { if (v.back().first > (inf - bx) / (ax)) break; if (v.back().second > (inf - by) / ay) break; long long int x1 = v.back().first * ax + bx; long long int y1 = v.back().second * ay + by; v.push_back({x1, y1}); } long long int n = v.size(); long long int ans = 0; for (long long int l = 0; l < n; l++) { for (long long int r = l; r < n; r++) { long long int d1 = abs(xs - v[l].first) + abs(ys - v[l].second); long long int d2 = abs(xs - v[r].first) + abs(v[r].second - ys); long long int len = abs(v[l].first - v[r].first) + abs(v[l].second - v[r].second); if (d1 + len <= t) { ans = max(ans, r - l + 1); } if (d2 + len <= t) { ans = max(ans, r - l + 1); } } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; void setIO(string name = ) { ios_base::sync_with_stdio(0); cin.tie(0); if (int((name).size())) { freopen((name + .in ).c_str(), r , stdin); freopen((name + .out ).c_str(), w , stdout); } } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long mcm(long long a, long long b) { return (a * b) / gcd(a, b); } bool prime(long long n, long long i) { if (i > sqrt(n)) return true; if (n % i == 0) return false; else return prime(n, i + 1); } struct compii { bool operator()(const pair<int, int> &a, const pair<int, int> &b) { if (a.first == a.second) return a.second < b.second; return a.first > b.first; } }; bool comp(int a, int b) { return a > b; } int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; const int mod = 1e9 + 7; const long long inf = 1e18; bool check(string a, string b) { if (a == rock and b == scissors ) return true; if (a == paper and b == rock ) return true; if (a == scissors and b == paper ) return true; return false; } void solve() { string first, m, second; cin >> first >> m >> second; if (check(first, m) and check(first, second)) cout << F ; else if (check(m, second) and check(m, first)) cout << M ; else if (check(second, first) and check(second, m)) cout << S ; else cout << ? ; } int main() { setIO( ); int t = 1; while (t-- > 0) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s = ; for (int i = 1; i < 1000; i++) { s += to_string(i); } int n; cin >> n; cout << s[n - 1]; return 0; } |
#include <bits/stdc++.h> using namespace std; const int adj[8][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; const long long int LLINF = 9e18; const int INF = 2e9; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = acos(-1); const int SIZE = 100011; int N; int cnt[SIZE]; map<string, int> Mp; string ts[8] = { S , M , L , XL , XXL , XXXL }; string s; struct edge { int v, f, c, u; }; vector<edge> E[SIZE]; int st[SIZE], lvl[SIZE]; void init() { for (int i = 0; i < SIZE; i++) { cnt[i] = 0; E[i].clear(); } } void addedge(int a, int b, int ca, int cb) { edge A = {a, 0, ca, (int)E[a].size()}; edge B = {b, 0, cb, (int)E[b].size()}; E[a].push_back(B); E[b].push_back(A); } bool BFS(int S, int T) { memset(lvl, -1, sizeof(lvl)); queue<int> q; q.push(S); lvl[S] = 0; while (!q.empty()) { int indx = q.front(); q.pop(); for (unsigned int i = 0; i < E[indx].size(); i++) { edge &t = E[indx][i]; int idx = t.v; if (lvl[idx] < 0 && t.c > t.f) { lvl[idx] = lvl[indx] + 1; q.push(idx); } } } return lvl[T] >= 0; } int DFS(int indx, int T, int flow) { if (indx == T) return flow; for (int &i = st[indx]; i < (int)E[indx].size(); i++) { edge &t = E[indx][i]; int idx = t.v; if (lvl[idx] == lvl[indx] + 1 && t.c > t.f) { int cur = min(flow, t.c - t.f); int ret = DFS(idx, T, cur); if (ret > 0) { t.f += ret; E[t.v][t.u].f -= ret; return ret; } } } return 0; } int Dinic(int S, int T) { int ret = 0; while (BFS(S, T)) { memset(st, 0, sizeof(st)); while (int flow = DFS(S, T, INF)) { ret += flow; } } return ret; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int S = 0, T = SIZE - 1, a; init(); for (int i = 1; i <= 6; i++) { cin >> a; Mp[ts[i - 1]] = i; addedge(i, T, 0, a); } cin >> N; for (int i = 1; i <= N; i++) { cin >> s; s += , ; string temp = ; for (unsigned int j = 0; j < s.size(); j++) { if (s[j] == , ) { addedge(i + 6, Mp[temp], 0, 1); temp.clear(); cnt[i + 6]++; } else temp.push_back(s[j]); } addedge(S, i + 6, 0, 1); } int ans = Dinic(S, T); if (ans != N) cout << NO << endl; else { cout << YES << endl; for (int i = 1; i <= N; i++) { edge t1, t2; if (cnt[i + 6] == 1) { t1 = E[i + 6][0]; cout << ts[t1.v - 1] << endl; } else { t1 = E[i + 6][0]; t2 = E[i + 6][1]; if (t1.f == 1) cout << ts[t1.v - 1] << endl; else if (t2.f == 1) cout << ts[t2.v - 1] << endl; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[101]; int b[101]; int storage[200001]; int storage2[200001]; int* value = storage + 100000; int* prevalue = storage2 + 100000; int n, k; int main() { scanf( %d%d , &n, &k); int gtzero = 0, lezero = 0; for (int i = 0; i < n; i++) scanf( %d , &a[i]); for (int i = 0; i < n; i++) scanf( %d , &b[i]), b[i] *= k; for (int i = 0; i < n; i++) { b[i] = a[i] - b[i]; if (b[i] > 0) gtzero += b[i]; else lezero += b[i]; } for (int i = 0; i < sizeof(storage) / sizeof(int); i++) { storage[i] = -19200000; storage2[i] = -19200000; } value[0] = prevalue[0] = 0; for (int i = 0; i < n; i++) { int C = b[i]; for (int j = -10000; j <= 10000; j++) { value[j] = max(prevalue[j], prevalue[j - b[i]] + a[i]); } memcpy(storage2, storage, sizeof(storage)); } if (value[0] > 0) printf( %d n , value[0]); else printf( -1 n ); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int num, a1, a2, b1, b2, c1, c2, d, a, b, c; cin >> num >> a1 >> a2 >> b1 >> b2 >> c1 >> c2; a = a2; b = b2; c = c2; if (a + b + c == num) cout << a << << b << << c; else { d = a + b + c - num; if (c - d >= c1) { c = c - d; d = 0; } else { d = d - c + c1; c = c1; } if (d > 0) if (b - d >= b1) { b = b - d; d = 0; } else { d = d - b + b1; b = b1; } if (d > 0) if (a - d >= a1) { a = a - d; d = 0; } else { d = d - a + a1; a = a1; } cout << a << << b << << c; } } |
#include <bits/stdc++.h> using namespace std; const int N = 500005; const int maxx = 1000005; int n, x, y, q, l, r; int a[N]; struct Tree { int ans, m, tag; } tr[maxx * 6]; void pushdown(int rt) { int lson = rt << 1; int rson = rt << 1 | 1; tr[lson].m += tr[rt].tag; tr[rson].m += tr[rt].tag; tr[lson].tag += tr[rt].tag; tr[rson].tag += tr[rt].tag; tr[rt].tag = 0; } void updata(int rt) { int lson = rt << 1; int rson = rt << 1 | 1; if (tr[lson].m == tr[rson].m) { tr[rt].m = tr[lson].m; tr[rt].ans = tr[lson].ans + tr[rson].ans; } else if (tr[lson].m < tr[rson].m) { tr[rt].m = tr[lson].m; tr[rt].ans = tr[lson].ans; } else { tr[rt].m = tr[rson].m; tr[rt].ans = tr[rson].ans; } } void change(int rt, int l, int r, int cl, int cr, int C) { if (cl <= l && r <= cr) { tr[rt].m += C; tr[rt].tag += C; return; } pushdown(rt); int mid = (l + r) >> 1; if (cl <= mid) change(rt << 1, l, mid, cl, cr, C); if (cr > mid) change(rt << 1 | 1, mid + 1, r, cl, cr, C); updata(rt); } void change2(int rt, int l, int r, int x, int C) { if (l == r) { tr[rt].ans += C; return; } pushdown(rt); int mid = (l + r) >> 1; if (x <= mid) change2(rt << 1, l, mid, x, C); if (x > mid) change2(rt << 1 | 1, mid + 1, r, x, C); updata(rt); } int query(int rt, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) { if (tr[rt].m == 1) return tr[rt].ans; return 0; } pushdown(rt); int mid = (l + r) >> 1, summ = 0; if (ql <= mid) summ += query(rt << 1, l, mid, ql, qr); if (qr > mid) summ += query(rt << 1 | 1, mid + 1, r, ql, qr); updata(rt); return summ; } int main() { scanf( %d %d , &n, &q); for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); a[0] = maxx; a[n + 1] = 0; for (int i = 1; i <= n; ++i) change2(1, 0, maxx, a[i], 1); for (int i = 1; i <= n + 1; ++i) { int l = a[i - 1]; int r = a[i]; if (l == r) continue; if (l > r) swap(l, r); change(1, 0, maxx, l, r - 1, 1); } for (int i = 1; i <= q; ++i) { scanf( %d %d , &x, &y); l = a[x]; r = a[x + 1]; if (l != r) { if (l > r) swap(l, r); change(1, 0, maxx, l, r - 1, -1); } l = a[x - 1]; r = a[x]; if (l != r) { if (l > r) swap(l, r); change(1, 0, maxx, l, r - 1, -1); } change2(1, 0, maxx, a[x], -1); change2(1, 0, maxx, y, 1); a[x] = y; l = a[x]; r = a[x + 1]; if (l != r) { if (l > r) swap(l, r); change(1, 0, maxx, l, r - 1, 1); } l = a[x - 1]; r = a[x]; if (l != r) { if (l > r) swap(l, r); change(1, 0, maxx, l, r - 1, 1); } printf( %d n , query(1, 0, maxx, 1, maxx - 1)); } return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int main() { int h, w, n, q; cin >> h >> w >> n >> q; vector<int> v[h]; for (int i = (int)(0); i < (int)(n); ++i) { int x, y; cin >> x >> y; --x; --y; v[x].push_back(y); } for (int i = (int)(0); i < (int)(h); ++i) sort(v[i].begin(), v[i].end()); vector<ll> ok; ok.push_back(-longinf); ok.push_back(longinf); for (int i = (int)(0); i < (int)(q); ++i) { int x; cin >> x; --x; ok.push_back(x); } sort(ok.begin(), ok.end()); ll pl = 0, pr = 0; ll dp[2][202020]; for (int i = (int)(0); i < (int)(2); ++i) for (int j = (int)(0); j < (int)(202020); ++j) dp[i][j] = longinf; dp[0][1] = dp[1][1] = 0; if (v[0].size()) { pl = v[0][0]; pr = v[0].back(); dp[0][1] = 2 * pr - pl; dp[1][1] = pr; } int maxh = 1; for (int i = (int)(1); i < (int)(h); ++i) { if (v[i].empty()) { dp[0][i + 1] = dp[0][i] + 1; dp[1][i + 1] = dp[1][i] + 1; continue; } int nl = v[i][0], nr = v[i].back(); auto itr = lower_bound(ok.begin(), ok.end(), pl); dp[0][i + 1] = min(dp[0][i + 1], dp[0][i] + abs(*itr - pl) + abs(nr - *itr) + abs(nr - nl) + 1); dp[1][i + 1] = min(dp[1][i + 1], dp[0][i] + abs(*itr - pl) + abs(nl - *itr) + abs(nr - nl) + 1); --itr; dp[0][i + 1] = min(dp[0][i + 1], dp[0][i] + abs(*itr - pl) + abs(nr - *itr) + abs(nr - nl) + 1); dp[1][i + 1] = min(dp[1][i + 1], dp[0][i] + abs(*itr - pl) + abs(nl - *itr) + abs(nr - nl) + 1); itr = lower_bound(ok.begin(), ok.end(), pr); dp[0][i + 1] = min(dp[0][i + 1], dp[1][i] + abs(*itr - pr) + abs(nr - *itr) + abs(nr - nl) + 1); dp[1][i + 1] = min(dp[1][i + 1], dp[1][i] + abs(*itr - pr) + abs(nl - *itr) + abs(nr - nl) + 1); --itr; dp[0][i + 1] = min(dp[0][i + 1], dp[1][i] + abs(*itr - pr) + abs(nr - *itr) + abs(nr - nl) + 1); dp[1][i + 1] = min(dp[1][i + 1], dp[1][i] + abs(*itr - pr) + abs(nl - *itr) + abs(nr - nl) + 1); pl = nl; pr = nr; maxh = max(i + 1, maxh); } cout << min(dp[0][maxh], dp[1][maxh]) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; int cnt = 0; cin >> n; string a; for (int i = 0; i < n; i++) { cin >> a; if (a == Tetrahedron ) cnt += 4; else if (a == Cube ) cnt += 6; else if (a == Octahedron ) cnt += 8; else if (a == Dodecahedron ) cnt += 12; else cnt += 20; } cout << cnt; return 0; } |
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /STACK:20000000 ) string problem_name = a ; void init() { freopen((problem_name + .in ).c_str(), rt , stdin); } long long res = 0; int ms[300100]; int len; void mul(int val) { int p = 0; for (int i = 0; i < 300000; i++) { p += ms[i] *= val; ms[i] = p % 10; p /= 10; } } int main() { string s; cin >> s; vector<int> u(255, 0); res = 1; int zero = 0; long long col = 10; int c = 0; int c2 = 0; for (int i = 0; i < int((s).size()); i++) { if (s[i] == ? ) { c++; } else if (isalpha(s[i])) { if (u[s[i]]) continue; u[s[i]] = 1; if (col <= 0) { res = 0; zero = 1; break; } c2++; col--; } } if (zero) { cout << 0; return 0; } if (c) { if (s[0] == ? ) ms[c - 1] = 9; else ms[c] = 1; } else ms[0] = 1; if (c2) { for (int i = 10; i > 10 - c2; i--) { if (i == 10 && isalpha(s[0])) mul(9); else mul(i); } } int len = 300000; while (!ms[len]) len--; for (int i = len; i >= 0; i--) printf( %d , ms[i]); return 0; } |
#include <bits/stdc++.h> const long long mod = 1e9 + 7; using namespace std; const long long N = 102; const long long M = 1e4 + 2; long long dp[N][M], c[N][N], cnt[N]; signed main() { long long n, i, j, k, l, ans = 0, type = 0; cin >> n; dp[0][0] = 1; c[0][0] = 1; for (i = 1; i <= n; i++) { cin >> j; if (!cnt[j]) { type++; } cnt[j]++; for (l = i; l >= 1; l--) { for (k = 10000; k >= j; k--) { dp[l][k] += dp[l - 1][k - j]; if (dp[l][k] > mod) { dp[l][k] -= mod; } } } } if (type <= 2) { cout << n; return 0; } for (i = 1; i <= 100; i++) { for (j = 0; j <= i; j++) { c[j][i] = c[j][i - 1]; if (j) { c[j][i] += c[j - 1][i - 1]; } c[j][i] %= mod; } } for (i = 1; i <= 100; i++) { for (j = 1; j <= cnt[i]; j++) { if (c[j][cnt[i]] == dp[j][i * j]) { ans = max(ans, j); } } } cout << ans; } |
#include <bits/stdc++.h> using namespace std; const int dx[] = {0, 1, -1, 0, 1, -1, -1, 1}; const int dy[] = {-1, 0, 0, 1, 1, 1, -1, -1}; const int N = 0; const int MOD = 0; const int INF = 1e9 + 10; const long long int LLINF = 1e18 + 10; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; int v[10010]; int l, r, x; for (int i = 1; i <= n; i++) { cin >> v[i]; } int aux; bool mudou; int cnt = 0; while (m--) { cin >> l >> r >> x; aux = x - l; cnt = 0; for (int i = l; i <= r; i++) { if (v[i] < v[x]) cnt++; } if (cnt == aux) cout << Yes n ; else cout << No n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int suma = 0, sumg = 0, a, g; char *arr = new char[n]; for (int i = 0; i < n; i++) { scanf( %d %d , &a, &g); if (abs(suma + a - sumg) < abs(sumg + g - suma)) { suma += a; arr[i] = A ; } else { sumg += g; arr[i] = G ; } } if (abs(suma - sumg) > 500) cout << -1 ; else for (int i = 0; i < n; i++) cout << arr[i]; cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; bool isPowerOfTwo(int x) { return (x && !(x & (x - 1))); } bool is_prime(int n) { if (n == 1) return false; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } void solve() { int n; cin >> n; long long int a, b; cin >> a >> b; set<long long int> st; st.insert(a); st.insert(b); for (int i = 2; i * i <= a; i++) { if (a % i == 0) { st.insert(i); st.insert(a / i); } } for (int i = 2; i * i <= b; i++) { if (b % i == 0) { st.insert(i); st.insert(b / i); } } map<pair<long long int, long long int>, long long int> ch; ch[{a, b}]++; for (int i = 1; i < n; i++) { cin >> a >> b; if (ch[{a, b}] >= 1) continue; ch[{a, b}]++; for (auto it = st.begin(); it != st.end();) { long long int k = *it; if (a % k == 0 || b % k == 0) { it++; } else { it = st.erase(it++); } } } if (st.empty()) cout << -1 ; else cout << (*st.begin()); } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; using int64 = long long; int64 bit_count(int64 n) { int64 a{}; while (n) { a += 1; n >>= 1; } return a; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int64 n, m; cin >> n >> m; vector<int64> P(m); for (auto &p : P) cin >> p; auto s = accumulate(begin(P), end(P), 0ll); if (s < n) { cout << -1 << n ; continue; } vector<int64> F(65); for (auto p : P) F[bit_count(p) - 1]++; int64 ans{}; for (int i = 0, j = -1; i < 64; i++) { if (j >= 0 and F[i]) { ans += i - j; j = -1; F[i]--; } if (n & (1 << i)) { if (F[i]) { F[i]--; } else { if (j == -1) j = i; } } F[i + 1] += F[i] / 2; } cout << ans << n ; } } |
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) { if (i == 1) printf( 14 n ); else { if (i == 2) printf( 16 n ); else printf( %lld n , (1LL * i * (i + 1) * (i + 1) - (i - 1))); } } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast,no-stack-protector ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) #pragma GCC target( avx,tune=native ) using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-6; const int mod = 1000000007; inline string getstr(string &s, int l, int r) { string ret = ; for (int i = l; i <= r; i++) ret.push_back(s[i]); return ret; } int modpow(int x, int y, int md = mod) { if (y == 0) return 1; int ret = modpow(x, y >> 1, md); ret = (long long)ret * ret % md; if (y & 1) ret = (long long)ret * x % md; return ret; } int n, m, t, ans; int a[10001]; int cnt[10005]; int in[11]; inline int calc(int use) { int ret = 1; for (int i = 1; i <= use; i++) { ret = (long long)ret * cnt[in[i]] % mod; } return ret; } void dfs(int use, int last, bitset<128> now) { ans += calc(use); if (ans >= mod) ans -= mod; for (int i = last + 1; i <= m / 2; i++) { if (cnt[i]) { if (now[i] || now[m - i]) continue; bitset<128> bs = now | (now >> i) | (now << i) | (now >> (m - i)) | (now << (m - i)); in[use + 1] = i; dfs(use + 1, i, bs); } } } bitset<128> bs; int main() { bs.reset(); bs.set(0); cin >> n >> m >> t; for (int i = 0; i < t; i++) cin >> a[i], cnt[min(m - a[i] % m, a[i] % m)]++; dfs(0, 0, bs); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { int n; string a, b; cin >> n >> a >> b; vector<int> res; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { if (i != 0) res.push_back(i + 1); res.push_back(1); if (i != 0) res.push_back(i + 1); } } cout << res.size() << ; for (int x : res) { cout << x << ; } cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 2 * 1e5 + 5; const int INF = 1e9; const int MOD = 1e9 + 7; int N, K, A[MAX], table[20][MAX]; int main() { cin >> N >> K; for (int i = 0; i < (N); ++i) cin >> A[i]; for (int i = 0; i < (N); ++i) table[0][i] = A[i]; vector<int> b(N, 0); for (int i = 0; i < (N); ++i) { for (int j = 0; j < (19); ++j) { table[j + 1][i] = table[j][i] / 2; b[i]++; if (table[j + 1][i] == 0) break; } } map<int, int> m, cost; int ans = INF; for (int i = 0; i < (20); ++i) { for (int j = 0; j < (N); ++j) { if (b[j] < i) continue; int num = table[i][j]; m[num]++; cost[num] += i; if (m[num] >= K) ans = min(ans, cost[num]); } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int r, g, b, ck, maximum = 0, temp; scanf( %d %d %d , &r, &g, &b); if (r % 2 == 1) r++; r = r / 2; if (g % 2 == 1) g++; g = g / 2; if (b % 2 == 1) b++; b = b / 2; temp = 27 + r * 3; if (temp > maximum) maximum = temp; temp = 28 + g * 3; if (temp > maximum) maximum = temp; temp = 29 + b * 3; if (temp > maximum) maximum = temp; printf( %d n , maximum); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) { for (char a = a ; a <= z ; a++) { string t = ; for (int j = 0; j < i; j++) { t += s[j]; } t += a; for (int j = i; j < s.size(); j++) { t += s[j]; } string r = t; reverse(t.begin(), t.end()); if (r == t) { cout << r; return 0; } } } for (char a = a ; a <= z ; a++) { string t = s; t += a; string r = t; reverse(t.begin(), t.end()); if (r == t) { cout << r; return 0; } } cout << NA ; } |
#include <bits/stdc++.h> using namespace std; inline void output(long long int x) { if (x % 2) cout << x / 2 << n ; else cout << x / 2 - 1 << n ; return; } long long int N, K; int main() { cin >> N >> K; if (N >= K) output(K); else { if ((N << 1) - 1 < K) cout << 0 << n ; else output(N - (K - N) + 2); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 5005; const int mod = 1000000007; int n, a, b, k; int dp[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> a >> b >> k; for (long long i = 1; i <= n; ++i) { dp[0][i] = i; } for (int trip = 1; trip <= k; trip++) { for (int floor = 1; floor <= n; floor++) { int d = abs(b - floor); dp[trip][floor] = dp[trip - 1][min(n, floor + d - 1)] - dp[trip - 1][max(0, floor - d)] - dp[trip - 1][floor] + dp[trip - 1][floor - 1]; while (dp[trip][floor] < 0) dp[trip][floor] += mod; dp[trip][floor] %= mod; dp[trip][floor] = (dp[trip][floor] + dp[trip][floor - 1]) % mod; } } cout << (dp[k][a] - dp[k][a - 1] + mod) % mod; return 0; } |
#include <bits/stdc++.h> using namespace std; int mdc(int a, int b) { return !b ? a : mdc(b, a % b); } int main() { int n, m, x, y, a, b, p, d, x1, y1, x2, y2; cin >> n >> m >> x >> y >> a >> b; d = mdc(a, b); a /= d; b /= d; p = min(n / a, m / b); a *= p; b *= p; if (x + a / 2 > n) { x2 = n; x1 = n - a; } else if (x - a / 2 < 0) { x1 = 0; x2 = a; } else { x2 = x + a / 2; x1 = x2 - a; } if (y + b / 2 > m) { y2 = m; y1 = m - b; } else if (y - b / 2 < 0) { y1 = 0; y2 = b; } else { y2 = y + b / 2; y1 = y2 - b; } cout << x1 << << y1 << << x2 << << y2 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 3e5 + 10; long long pow(long long a, long long b, long long m) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } long long y[5], m[5]; long long h, a, d; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); for (long long i = 1; i <= 3; i++) { cin >> y[i]; } for (long long i = 1; i <= 3; i++) { cin >> m[i]; } cin >> h >> a >> d; long long mn = 1e18; for (long long i = y[1]; i <= 1000; i++) { for (long long j = y[2]; j <= 201; j++) { for (long long k = y[3]; k <= 201; k++) { long long z = (i - y[1]) * h + (j - y[2]) * a + (k - y[3]) * d; if (j <= m[3]) { continue; } else if (k >= m[2]) { mn = min(mn, z); } else { long long sec_to_take_yong = (m[1] / (j - m[3])) + (m[1] % (j - m[3]) != 0); long long sec_to_take_m = (i / (m[2] - k)) + (i % (m[2] - k) != 0); if (sec_to_take_yong < sec_to_take_m) mn = min(mn, z); } } } } cout << mn << endl; } |
#include <bits/stdc++.h> using namespace std; float distance(long long x, long long y, long long a, long long b) { return sqrt((x - y) * (x - y) + (a - b) * (a - b)); } int32_t main() { float n, x1, y1, x2, y2; cin >> n >> x1 >> y1 >> x2 >> y2; if ((x1 == x2 && (x1 == n || x1 == 0)) || (y1 == y2 && (y1 == 0 || y2 == n))) { cout << abs(x1 - x2) + abs(y1 - y2); return 0; } long long path1 = 0, path2 = 0; if (abs(x1 - x2) == n) { path1 = 2 * n - (y1 + y2); path2 = y1 + y2; cout << (path1 > path2 ? path2 : path1) + n; return 0; } if (abs(y1 - y2) == n) { path1 = 2 * n - (x1 + x2); path2 = x1 + x2; cout << (path1 > path2 ? path2 : path1) + n; return 0; } cout << abs(x1 - x2) + abs(y2 - y1); } |
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, l, r, c, ans; cin >> a >> b >> l >> r; c = l % (a + b); if (c > a) { ans = 1; l += (a + b - c + 1); if (l <= r) ans += min(a, r - l + 1); if (a > b) { l += (2 * b); if (l <= r) ans += min(a - b, r - l + 1); } } else { ans = min(a - c + 1, r - l + 1); l += (a - c + 1); if (l <= r) { ans++; l += b; if ((a <= b && a >= r - l + 1) || a > b) ans--; } if (l <= r) ans += min(c - 1, r - l + 1); l += (c - 1); if (a > b) { if ((a - c + 1) > (a - b)) { l += (b - c + 1); if (l <= r) ans += min(a - b, r - l + 1); } else { if (l <= r) ans += min(a - c + 1, r - l + 1); l += (a - c + 1); l += (b + b); if (l <= r) ans += min(c - b - 1, r - l + 1); } } } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int maxn = 1e3 + 10; int n, m, mp[maxn][maxn], cnt, dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; bool vis[maxn][maxn]; vector<int> ans; void dfs(int x, int y) { if (vis[x][y] || x <= 0 || x > n || y <= 0 || y > m) return; vis[x][y] = 1, ++cnt; for (int i = 0; i < 4; ++i) if (!(mp[x][y] & (1 << i))) dfs(x + dx[i], y + dy[i]); } void solve() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) scanf( %d , &mp[i][j]); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (!vis[i][j]) cnt = 0, dfs(i, j), ans.push_back(cnt); sort(ans.begin(), ans.end()); for (int i = ans.size() - 1; ~i; --i) printf( %d , ans[i]); } int main() { solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, t; char v[500010]; vector<pair<int, int> > inter; int memo[500010]; int calcula(int ind) { int res = 0; int x = ind; int ini = 0; int fim = 0; bool erro = false; int lh = -1; inter.clear(); for (int i = 0; i < (n); i++) { if (v[i] == H ) { x--; lh = i; } else if (v[i] == S ) { x++; } if (x == -1 && !erro) { ini = i; erro = true; } if (x == 0 && erro) { if (i < n && v[i + 1] == H ) continue; inter.push_back(make_pair(ini, i)); erro = false; } } if (x < 0) return false; if ((int)inter.size() == 0) { if (lh < t) return true; return false; } int i = 0; res = inter[0].first; fim = max(lh, inter[(int)inter.size() - 1].second); memo[inter.size()] = 0; int next; for (int i = (int)inter.size() - 1; i >= 0; i--) { if (i + 1 < (int)inter.size()) { next = inter[i + 1].first - 1; } else { next = fim; } memo[i] = min((inter[i].second - inter[i].first + 1) * 3 - 2 + memo[i + 1] + (next - inter[i].second), (fim - inter[i].first + 1) * 2 - 1); } res += memo[0]; if (res <= t) return true; return false; } int main() { scanf( %d %d , &n, &t); for (int i = 0; i < (n); i++) { scanf( %c , &v[i]); } int inicio = 0; int fim = n; int res = -1; while (1) { if (inicio > fim) break; int meio = (inicio + fim) / 2; if (calcula(meio)) { res = meio; fim = meio - 1; } else { inicio = meio + 1; } } printf( %d n , res); return 0; } |
#include <bits/stdc++.h> int main() { int t = 1, i, n; scanf( %d , &n); for (i = 1; i <= n - 1; i++) { t = (t + i - 1) % n + 1; printf( %d , t); } return 0; } |
#include <bits/stdc++.h> using namespace std; struct Point { int x, y; Point(int x = 0, int y = 0) : x(x), y(y) {} Point operator-(const Point z) const { return Point(x - z.x, y - z.y); } friend long long Cross(Point a, Point b) { return 1ll * a.x * b.y - 1ll * a.y * b.x; } } a[2010], b[2010]; bool f[2010]; char ch[2010]; int main(int argc, char const *argv[]) { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i].x >> a[i].y; cin >> ch; int p = 0; for (int i = 1; i < n; i++) if (a[i].y < a[p].y || a[i].y == a[p].y && a[i].x < a[p].x) p = i; f[p] = 1; cout << p + 1 << ; for (int i = 0; !i || ch[i - 1]; i++) { int x = -1; for (int j = 0; j < n; j++) if (!f[j]) { if (x == -1) x = j; else if (ch[i] == L ) if (Cross(a[j] - a[p], a[x] - a[p]) > 0) x = j; else ; else if (Cross(a[j] - a[p], a[x] - a[p]) < 0) x = j; } cout << x + 1 << ; f[x] = 1; p = x; } return 0; } |
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef unsigned long long ull; template <typename T> bool umax(T& a, const T& b) { return b <= a ? false : (a = b, true); } template <typename T> bool umin(T& a, const T& b) { return b >= a ? false : (a = b, true); } template <typename T> void V2A(T a[], const vector<T>& b) { for (int i = 0; i < b.size(); i++) a[i] = b[i]; } template <typename T> void A2V(vector<T>& a, const T b[]) { for (int i = 0; i < a.size(); i++) a[i] = b[i]; } const double PI = acos(-1.0); const int INF = 1e9 + 7; const int maxn = 2e3 + 7; bool plot[maxn][maxn]; int r[maxn][maxn]; int n, m, k; int check(int row, int col) { if (col >= 0 && col < m) return true; return false; } int main() { cin >> n >> m >> k; for (int i = 0; i < k; i++) { int x, y; scanf( %d%d , &x, &y); x--; y--; if (!plot[x][y]) { printf( %d %d n , x + 1, y + 1); plot[x][y] = true; continue; } for (int d = 1; d <= 3; d++) { int Max = min(n, x + d + 1); for (int row = max(0, x - d); row < Max; row++) { int t = d - abs(x - row), col1 = y - t, col2 = y + t; if (check(row, col1)) umax(r[x][y], r[row][col1] - d); if (check(row, col2)) umax(r[x][y], r[row][col2] - d); } } for (int d = r[x][y] + 1;; d++) { int Max = min(n, x + d + 1); bool ok = false; for (int row = max(0, x - d); row < Max; row++) { int t = d - abs(x - row), col1 = y - t, col2 = y + t; if (check(row, col1) && !plot[row][col1]) { ok = true; printf( %d %d n , row + 1, col1 + 1); plot[row][col1] = true; break; } if (check(row, col2) && !plot[row][col2]) { ok = true; printf( %d %d n , row + 1, col2 + 1); plot[row][col2] = true; break; } } if (ok) { r[x][y] = d - 1; break; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; struct node { int p, q; } a[1009]; bool cmp(node a, node b) { if (a.p != b.p) return a.p < b.p; if (a.p == b.p) return a.q > b.q; } int main() { int m, n, i; scanf( %d%d , &m, &n); for (i = 1; i <= n; i++) { scanf( %d%d , &a[i].p, &a[i].q); } sort(a + 1, a + 1 + n, cmp); int sum = m; for (i = 1; i <= n; i++) { if (sum > a[i].p) sum += a[i].q; else break; } if (i == n + 1) printf( YES n ); else printf( NO n ); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int len = s.size(); cout << s[0]; int flag = 0; for (int i = 1; i < len; i++) { if (s[i] == d && s[i + 1] == o && s[i + 2] == t && (i + 2 != len - 1)) { cout << . ; i += 2; } else if (s[i] == a && s[i + 1] == t && flag == 0) { cout << @ ; i++; flag = 1; } else cout << s[i]; } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); long long n, k, a, z = 0; cin >> n >> k; vector<int> x(n), c(n); for (int i = 0; i < n; i++) cin >> x[i]; cin >> a; for (int i = 0; i < n; i++) cin >> c[i]; multiset<int> ms; for (int i = 0; i < n; i++) { ms.insert(c[i]); while (k < x[i]) { if (ms.size() == 0) { cout << -1 n ; return 0; } else { k += a; z += *ms.begin(); ms.erase(ms.begin()); } } } cout << z << n ; } |
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 * 2; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; int l = 0, r = n - 1; while (l <= n && s[l] == 0 ) l++; while (r >= 0 && s[r] == 0 ) r--; int ans; if (l > r) ans = n; else ans = max(r + 1, n - l) * 2; cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /stack:200000000 ) #pragma GCC optimize( Ofast ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) #pragma GCC optimize( -ffloat-store ) const long long INF = 0xFFFFFFFFFFFFFFFL; const long long N = 1e9 + 7; vector<long long> pri; void yo(int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p <= n; p++) if (prime[p]) pri.push_back(p); } void solve() { long long n; cin >> n; for (long long i = 1; i < 1001; i++) { if (!binary_search((pri).begin(), (pri).end(), n * i + 1)) { cout << i; return; } } } int32_t main(int32_t argc, char **argv) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); yo(1000003); long long t; t = 1; while (t--) solve(); cerr << n << Time : << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << ms n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf( %d%d , &n, &k); int mat[n + 1][n + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) mat[i][j] = 0; } int br = 0; for (int i = 1; i <= n; i += 2) { for (int j = 1, x = i; j <= n && x <= n; j++, x++) { mat[x][j] = 1; br++; } } for (int i = 3; i <= n; i += 2) { for (int h = 1, j = i; h <= n && j <= n; h++, j++) { mat[h][j] = 1; br++; } } if (br < k) { printf( NO ); return 0; } printf( YES n ); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (k > 0) { if (mat[i][j]) { printf( L ); k--; } else printf( S ); } else printf( S ); } printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a = 1; int b = 2; bool ok = true; int k; for (int i = 0; i < n; i++) { cin >> k; if (k != a && k != b) { ok = false; } else { if (b == 3 && a == 1) { a = min(2, k); b = max(2, k); } else if (b == 2 && a == 1) { a = min(3, k); b = max(3, k); } else if (b == 3 && a == 2) { a = min(1, k); b = max(1, k); } } } if (ok) { cout << YES ; } else { cout << NO ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 5, M = 20; int n, m, a[N], b[M], ans[M]; set<vector<int> > use[M + 1]; void find(int y, vector<int>& current) { if (y == m) { for (int i = 0; i < n; i++) if (a[i] != current[i]) return; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << .* [(bool)(ans[j] & (1 << i))]; cout << endl; } exit(0); } if (use[y].find(current) != use[y].end()) return; for (int i = 0; i < (1 << n); i++) { if ((((((((i << 1) | i) ^ i) >> 1) >> 0)) & 1) + (((((((i << 1) | i) ^ i) >> 1) >> 1)) & 1) + (((((((i << 1) | i) ^ i) >> 1) >> 2)) & 1) + (((((((i << 1) | i) ^ i) >> 1) >> 3)) & 1) + (((((((i << 1) | i) ^ i) >> 1) >> 4)) & 1) != b[y]) continue; ans[y] = i; vector<int> newCur = current; bool flag = true; for (int j = 0; j < n; j++) { if (ans[y] & (1 << j) && (!y || !(ans[y - 1] & (1 << j)))) newCur[j]++; if (newCur[j] > a[j] || newCur[j] < a[j] - ((m - y) / 2)) { flag = false; break; } } if (flag) find(y + 1, newCur); } use[y].insert(current); } int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; vector<int> c(n + 1, 0); find(0, c); } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1100; int n; char s[maxn + 10]; int data[maxn + 10]; int dp[maxn + 10][2]; int main() { cin >> n; getchar(); gets(s); int i; for (i = 0; s[i]; i++) data[i] = s[i] - 0 ; if (i == 1) printf( 0 n ); else { int ans1 = 0, ans2 = 0; for (int j = 0; j < i; j++) { if ((j & 1) != data[j]) ans1++; } for (int j = 0; j < i; j++) { if ((j & 1) == data[j]) ans2++; } printf( %d n , min(ans1, ans2)); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 200005; const int D = 30; struct node { int a, b; }; vector<int> road[N], h[N]; vector<node> q[N]; int n, m, t, fa[N][D + 1], w[N][D + 1], head[N], sum[N], ans[N], g[N]; int cnt, tot, dep[N], id[N], size[N], c[N]; int lowbit(int x) { return x & -x; } void update(int x) { for (int i = x; i <= n; i += lowbit(i)) { c[i] += 1; } } int check(int x) { int tmp = 0; for (int i = x; i >= 1; i -= lowbit(i)) { tmp += c[i]; } return tmp; } struct edge { int to, nxt; } e[N * 2]; void add(int u, int v) { cnt++; e[cnt].nxt = head[u]; e[cnt].to = v; head[u] = cnt; } void dfs1(int u, int deep) { dep[u] = deep; id[u] = ++tot; size[u] = 1; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (v == fa[u][0]) continue; dfs1(v, deep + 1); size[u] += size[v]; } } void dfs2(int u) { g[u] = u; for (int i = 0; i < h[u].size(); i++) { if (dep[h[u][i]] < dep[g[u]]) g[u] = h[u][i]; } for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (v == fa[u][0]) continue; dfs2(v); if (dep[g[v]] < dep[g[u]]) g[u] = g[v]; } w[u][0] = g[u]; } int lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = D; i >= 0; i--) { if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; } if (x == y) return x; for (int i = D; i >= 0; i--) { if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; } return fa[x][0]; } node work(int x, int LCA) { if (x == LCA) return (node){x, 0}; int tmp = 0; for (int i = D; i >= 0; i--) if (dep[w[x][i]] > dep[LCA]) x = w[x][i], tmp += (1 << i); if (w[x][0] == x) return (node){x, -1}; return (node){x, tmp}; } int main() { scanf( %d , &n); for (int i = 2; i <= n; i++) { scanf( %d , &fa[i][0]); add(fa[i][0], i); add(i, fa[i][0]); } dfs1(1, 1); for (int i = 1; i <= D; i++) for (int j = 1; j <= n; j++) { fa[j][i] = fa[fa[j][i - 1]][i - 1]; } scanf( %d , &m); for (int i = 1; i <= m; i++) { int x, y; scanf( %d%d , &x, &y); int c = lca(x, y); if (id[x] > id[y]) swap(x, y); road[id[x]].push_back(id[y]); if (dep[c] < dep[x]) h[x].push_back(c); if (dep[c] < dep[y]) h[y].push_back(c); } dfs2(1); for (int i = 1; i <= n; i++) { } for (int i = 1; i <= D; i++) for (int j = 1; j <= n; j++) { w[j][i] = w[w[j][i - 1]][i - 1]; } scanf( %d , &t); for (int i = 1; i <= t; i++) { int x, y; scanf( %d%d , &x, &y); int c = lca(x, y); node A = work(x, c); node B = work(y, c); if (A.b == -1 || B.b == -1) { ans[i] = -1; continue; } if (x == c || y == c) { ans[i] = A.b + B.b + 1; continue; } ans[i] = A.b + B.b + 2; x = A.a; y = B.a; if (id[x] > id[y]) swap(x, y); q[id[x] - 1].push_back((node){y, -i}); q[id[x] + size[x] - 1].push_back((node){y, i}); } for (int i = 1; i <= n; i++) { for (int j = 0; j < road[i].size(); j++) update(road[i][j]); for (int j = 0; j < q[i].size(); j++) { int num = (q[i][j].b > 0 ? 1 : -1); int x = q[i][j].a; int y = q[i][j].b * num; sum[y] += num * (check(id[x] + size[x] - 1) - check(id[x] - 1)); } } for (int i = 1; i <= t; i++) { printf( %d n , ans[i] - (sum[i] > 0)); } return 0; } |
#include <bits/stdc++.h> using namespace std; struct prog { int i; int cmp; } a[100005]; int ans[100005]; bool cmp(prog a, prog b) { if (a.cmp == b.cmp) return a.i < b.i; else return a.cmp < b.cmp; } int main() { int n, m; while (cin >> n >> m) { priority_queue<prog> q; int i, j; for (i = 0; i < m; i++) { a[i].i = i + 1; a[i].cmp = (int)fabs((m + 1) / 2.0 - i - 1); } sort(a, a + m, cmp); for (i = 0; i < n; i++) { int k = i % m; cout << a[k].i << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxN = 3e5 + 11, maxlg = 2e1; typedef int i_N[maxN]; typedef long long l_N[maxN]; int N, M, Q, par[maxN][maxlg], umax[maxN][maxlg], umin[maxN][maxlg]; vector<int> adj[maxN], pos[maxN]; vector<pair<int, int> > query[maxN]; pair<int, int> edge[maxN]; i_N P, depth, mark, F; l_N cnt, sum, mus, ans; int lg(int x) { for (int i = maxlg; i; i--) if ((1 << i) <= x) return i; return (1 - 1); } int get_root(int u) { if (P[u] < (1 - 1)) return u; return P[u] = get_root(P[u]); } void join(int u, int v) { if (P[u] > P[v]) swap(P[u], P[v]); P[u] += P[v]; P[v] = u; } void dfs(int u) { for (int i = 1, x = lg(depth[u]); i <= x; i++) { par[u][i] = par[par[u][i - 1]][i - 1]; umax[u][i] = max(umax[u][i - 1], umax[par[u][i - 1]][i - 1]); umin[u][i] = min(umin[u][i - 1], umin[par[u][i - 1]][i - 1]); } for (int i = (1 - 1); i < (int)adj[u].size(); i++) { int v = adj[u][i]; if (par[v][(1 - 1)]) continue; par[v][(1 - 1)] = u; umax[v][(1 - 1)] = max(u, v); umin[v][(1 - 1)] = min(u, v); depth[v] = depth[u] + 1; dfs(v); } } void get(int u, int v, int& x, int& y) { if (depth[u] < depth[v]) swap(u, v); for (int i = lg(depth[u]); i >= (1 - 1); i--) if (depth[u] - (1 << i) >= depth[v]) { x = min(x, umin[u][i]); y = max(y, umax[u][i]); u = par[u][i]; } for (int i = lg(depth[u]); i >= (1 - 1); i--) if (par[u][i] != par[v][i]) { x = min(x, min(umin[u][i], umin[v][i])); y = max(y, max(umax[u][i], umax[v][i])); u = par[u][i]; v = par[v][i]; } if (u != v) { x = min(x, min(umin[u][(1 - 1)], umin[v][(1 - 1)])); y = max(y, max(umax[u][(1 - 1)], umax[v][(1 - 1)])); } } void update(long long* bit, int x, int v) { for (int i = x; i <= N; i |= i + 1) bit[i] += v; } long long get_bit(long long* bit, int x) { long long res = (1 - 1); for (int i = x; i >= 1; i = (i & (i + 1)) - 1) res += bit[i]; return res; } long long get_bit(long long* bit, int l, int r) { return get_bit(bit, r) - get_bit(bit, l - 1); } int main() { scanf( %d %d n , &N, &M); for (int i = 1; i <= M; i++) { int u, v; scanf( %d %d n , &u, &v); edge[i] = pair<int, int>(u, v); } memset(P, -1, sizeof(P)); for (int i = 1; i <= M; i++) { int u = edge[i].first, v = edge[i].second; int r1 = get_root(u), r2 = get_root(v); if (r1 != r2) { mark[i] = 1; join(r1, r2); adj[u].push_back(v); adj[v].push_back(u); } } for (int u = 1; u <= N; u++) if (!par[u][(1 - 1)]) { par[u][(1 - 1)] = -1; dfs(u); } for (int i = 1; i <= M; i++) if (!mark[i]) { int u = edge[i].first, v = edge[i].second; int x = 1e9, y = (1 - 1); get(u, v, x, y); F[x] = y; } F[N + 1] = N + 1; for (int i = N; i >= 1; i--) { if (F[i] == (1 - 1)) F[i] = F[i + 1]; else F[i] = min(F[i], F[i + 1]); pos[F[i]].push_back(i); } scanf( %d n , &Q); for (int i = 1; i <= Q; i++) { int l, r; scanf( %d %d n , &l, &r); query[r].push_back(pair<int, int>(l, i)); } for (int i = 1; i <= N; i++) { update(cnt, i, 1); update(sum, i, i - 1); for (int j = (1 - 1); j < (int)pos[i].size(); j++) { int p = pos[i][j]; update(cnt, p, -1); update(sum, p, 1 - p); update(mus, p, i - p); } for (int j = (1 - 1); j < (int)query[i].size(); j++) { int r = i, l = query[i][j].first, id = query[i][j].second; long long C = get_bit(cnt, l, r); long long S = get_bit(sum, l, r); long long T = get_bit(mus, l, r); ans[id] += C * i - S + T; } } for (int i = 1; i <= Q; i++) printf( %I64d n , ans[i]); } |
#include <bits/stdc++.h> using namespace std; template <class T, class L> bool smax(T& x, L y) { return x < y ? (x = y, 1) : 0; } template <class T, class L> bool smin(T& x, L y) { return x > y ? (x = y, 1) : 0; } const int maxn = 16; int n, a[maxn], b[maxn][maxn], ex, x[maxn]; bool mark[maxn]; vector<vector<int> > all; vector<int> todo; int fond(int x) { for (int i = 0; i < n * n; i++) if (!mark[i] && a[i] == x) return i; return -1; } void back(int s = 0) { if (s == n) { for (int i = 0, sum = 0; i < n; i++, sum = 0) { for (int j = 0; j < n; j++) sum += b[j][i]; if (sum != ex) return; } { int sum = 0; for (int i = 0; i < n; i++) sum += b[i][i]; if (sum != ex) return; } { int sum = 0; for (int i = 0; i < n; i++) sum += b[i][n - i - 1]; if (sum != ex) return; } for (int i = 0; i < n; i++, cout << n ) for (int j = 0; j < n; j++) cout << b[i][j] << ; exit(0); } if (s == n - 1) { fill(b[n - 1], b[n - 1] + n, ex); for (int i = 0; i < n - 1; i++) for (int j = 0; j < n; j++) b[n - 1][j] -= b[i][j]; int i = 0; for (int tmp; i < n; i++) if (~(tmp = fond(b[n - 1][i]))) mark[tmp] = 1, todo.push_back(tmp); else break; if (i == n) back(n); for (auto x : todo) mark[x] = 0; todo.clear(); } for (auto v : all) { bool reval = 1; for (auto x : v) if (mark[x]) { reval = 0; break; } if (!reval) continue; do { for (int i = 0; i < n; i++) b[s][i] = a[v[i]], mark[v[i]] = 1; back(s + 1); for (int i = 0; i < n; i++) mark[v[i]] = 0; } while (next_permutation(v.begin(), v.end())); } } void tof(int idx) { if (idx == 4 && x[0] + x[1] + x[2] + x[3] != ex) return; if (idx == 8 && x[4] + x[5] + x[6] + x[7] != ex) return; if (idx == 12 && x[8] + x[9] + x[10] + x[11] != ex) return; if (idx == 16 && x[12] + x[13] + x[14] + x[15] != ex) return; if (idx == 13 && x[0] + x[4] + x[8] + x[12] != ex) return; if (idx == 14 && x[1] + x[5] + x[9] + x[13] != ex) return; if (idx == 15 && x[2] + x[6] + x[10] + x[14] != ex) return; if (idx == 16 && x[3] + x[7] + x[11] + x[15] != ex) return; if (idx == 13 && x[3] + x[6] + x[9] + x[12] != ex) return; if (idx == 16 && x[0] + x[5] + x[10] + x[15] != ex) return; if (idx == 16) { for (int i = 0; i < n; i++, cout << n ) for (int j = 0; j < n; j++) cout << x[i * 4 + j] << ; exit(0); } for (int i = 0; !(i >> 4); ++i) if (!mark[i]) { x[idx] = a[i], mark[i] = 1; tof(idx + 1); mark[i] = 0; } } int main() { srand(time(0)); ios::sync_with_stdio(0), cin.tie(); cin >> n; for (int i = 0; i < n * n; i++) cin >> a[i], ex += a[i]; random_shuffle(a, a + n * n); ex /= n; cout << ex << n ; if (n < 4) { for (int i = 0; i < 1 << n * n; i++) if (__builtin_popcount(i) == n) { int sum = 0; for (int j = 0; j < n * n; j++) sum += (i >> j & 1) * a[j]; if (sum == ex) { all.push_back({}); for (int j = 0; j < n * n; j++) if (i >> j & 1) all.back().push_back(j); sort(all.back().begin(), all.back().end()); } } return back(), 0; } tof(0); return 0; } |
#include <bits/stdc++.h> using namespace std; set<pair<pair<int, int>, pair<int, int> > > s; vector<int> ed[2][300000]; int n, m, k; int a[300000], b[300000]; long long ans[300000]; bool add(int x, int y, int dx, int dy) { pair<pair<int, int>, pair<int, int> > g = make_pair(make_pair(x, y), make_pair(dx, dy)); if (s.find(g) != s.end()) return false; s.insert(g); return true; } void go(int x, int y, int dx, int dy, long long t) { int tt = 1; int d = x - y + m; if (dx - dy != 0) { tt = 0; d = x + y; } bool f = add(x, y, dx, dy); if (!f) return; for (int j = 0; j < ed[tt][d].size(); j++) { int kx = a[ed[tt][d][j]]; int ky = b[ed[tt][d][j]]; kx = kx - x; ky = ky - y; kx = kx / dx; ky = ky / dy; int g = ed[tt][d][j]; if (ans[g] != -1) continue; ans[g] = t + min(kx, ky); } if (dx == 1 && dy == 1) { int v = min(n - x, m - y); x += v; y += v; if (x == n && y == m) return; if (y == m) go(x, y, 1, -1, t + v); else go(x, y, -1, 1, t + v); } if (dx == -1 && dy == -1) { int v = min(x, y); x -= v; y -= v; if (x == 0 && y == 0) return; if (y == 0) go(x, y, -1, 1, t + v); else go(x, y, 1, -1, t + v); } if (dx == 1 && dy == -1) { int v = min(n - x, y); x += v; y -= v; if (x == n && y == 0) return; if (y == 0) go(x, y, 1, 1, t + v); else go(x, y, -1, -1, t + v); } if (dx == -1 && dy == 1) { int v = min(x, m - y); x -= v; y += v; if (x == 0 && y == m) return; if (x == 0) go(x, y, 1, 1, t + v); else go(x, y, -1, -1, t + v); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; for (int i = 1; i <= k; i++) ans[i] = -1; for (int i = 1; i <= k; i++) { cin >> a[i] >> b[i]; ed[0][a[i] + b[i]].push_back(i); ed[1][a[i] - b[i] + m].push_back(i); } go(0, 0, 1, 1, 0); for (int i = 1; i <= k; i++) cout << ans[i] << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; inline long long read() { char ch = getchar(); long long x = 0, f = 1; while (ch < 0 || ch > 9 ) f = ch == - ? -1 : 1, ch = getchar(); while (ch >= 0 && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); return x * f; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin.tie(0); long long base = (1LL << 7) - 1; long long res = 0, cur; printf( ? ); for (long long i = (1); i <= (100); i += (1)) printf( %lld , i); putchar( n ); fflush(stdout); cur = read(); res += (cur >> 7) << 7; printf( ? ); for (long long i = (1); i <= (100); i += (1)) printf( %lld , i << 7); putchar( n ); fflush(stdout); cur = read(); res += cur & base; printf( ! %lld n , res); fflush(stdout); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 205, MAXL = 1 << 20; char qq[MAXL]; char s[MAX][MAX]; pair<int, int> kamo[MAX][MAX]; vector<pair<int, int> > R[MAX][MAX]; int indeg[MAX][MAX]; int cik[MAX][MAX]; int imacik[MAX * MAX][10]; int brred, brstup; int pr[10], ps[10]; int bio[MAX][MAX]; int dokle[MAX][MAX]; int imaq[MAXL][10]; int brc = 1; int Vani(int red, int stup) { return red < 0 || red >= brred || stup < 0 || stup >= brstup; } void Osisaj() { queue<pair<int, int> > Q; for (int i = 0; i < brred; i++) for (int j = 0; j < brstup; j++) if (!indeg[i][j]) Q.push(pair<int, int>(i, j)); for (; !Q.empty();) { pair<int, int> tmp = Q.front(); bio[tmp.first][tmp.second] = 1; Q.pop(); pair<int, int> nn = kamo[tmp.first][tmp.second]; indeg[nn.first][nn.second]--; if (!indeg[nn.first][nn.second]) Q.push(nn); } for (int i = 0; i < brred; i++) { for (int j = 0; j < brstup; j++) { if (!bio[i][j]) { int sz = 0; for (pair<int, int> tmp = pair<int, int>(i, j); tmp != pair<int, int>(i, j) || !sz; sz++, tmp = kamo[tmp.first][tmp.second]) { bio[tmp.first][tmp.second] = 1; cik[tmp.first][tmp.second] = brc; imacik[brc][s[tmp.first][tmp.second] - 0 ] = 1; } brc++; } } } } int Rijesi() { queue<pair<int, int> > Q; memset(dokle, 0, sizeof dokle); int len = (int)strlen(qq); for (int i = 0; i <= len; i++) for (int j = 0; j < 10; j++) imaq[i][j] = 0; for (int i = len - 1; i >= 0; i--) { imaq[i][qq[i] - 0 ] = 1; for (int j = 0; j < 10; j++) imaq[i][j] |= imaq[i + 1][j]; } for (int i = 0; i < brred; i++) for (int j = 0; j < brstup; j++) { indeg[i][j] = (int)R[i][j].size(); if (!indeg[i][j]) Q.push(pair<int, int>(i, j)); } for (; !Q.empty();) { pair<int, int> tmp = Q.front(); Q.pop(); int nd = dokle[tmp.first][tmp.second]; if (s[tmp.first][tmp.second] == qq[nd]) nd++; if (nd == len) return 1; pair<int, int> nn = kamo[tmp.first][tmp.second]; dokle[nn.first][nn.second] = max(dokle[nn.first][nn.second], nd); indeg[nn.first][nn.second]--; if (!indeg[nn.first][nn.second]) Q.push(nn); } for (int i = 0; i < brred; i++) { for (int j = 0; j < brstup; j++) { if (cik[i][j]) { int da = 1; int indc = cik[i][j]; for (int k = 0; k < 10; k++) if (!imacik[indc][k] && imaq[dokle[i][j]][k]) da = 0; if (da) return 1; } } } return 0; } int main() { int brq; scanf( %d%d%d , &brred, &brstup, &brq); for (int i = 0; i < brred; i++) scanf( %s , s[i]); for (int i = 0; i < 10; i++) scanf( %d%d , &pr[i], &ps[i]); for (int i = 0; i < brred; i++) { for (int j = 0; j < brstup; j++) { int nr = i + pr[s[i][j] - 0 ]; int ns = j + ps[s[i][j] - 0 ]; if (Vani(nr, ns)) kamo[i][j] = pair<int, int>(i, j); else kamo[i][j] = pair<int, int>(nr, ns); indeg[kamo[i][j].first][kamo[i][j].second]++; R[kamo[i][j].first][kamo[i][j].second].push_back(pair<int, int>(i, j)); } } Osisaj(); for (int i = 0; i < brq; i++) { scanf( %s , qq); printf( %s n , Rijesi() ? YES : NO ); } return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> struct Segtree { int N; vector<T> seg, lazy; Segtree(int _n) : N(_n) { seg.resize(4 * N, 0); lazy.resize(4 * N, 0); } T join(T x, T y) { return max(x, y); } void push(int x, int s, int e) { if (!lazy[x]) return; seg[x] = max(seg[x], lazy[x]); if (s != e) { lazy[2 * x] = max(lazy[2 * x], lazy[x]); lazy[2 * x + 1] = max(lazy[2 * x + 1], lazy[x]); } lazy[x] = 0; } void build(int x, int s, int e) { if (s == e) { seg[x] = 0; return; } int mid = (s + e) >> 1; build(2 * x, s, mid); build(2 * x + 1, mid + 1, e); seg[x] = join(seg[2 * x], seg[2 * x + 1]); } void update(int x, int s, int e, int l, int r, T val) { push(x, s, e); if (s > r || e < l) return; if (s >= l && e <= r) { lazy[x] = val; push(x, s, e); return; } int mid = (s + e) >> 1; update(2 * x, s, mid, l, r, val); update(2 * x + 1, mid + 1, e, l, r, val); seg[x] = join(seg[2 * x], seg[2 * x + 1]); } T query(int x, int s, int e, int l, int r) { push(x, s, e); if (e < l || s > r) return 0; if (s >= l && e <= r) { return seg[x]; } int mid = (s + e) >> 1; return join(query(2 * x, s, mid, l, r), query(2 * x + 1, mid + 1, e, l, r)); } void update(int l, int r, T val) { update(1, 0, N - 1, l, r, val); } T query(int l, int r) { return query(1, 0, N - 1, l, r); } }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; vector<int> x(q), y(q), d(q), cc(1, 0); for (int i = 0; i < q; ++i) { char c; cin >> x[i] >> y[i] >> c; d[i] = (c == U ); cc.push_back(x[i]); cc.push_back(y[i]); cc.push_back(n + 1 - x[i]); cc.push_back(n + 1 - y[i]); } sort(cc.begin(), cc.end()); cc.erase(unique(cc.begin(), cc.end()), cc.end()); Segtree<int> left(cc.size() + 1), up(cc.size() + 1); for (int i = 0; i < q; ++i) { x[i] = lower_bound(cc.begin(), cc.end(), x[i]) - cc.begin(); y[i] = lower_bound(cc.begin(), cc.end(), y[i]) - cc.begin(); if (d[i]) { int id = up.query(y[i], y[i]); up.update(y[i], y[i], y[i]); cout << cc[y[i]] - cc[id] << n ; id = lower_bound(cc.begin(), cc.end(), n + 1 - cc[id]) - cc.begin(); y[i] = lower_bound(cc.begin(), cc.end(), n + 1 - cc[y[i]]) - cc.begin(); left.update(y[i], id, x[i]); } else { int id = left.query(x[i], x[i]); left.update(x[i], x[i], x[i]); cout << cc[x[i]] - cc[id] << n ; id = lower_bound(cc.begin(), cc.end(), n + 1 - cc[id]) - cc.begin(); x[i] = lower_bound(cc.begin(), cc.end(), n + 1 - cc[x[i]]) - cc.begin(); up.update(x[i], id, y[i]); } } } |
#include <bits/stdc++.h> using namespace std; void addmod(int &a, long long b) { a = (a + b); if (a >= 1000000007) a -= 1000000007; } void mulmod(int &a, long long b) { a = (a * b) % 1000000007; } template <class T> bool domin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <class T> bool domax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } int gi() { int a; scanf( %d , &a); return a; } long long gll() { long long a; scanf( %lld , &a); return a; } int n; namespace treap { struct treap { int val, size, lazy; long long rnd; treap *l, *r; treap() : val(0), size(0), lazy(0), rnd((((long long)rand()) << 32) | rand()), l(NULL), r(NULL) {} treap(int val) : val(val), size(1), lazy(0), rnd((((long long)rand()) << 32) | rand()), l(NULL), r(NULL) {} void push() { if (lazy == 0) return; val += lazy; if (l != NULL) l->lazy += lazy; if (r != NULL) r->lazy += lazy; lazy = 0; } void updateSz() { size = 1; if (l != NULL) size += l->size; if (r != NULL) size += r->size; } }; typedef treap *ptreap; ptreap root = NULL; void split(ptreap tp, int key, ptreap &l, ptreap &r) { if (tp == NULL) { l = NULL, r = NULL; return; } tp->push(); if (tp->val < key) l = tp, split(tp->r, key, tp->r, r); else r = tp, split(tp->l, key, l, tp->l); if (l != NULL) l->updateSz(); if (r != NULL) r->updateSz(); } pair<ptreap, ptreap> split(int v) { ptreap l, r; split(root, v, l, r); return {l, r}; } pair<ptreap, ptreap> split(ptreap tp, int v) { ptreap l, r; split(tp, v, l, r); return {l, r}; } void insert(ptreap &tp, ptreap v) { if (tp == NULL) { tp = v; return; } tp->push(); if (v->rnd < tp->rnd) insert(v->val > tp->val ? tp->r : tp->l, v); else split(tp, v->val, v->l, v->r), tp = v; tp->updateSz(); } void insert(int v) { ptreap tp = new treap(v); insert(root, tp); } void merge(ptreap &t, ptreap l, ptreap r) { if (l != NULL) l->push(); if (r != NULL) r->push(); if (l == NULL || r == NULL) t = (l == NULL ? r : l); else if (l->rnd > r->rnd) t = l, merge(t->r, l->r, r); else t = r, merge(t->l, l, r->l); if (t != NULL) t->updateSz(); } void erase(ptreap &tp, int v) { if (tp == NULL) return; tp->push(); if (v == tp->val) { merge(tp, tp->l, tp->r); } else { erase(v < tp->val ? tp->l : tp->r, v); tp->updateSz(); } } int getmin(ptreap tp) { if (tp == NULL) return 0; tp->push(); return tp->l == NULL ? tp->val : getmin(tp->l); } int getmax(ptreap tp) { if (tp == NULL) return 0; tp->push(); return tp->r == NULL ? tp->val : getmax(tp->r); } void recalc(ptreap tp) { if (tp == NULL) return; tp->push(); recalc(tp->l); recalc(tp->r); tp->updateSz(); } void debug(ptreap &tp) { if (tp == NULL) return; tp->push(); if (tp->l != NULL) debug(tp->l); cout << << tp->val << : << tp->rnd << : << tp->size << endl; if (tp->r != NULL) debug(tp->r); } } // namespace treap int main() { n = gi(); for (int i = 0; i < n; i++) { int l = gi(), r = gi(); auto v = treap::split(l); auto v2 = treap::split(v.second, r); if (v2.first != NULL) v2.first->lazy++; if (v2.second != NULL) { auto v = treap::getmin(v2.second); treap::erase(v2.second, v); } treap::merge(treap::root, v.first, v2.first); treap::merge(treap::root, treap::root, v2.second); treap::insert(l); } cout << treap::root->size << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int l = 1, r = 0; int a[MAXN], b[MAXN], now[MAXN], num[MAXN << 1], cnt[MAXN], ans[MAXN]; map<int, int> ms; struct Change { int p, x, y; } c[MAXN]; struct Query { int l, r, t, id; bool operator<(const Query &A) const { if (b[l] == b[A.l]) return b[r] == b[A.r] ? t < A.t : b[r] < b[A.r]; return b[l] < b[A.l]; } } q[MAXN]; void update(int x, int d) { int &y = num[x]; if (y > 0) cnt[y]--; y += d; if (y > 0) cnt[y]++; } void modify(int p, int d) { if (l <= p && p <= r) { update(a[p], -1); update(d, 1); } a[p] = d; } int get() { for (int i = 1;; i++) { if (!cnt[i]) return i; } } int main() { int n, m, id = 0, Time = 0, sz = 0; scanf( %d%d , &n, &m); int B = 2000; for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); int &t = ms[a[i]]; if (!t) t = ++sz; now[i] = a[i] = t; b[i] = i / B + 1; } while (m--) { int op, x, y; scanf( %d%d%d , &op, &x, &y); if (op == 1) q[++id] = (Query){x, y, Time, id}; else { int &t = ms[y]; if (!t) t = ++sz; c[++Time] = (Change){x, y = t, now[x]}; now[x] = y; } } sort(q + 1, q + id + 1); int v = 0; for (int i = 1; i <= id; i++) { while (v < q[i].t) modify(c[v + 1].p, c[v + 1].x), v++; while (v > q[i].t) modify(c[v].p, c[v].y), v--; while (l < q[i].l) update(a[l], -1), l++; while (l > q[i].l) update(a[l - 1], 1), l--; while (r < q[i].r) update(a[r + 1], 1), r++; while (r > q[i].r) update(a[r], -1), r--; ans[q[i].id] = get(); } for (int i = 1; i <= id; i++) { printf( %d n , ans[i]); } return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.