We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: tt12
Bob will always be able to construct the lexographically largest permutation using the magic spell.
Why?
Let's look at the proof :
By applying the spell on the second index, Bob can generate $$$2$$$ distinct permutations.
Similarly, casting the spell on the third index enables him to create $$$3 * 2 = 3!$$$ different permutations $$$(3$$$ swaps with $$$2$$$ distinct previous permutations.$$$)$$$
Extending this pattern, when the spell is used on the fourth index, Bob can construct $$$4 * 3 * 2 = 4!$$$ permutations $$$(4$$$ swaps with $$$3 * 2$$$ distinct previous permutations.$$$)$$$
In general, by casting the spell on the $$$(n-1)th$$$ index, Bob can generate $$$n!$$$ different permutations, calculated as $$$n * (n-1) * (n-2) * ... * 3 * 2$$$.
Since the number of permutations of size $$$n$$$ are $$$n!$$$, Bob will be able to construct any permutation using the magic spell.
The difference thus can be calculated easily.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
void solve(){
ll n, x;
cin >> n;
for(int i=0; i<n; i++){
cin >> x;
}
cout << (n + 1) / 2 << '\n';
}
int main(){
int t;
cin >> t;
while(t--){
solve();
}
}
Author: C0de_Beast
If we consider the element at an index to be a pointer to respective index, than we can say that the unsorted permutation consists of multiple cycles of varying length and a sorted array will have all cycles of length $$$1$$$ thus our goal is to break all cycles into $$$1$$$ length cycles. Now if we look at the operations in terms how they break the cycle:
- First operation will affect odd and even length cycles differently.
- Odd length cycle will have no affect on its length.
- Even length cycle will break into $$$2$$$ cycles of equal length.
- Second operation will break a cycle of length $$$n(n \gt 1)$$$ into $$$2$$$ cycles of length $$$1$$$ and $$$n-1$$$.
#include "stdio.h"
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
int arr[n];
for(int i=0; i<n; i++) {
scanf("%d", &arr[i]);
}
int count{};
for(int i=0; i<n; i++) {
if(arr[i] == -1) {
continue;
}
int cycle_len{};
int cur_ptr{i};
while(arr[cur_ptr] != -1) {
int temp_ptr{cur_ptr};
cycle_len++;
cur_ptr = arr[cur_ptr]-1;
arr[temp_ptr] = -1;
}
for(int j=16; j>=0; j--) {
if(cycle_len&(1<<j)) {
count += cycle_len^(1<<j);
break;
}
}
}
printf("%d\n", count);
}
return 0;
}
Author: tt12
Let's solve for the case where there is no constraint on consecutive moves. Hence the final path from the source to the destination would consist of only 2 type of moves each from (either $$$L$$$ or $$$R$$$) and (either $$$U$$$ or $$$D$$$). Let's assume these are $$$R$$$ and $$$U$$$ moves, with their respective counts as say, $$$k_1$$$ and $$$k_2$$$. We also understand that any permutation of these set of moves will result in us reaching the destination. Thus, the final answer for this case will be $$$k_1*r + k_2*u$$$.
Now with the condition on consecutive moves, the optimal way of moving would be to move equal number of $$$R$$$ and $$$U$$$ moves in an alternating manner. We would then be left with only one type of move to perform $$$($$$assume $$$k_1 \gt k_2$$$ and hence cost till now $$$= k_2*r + k_2*u)$$$. Therefore, we would have now reached the $$$y$$$ coordinate of the destination.
Finally, to move to the $$$x$$$ coordinate of the destination, we have to perform the remaining $$$(k_1 - k_2)$$$ $$$R$$$ moves. Since these moves are not allowed to be consecutive, we must perform either a $$$U$$$ move or a $$$D$$$ move after every $$$R$$$ move such that the $$$y$$$ coordinate remains unchanged at the end. Hence during these set of moves, $$$|U| = |D|$$$. Hence, $$$(k_1 - k_2) / 2$$$ moves both of the type $$$U$$$ and $$$D$$$ must be performed in between the $$$R$$$ moves.
$$$Final cost = k_2*(r + u) + (k_1 - k_2)*r + (k_1 - k_2)/2 * (u + d)$$$.
To reduce the complexity of solving multiple types of cases, one can rotate the graph by swapping values of $$$x, y, l, r, d$$$ and $$$u$$$ accordingly so as to always solve for a specific type of solution.
Time complexity: $$$\mathcal{O}(1)$$$
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
void solve(){
ll n, m, c, x1, y1, x2, y2, l, r, d, u;
cin >> n >> m >> c >> x1 >> y1 >> x2 >> y2 >> l >> r >> d >> u;
if(x1 > x2){
swap(x1, x2);
swap(l, r);
}
if(y1 > y2){
swap(y1, y2);
swap(u, d);
}
if(y2 - y1 > x2 - x1){
swap(x1, y1);
swap(x2, y2);
swap(u, r);
swap(l, d);
}
ll xdif = x2 - x1;
ll ydif = y2 - y1;
ll ans = ydif * (u + r) + (xdif - ydif) * r + (xdif - ydif) / 2 * (u + d);
cout << max(-1ll, c - ans) << '\n';
}
int main(){
int t;
cin >> t;
while(t--){
solve();
}
}
Author: vrintle
As we have $$$t_i$$$'s only till $$$n \leq 2 \cdot 10^5$$$, therefore we can easily say that the bit count can be upto $$$\log(n) = 18$$$. Let's iterate over every bit count and maximize our answer for each group of nodes.
Let's consider current bit count to be $$$x$$$, so let's mark nodes with bit count $$$x$$$ in red color, and the other nodes in black color. So, this problem boils down to finding maximum distance between any two red colored nodes (a similar codeforces problem). The answer to this sub problem is the diameter of this red-black tree. Note that, in this version, the diameter ends will always be red nodes.
Time complexity: $$$\mathcal{O}(n \cdot \log{n})$$$
PS: I have thought about this partial diameter problem before the actual div3 contest linked above.
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define endl '\n'
#define sz(v) ((int) v.size())
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define pii pair<int, int>
#define inf 2e18
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
const int N = 2e5 + 7;
vi g[N];
void solve() {
int n;
cin >> n;
vi a(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
a[i] = __builtin_popcount(a[i]);
g[i].clear();
}
for(int _ = 0; _ < n — 1; _++) {
int u, v;
cin >> u >> v;
u--; v--;
g[u].push_back(v);
g[v].push_back(u);
}
int ans = 0;
for(int b = 1; b <= 20; b++) {
vi dist(n, inf);
function<void(int, int)> dfs = [&](int u, int p) {
for(auto& v: g[u]) {
if(v == p) continue;
dist[v] = dist[u] + 1;
dfs(v, u);
}
};
for(int i = 0; i < n; i++) {
if(a[i] == b) {
dist[i] = 0;
dfs(i, -1);
break;
}
}
int mx = 0;
for(int i = 0; i < n; i++) {
if(a[i] == b) mx = max(mx, dist[i]);
}
for(int i = 0; i < n; i++) {
if(a[i] == b && dist[i] == mx) {
dist.assign(n, inf);
dist[i] = 0;
dfs(i, -1);
break;
}
}
for(int i = 0; i < n; i++) {
if(a[i] == b) ans = max(ans, dist[i]);
}
}
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef VRINTLE
freopen("./tests/tree_27.txt", "r", stdin);
freopen("./out.txt", "w", stdout);
freopen("./err.txt", "w", stderr);
#endif
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
}
return 0;
}
Author: C0de_Beast
This problem can be solved by applying dp on tree, where we will consider following $$$3$$$ states and use the minimum number of core required in each state for a node to build up and get the final result.
- Case where current node is unpowered.
- Case where current node is powered but does not have a core on it.
- Case where current node have a core on it.
#include "stdio.h"
#include <vector>
using namespace std;
vector<int> dfs(vector<vector<int>>& g, int i, int p) {
vector<int> r(3,0);
int mn{200001};
for(auto& it: g[i]) {
if(it == p) {
continue;
}
vector<int> tp = dfs(g, it, i);
r[0] += tp[1];
r[1] += tp[1];
r[2] += tp[0];
mn = min(mn, tp[2] - tp[1]);
}
if(mn == 200001) {
r[1] += 1;
}else {
r[1] += mn;
}
r[2] += 1;
r[1] = min(r[1], r[2]);
r[0] = min(r[0], r[1]);
return r;
}
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
vector<vector<int>> g(n);
for(int i = 0; i < n-1; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int> r = dfs(g, 0, -1);
printf("%d\n", r[1]);
}
return 0;
}
Author: Gaurav3478
Every number has a unique prime factorization that can be found in $$$\mathcal{O}(\sqrt{n})$$$. We will use this fact to solve this problem efficiently.
For each $$$a_i$$$, find it's prime factorization. Then, find all the possible numbers $$$x$$$, such that $$$\frac{a_i}{x}$$$ is prime. The possible values of $$$x$$$ are simply $$$a_i$$$ divided by each of it's prime factors. This is because, after dividing $$$a_i$$$ by $$$x$$$, only a prime factor would be remaining, which is exactly the task.
For example, the prime factorization of $$$63$$$ is $$$3x3x7$$$. So, the possible values of $$$x$$$ are $$$\frac{63}{3} = 21$$$ and $$$\frac{63}{7} = 9$$$.
Note: It is important to only consider unique prime factors, as there could be overcounting otherwise.
//Solution for Prime Pairings
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vector<int> arr(n);
map<int, int> freq;
for(int i = 0; i < n; i++) {
cin >> arr[i];
freq[arr[i]]++;
}
int ans = 0;
for(int i = 0; i < n; i++) {
vector<int> prime_factors;
int prod = arr[i];
for(int j = 2; j * j <= arr[i]; j++) {
if(arr[i] % j == 0) {
prime_factors.push_back(j);
while(arr[i] % j == 0) {
arr[i] /= j;
}
}
}
if(arr[i] > 1) {
prime_factors.push_back(arr[i]);
arr[i] = 1;
}
for(int j = 0; j < (int)prime_factors.size(); j++) {
prod /= prime_factors[j];
ans += freq[prod];
prod *= prime_factors[j];
}
}
cout << ans << '\n';
}
}
Author: C0de_Beast
This question is entirely based on Josephus Problem.
#include<stdio.h>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while(t--) {
long long n, k, d;
scanf("%lld %lld %lld", &n, &k, &d);
long long c{}, tp{n}, s{}, i{1};
while(tp) {
c++;
tp = tp>>1;
}
long long m = ((n<<1) ^ (i<<c)) | 1;
int r{};
for(int x = 0; x < c - 1; x++) {
if((m & (i<<x)) != (k & (i<<x))) {
s += i<<x;
if(s > d) {
break;
}else {
k += i<<x;
if(k > n) {
break;
}
}
}
r++;
}
printf("%d\n", r);
}
return 0;
}



