#include<algorithm> #include<cstdio> #include<iostream> #include<map> #include<set> #include<vector> #define all(x) x.begin(), x.end() typedeflonglong ll; typedefstd::pair<int, int> pii; typedefstd::pair<ll, int> pli; typedefstd::pair<ll, ll> pll; typedefdouble db; typedeflongdouble ldb; constint maxN = 2e5 + 5; std::vector<int> edge[maxN]; int n, k, size[maxN]; int tot; inttryCut(int u, int p, int x){ int res = 0; size[u] = 1; for (auto v : edge[u]) { if (v == p) continue; res += tryCut(v, u, x); if (size[v] >= x && tot - size[v] >= x){ res++; tot -= size[v]; } else { size[u] += size[v]; } } return res; } boolcheck(int x){ tot = n; return tryCut(1, 0, x) >= k; } intmain(){ // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); std::ios::sync_with_stdio(false); std::cin.tie(NULL);
int T = 1; std::cin >> T; while (T--) { std::cin >> n >> k; for (int i = 1; i < n; ++i) { int u, v; std::cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); } int l = 1, r = n, ans = 0; while(l <= r) { int mid = (l + r) >> 1; if (check(mid)){ l = mid + 1, ans = std::max(ans, mid); } else { r = mid - 1; } } std::cout << ans << '\n'; for (int i = 1; i <= n; ++i) edge[i].clear(); }
#include<algorithm> #include<cstdio> #include<iostream> #include<map> #include<set> #include<vector> #define all(x) x.begin(), x.end() typedeflonglong ll; typedefstd::pair<int, int> pii; typedefstd::pair<ll, int> pli; typedefstd::pair<ll, ll> pll; typedefdouble db; typedeflongdouble ldb; constint maxn = 2e5 + 5; int n, x; int a[maxn]; intcheck(int x, int mask){ int xorSum = 0, count = 0; for (int i = 1; i <= n; ++i) { xorSum ^= a[i] & mask; if ((xorSum | x) == x) count++; } if ((xorSum | x) == x) return count; elsereturn-1; } intmain(){ // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); std::ios::sync_with_stdio(false); std::cin.tie(NULL);
int T = 1; std::cin >> T; while (T--) { std::cin >> n >> x; for (int i = 1; i <= n; ++i) { std::cin >> a[i]; } int mask = 0; int ans = -1; for (int i = 30; i >= 0; --i) { mask |= 1 << i; if ((1 << i) & x) { ans = std::max(ans, check((x & mask) ^ (1 << i), mask)); } } ans = std::max(ans, check(x & mask, mask)); std::cout << ans << '\n'; }
#include<algorithm> #include<cstdio> #include<iostream> #include<map> #include<set> #include<vector> #define all(x) x.begin(), x.end() typedeflonglong ll; typedefstd::pair<int, int> pii; typedefstd::pair<ll, int> pli; typedefstd::pair<ll, ll> pll; typedefdouble db; typedeflongdouble ldb; const ll mod = 1e9 + 7; constint maxn = 2e5 + 5; ll fac[maxn]; ll quickPow(ll x, ll y){ ll res = 1; while (y) { if (y & 1) res *= x, res %= mod; x *= x, x %= mod; y /= 2; } return res; } ll inv(ll x){ return quickPow(x, mod - 2); } ll C(ll up, ll down){ return fac[down] * inv(fac[up]) % mod * inv(fac[down - up]) % mod; } int m1, m2, n; int p[maxn], s[maxn]; voidsolve(){ if (p[m1] != s[1]) { std::cout << "0\n"; return; } if (p[1] != 1 || s[m2] != n) { std::cout << "0\n"; return; }
ll ans = C(p[m1] - 1, n - 1); for (int i = m1; i > 1; --i) { ans *= C(p[i] - p[i - 1] - 1, p[i] - 2) * fac[p[i] - p[i - 1] - 1] % mod; ans %= mod; }
for (int i = 1; i < m2; ++i) { ans *= C(s[i + 1] - s[i] - 1, n - s[i] - 1) * fac[s[i + 1] - s[i] - 1] % mod; ans %= mod; } std::cout << ans << '\n'; } intmain(){ // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); std::ios::sync_with_stdio(false); std::cin.tie(NULL);
int T = 1; std::cin >> T; fac[0] = 1; for (int i = 1; i < maxn; ++i) fac[i] = fac[i - 1] * i, fac[i] %= mod; while (T--) { std::cin >> n >> m1 >> m2; for (int i = 1; i <= m1; ++i) std::cin >> p[i]; for (int i = 1; i <= m2; ++i) std::cin >> s[i]; solve(); }
return0; }
F.
题目大意:
给定一个大小为 n 的排列,然后给出 q 个询问,每个询问输入两个值,分别是 l 与 r。求出满足如下要求的子序列数量:
#include<algorithm> #include<cstdio> #include<iostream> #include<map> #include<set> #include<vector> #define all(x) x.begin(), x.end() typedeflonglong ll; typedefstd::pair<int, int> pii; typedefstd::pair<ll, int> pli; typedefstd::pair<ll, ll> pll; typedefdouble db; typedeflongdouble ldb; constint maxn = 1e6 + 5; int n, q, a[maxn]; std::vector<pii> seg[maxn]; ll dp[maxn]; int pos[maxn]; ll res[maxn]; structFenwick { ll tree[maxn]; int size; voidreset(int n){ for (int i = 0; i <= n; ++i) tree[i] = 0; size = n; } voidadd(int x, ll val){ while(x <= size) { tree[x] += val; x += lowbit(x); } } ll query(int l, int r){ return sum(r) - sum(l - 1); } ll sum(int x){ ll res = 0; while(x) { res += tree[x]; x -= lowbit(x); } return res; } intlowbit(int x){ return x & (-x); } }spike; voidinitializeDP(int l){ dp[l] = 1; for (int x = a[l]; x <= spike.size; x += a[l]) { if (pos[x] < l) continue; spike.add(pos[x], dp[pos[x]]); for (int y = x * 2; y <= spike.size; y += x) { if (pos[y] < pos[x]) continue; dp[pos[y]] += dp[pos[x]]; } } for (int x = a[l]; x <= spike.size; x += a[l]) { dp[pos[x]] = 0; } } intmain(){ // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); std::ios::sync_with_stdio(false); std::cin.tie(NULL);
int T = 1; std::cin >> T; while (T--) { std::cin >> n >> q; for (int i = 1; i <= n; ++i) { std::cin >> a[i]; pos[a[i]] = i; } spike.reset(n); for(int i = 1; i <= q; ++i) { int l, r; std::cin >> l >> r; seg[l].push_back({r, i}); } for (int l = n; l >= 1; --l) { initializeDP(l); for (auto [r, i] : seg[l]) { res[i] = spike.query(l, r); } } for (int i = 1; i <= q; ++i) { std::cout << res[i] << " "; } std::cout << '\n'; for (int i = 1; i <= n; ++i) pos[i] = 0; for (int i = 1; i <= n; ++i) seg[i].clear(); }