Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Write out a few examples by hand. The signs go , , , , so the only thing changing is whether a term gets canceled later.
Look at the sequence in chunks of two: . What is the sum of one full chunk?
Every complete pair contributes . So most of the sequence is just disappearing. Very dramatic, very useful.
Since pairs cancel, the actual value of the answer depends only on whether is even or odd.
If is even, all terms form canceling pairs, so the sum is . If is odd, one extra remains, so the sum is .
We have a sequence of length :
It always starts with , then alternates signs.
The important move is to stop thinking about all terms individually. Pair them up:
Each full pair has sum:
So every two terms cancel perfectly. No brute force needed, though brute force would also pass because the constraints are tiny. The math solution is just cleaner.
Case 1: is even
If is even, the whole sequence splits into pairs:
Every pair sums to , so the total sum is:
Example with , :
Case 2: is odd
If is odd, all but one term can be paired away. Since the sequence starts with , the unpaired leftover term is also .
Example with , :
So the answer is:
That is literally the whole problem. The sequence tries to look like a sequence problem, but it is just parity wearing a fake mustache.
Complexity per test case is time and memory.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int x, n;
cin >> x >> n;
cout << (n % 2 == 0 ? 0 : x) << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int x, n;
cin >> x >> n;
cout << (n % 2 == 0 ? 0 : x) << '\n';
}
return 0;
}