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.
Forget the individual elements for a second. What does one operation do to the sum of the whole array?
Each operation increases the total sum by exactly , no matter which index you choose.
If the current sum is already larger than , you're cooked: the operation can only increase values, never decrease them.
If the current sum is , after operations the sum must be for some integer .
So the answer is YES exactly when and is divisible by .
Let
be the current sum of the array.
The key observation is that the chosen index does not matter for the total sum. One operation adds to exactly one element, so the total sum increases by exactly .
After doing operations, where , the sum becomes
We need this to equal :
Rearranging gives
So there are only two things to check:
Therefore, the array can become appealing iff
Why this is enough: if those conditions hold, let
This is a nonnegative integer. We can simply apply the operation times to any element, say . The total sum becomes exactly . No need for fancy distribution among elements. The array elements have no upper bound after operations, so stacking all operations onto one index is totally fine.
Edge cases:
YES.NO.Complexity is tiny:
per test case to compute the sum, with
extra 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 n, s, x;
cin >> n >> s >> x;
int sum = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
sum += a;
}
int diff = s - sum;
if (diff >= 0 && diff % x == 0) {
cout << "YES\n";
} else {
cout << "NO\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 n, s, x;
cin >> n >> s >> x;
int sum = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
sum += a;
}
int diff = s - sum;
if (diff >= 0 && diff % x == 0) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}