voidsolve() { string str; cin >> str; for (auto &ch : str) if (ch != '.') cout << ch; }
B.3^A
思路
从高到低枚举 pow(3,i)是否小于等于 n,是则相减,易得构造出来的数列一定在 20 个以内。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13
voidsolve() { int m; cin >> m; vector<int> ans; while (m) for (int i = 10; i >= 0; i--) while (pow(3, i) <= m) m -= pow(3, i),ans.push_back(i); cout << ans.size() << endl; for (auto &v : ans) cout << v << " "; }
structdsu { vector<size_t> pa, size; map<size_t, vector<size_t>> components; explicitdsu(size_t size_) : pa(size_), size(size_, 1) { iota(pa.begin(), pa.end(), 0); for (size_t i = 0; i < size_; ++i) { components[i] = {i}; } } size_tfind(size_t x){ return pa[x] == x ? x : pa[x] = find(pa[x]); } voidunite(size_t x, size_t y) { x = find(x), y = find(y); if (x == y) return; if (size[x] < size[y]) swap(x, y); pa[y] = x; size[x] += size[y]; vector<int> n; sort(all(components[x]), greater<>()); sort(all(components[y]), greater<>()); for (int i = 0; i < min(10, (int)components[x].size()); i++) n.push_back(components[x][i]); for (int i = 0; i < min(10, (int)components[y].size()); i++) n.push_back(components[y][i]); sort(all(n), greater<>()); components[x].clear(); for (int i = 0; i < min((int)n.size(), 10); i++) components[x].push_back(n[i]); components.erase(y); } vector<size_t> connected(size_t x) { return components[find(x)]; } };
voidsolve() { int n, q; cin >> n >> q; dsu d(n + 1); while (q--) { int op, a, b; cin >> op >> a >> b; if (op & 1) d.unite(a, b); else { vector res = d.connected(a); if (res.size() >= b) cout << res[b - 1] << endl; else cout << -1 << endl; } } }