The best way to check the condition in the statement is to use bitmasks. Iterate from 0 to 2n−1 and for each number consider its binary representation. If the i-th bit of the representation is set to 0, then decide to perform the -th rotation clockwise. In the opposite case, do it counterclockwise. Finally, check if the whole angle is a multiple of and if so, output "YES". If no good combination of rotations was found, output "NO".
Assistant
i
360
From the problem fragment: ## Editorial
The best way to check the condition in the statement is to use bitmasks. Iterate from 0 to 2n−1 and for each number consider its binary representation. If the i-th bit of the representation is set to 0, then decide to perform the i-th rotation clockwise. In the opposite case, do it counterclockwise. Finally, check if the whole angle is a multiple of 360 and if so, output
Key insight: Checking Rotation Combinations with Bitmasks
Working through the approach: Solution: Checking Rotation Combinations with Bitmasks
Problem Understanding
We have n rotations with angles a1,a2,…,an. Each rotation can be performed either:
Clockwise: contributes −ai to total angle
Counterclockwise: contributes +ai to total angle
We need to determine if some combination of choices results in a total angle that's a multiple of 360°. Detailed Reasoning
Why Bitmasks Work
For n rotations, each has 2 choices (clockwise or counterclockwise), giving 2n total combinations. We can represent each combination as an n-bit number:
Bit i = 0: perform rotation i clockwise
Bit i = 1: perform rotation i counterclockwise
Algorithm Steps
For each mask, calculate total angle by:
If bit i is set → add ai
If bit i is unset → subtract ai
Check if total is divisible by 360
Return "YES" if any combination works, "NO" otherwise
Complexity Analysis
Time: O(n⋅2n) — we iterate through 2n masks, each requiring n operations
Space: O( — storing the angles
Solution Code (C++)
cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[20]; // n is typically small (≤ 15-20) since 2^n grows fast
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// Try all 2^n combinations using bitmasks
for (int mask = 0; mask < (1 << n); mask++) {
int total = 0;
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
// Bit i is 1: rotate counterclockwise (positive)
total += a[i];
} else {
// Bit i is 0: rotate clockwise (negative)
total -= a[i];
}
}
// Check if total angle is multiple of 360°
if (total % 360 == 0) {
cout << "YES" << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
Example 3: angles = [180, 180, 180]
We can achieve: -180 - 180 - 180 = -540 = -1.5 × 360 (not divisible)
Or: +180 + 180 = 360 = 1 × 360 ✓
Output: YES ✓
Key Insight
The bitmask technique elegantly handles the exponential search space.
I should verify this approach against the given examples to make sure it works.
Solution: Checking Rotation Combinations with Bitmasks
Problem Understanding
We have n rotations with angles a1,a2,…,an. Each rotation can be performed either:
Clockwise: contributes −ai to total angle
Counterclockwise: contributes +ai to total angle
We need to determine if some combination of choices results in a total angle that's a multiple of 360°.
Detailed Reasoning
Why Bitmasks Work
For n rotations, each has 2 choices (clockwise or counterclockwise), giving 2n total combinations. We can represent each combination as an n-bit number:
Bit i = 0: perform rotation i clockwise
Bit i = 1: perform rotation i counterclockwise
Algorithm Steps
Enumerate all bitmasks from 0 to 2n−1
For each mask, calculate total angle by:
If bit i is set → add ai
If bit i is unset → subtract
Complexity Analysis
Time: O(n⋅2n) — we iterate through 2n masks, each requiring n operations
Space: O — storing the angles
Solution Code (C++)
cpp
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[20]; // n is typically small (≤ 15-20) since 2^n grows fast
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// Try all 2^n combinations using bitmasks
for (int mask = 0; mask < (1 << n); mask++) {
int total = 0;
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
// Bit i is 1: rotate counterclockwise (positive)
total += a[i];
} else {
// Bit i is 0: rotate clockwise (negative)
total -= a[i];
}
}
// Check if total angle is multiple of 360°
if (total % 360 == 0) {
cout << "YES" << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
Verification with Examples
Example 1: angles = [90, 90]
Mask
Binary
Total Calculation
Sum
% 360
0
00
-90 - 90
-180
≠ 0
1
01
-90 + 90
0
= 0 ✓
2
10
+90 - 90
0
= 0 ✓
3
11
+90 + 90
180
≠ 0
Output: YES ✓
Example 2: angles = [10, 20]
Mask
Binary
Total
% 360
0
00
-10 - 20 = -30
≠ 0
1
01
-10 + 20 = 10
≠ 0
2
10
+10 - 20 = -10
≠ 0
3
11
+10 + 20 = 30
≠ 0
Output: NO ✓
Example 3: angles = [180, 180, 180]
We can achieve: -180 - 180 - 180 = -540 = -1.5 × 360 (not divisible)
Or: +180 + 180 = 360 = 1 × 360 ✓
Output: YES ✓
Key Insight
The bitmask technique elegantly handles the exponential search space. Each of the n bits independently encodes one binary decision, making the code clean and efficient for small n.
n
)
a
i
Check if total is divisible by 360
Return "YES" if any combination works, "NO" otherwise