Skip to content

Commit 43bfca9

Browse files
Added 6 problems from Codechef's June Long Challenge One 2022
1 parent d2ed5c6 commit 43bfca9

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

Codechef/alternate_additions.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define long long long int
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
11+
int T; cin >> T;
12+
while (T--) {
13+
int A, B;
14+
cin >> A >> B;
15+
16+
long mn = -1LL;
17+
int l = 1, r = 1e9 + 5;
18+
19+
while (l <= r) {
20+
int mid = (l + r) / 2;
21+
22+
int ones = (mid + 1) / 2;
23+
int twos = mid - ones;
24+
25+
if (A + ones + 2LL * twos < B) l = mid + 1;
26+
else r = mid - 1, mn = A + ones + 2LL * twos;
27+
}
28+
29+
bool possible = mn == B;
30+
cout << (possible ? "YES" : "NO") << "\n";
31+
}
32+
}

Codechef/divisible_by_i.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define long long long int
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
11+
int T; cin >> T;
12+
while (T--) {
13+
int N; cin >> N;
14+
15+
int l = 2, r = N;
16+
vector<int> ans(N, 1);
17+
18+
for (int i = 1; i < N; i++)
19+
ans[N - i - 1] = i & 1 ? r-- : l++;
20+
21+
for (int i = 0; i < N; i++) {
22+
if (i) cout << " ";
23+
cout << ans[i];
24+
}
25+
26+
cout << "\n";
27+
}
28+
}

Codechef/equal_strings.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define long long long int
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
11+
int T; cin >> T;
12+
while (T--) {
13+
int N; cin >> N;
14+
15+
string A; cin >> A;
16+
string B; cin >> B;
17+
18+
int ans = 0;
19+
for (char c = 'a'; c <= 'z'; c++) {
20+
bool change = false;
21+
for (int i = 0; i < N && !change; i++)
22+
if (A[i] != c && B[i] == c) change = true;
23+
24+
ans += change;
25+
}
26+
27+
cout << ans << "\n";
28+
}
29+
}

Codechef/full_path_eraser.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define long long long int
6+
7+
vector<int> A, gcds;
8+
vector<vector<int>> tree;
9+
10+
void get_gcds(int node = 0, int parent = -1) {
11+
gcds[node] = A[node];
12+
for (int child: tree[node])
13+
if (child != parent) {
14+
get_gcds(child, node);
15+
gcds[node] = gcd(gcds[node], gcds[child]);
16+
}
17+
}
18+
19+
long solve(int node = 0, int parent = -1, long sum = 0LL) {
20+
long curr = 0LL;
21+
for (int child: tree[node])
22+
if (child != parent)
23+
curr += gcds[child];
24+
25+
long ans = sum + curr;
26+
for (int child: tree[node])
27+
if (child != parent)
28+
ans = max(ans, solve(child, node, sum + curr - gcds[child]));
29+
30+
return ans;
31+
}
32+
33+
int main() {
34+
ios_base::sync_with_stdio(false);
35+
cin.tie(NULL);
36+
37+
int T; cin >> T;
38+
while (T--) {
39+
int N; cin >> N;
40+
41+
A.resize(N);
42+
for (int i = 0; i < N; i++)
43+
cin >> A[i];
44+
45+
tree.assign(N, vector<int>());
46+
for (int i = 1; i < N; i++) {
47+
int u, v;
48+
cin >> u >> v;
49+
50+
tree[u - 1].push_back(v - 1);
51+
tree[v - 1].push_back(u - 1);
52+
}
53+
54+
gcds.resize(N);
55+
get_gcds();
56+
57+
long ans = solve();
58+
cout << ans << "\n";
59+
}
60+
}

Codechef/possible_gcd.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define long long long int
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
11+
int T; cin >> T;
12+
while (T--) {
13+
int A, B;
14+
cin >> A >> B;
15+
16+
if (A < B) swap(A, B);
17+
18+
int ans = 0;
19+
for (int i = 1; i * i <= A - B; i++)
20+
if ((A - B) % i == 0) {
21+
if (i * i < A - B) ans += 2;
22+
else ans++;
23+
}
24+
25+
cout << ans << "\n";
26+
}
27+
}

Codechef/reduce_to_zero.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define long long long int
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
11+
int T; cin >> T;
12+
while (T--) {
13+
long X, Y;
14+
cin >> X >> Y;
15+
16+
if (!X && !Y) cout << "0\n";
17+
else if (!X || !Y) cout << "-1\n";
18+
else {
19+
if (X > Y)
20+
swap(X, Y);
21+
22+
long ans = 0LL;
23+
while (Y - X > X) {
24+
ans++;
25+
X *= 2LL;
26+
27+
if (X > Y)
28+
swap(X, Y);
29+
}
30+
31+
ans += 2LL * X - Y + (X < Y);
32+
ans += 2LL * (Y - X);
33+
34+
cout << ans << "\n";
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)