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.
After doing some number of operations, each element becomes , where is how many times you chose to add to that element.
If you use at most operations, then each can be any integer from to . So the whole problem is: choose so all share a divisor bigger than .
Try forcing every final number to be divisible by the same number. A very convenient target is .
Modulo , we have . So .
Choose . This is always between and , and it makes divisible by .
The operation looks a bit annoying because every operation touches the whole array, but the key is that each element can independently choose whether it gets or .
So after operations, element becomes
where is the number of operations in which we chose to add to that element. Since , if we decide to use at most operations, then we are allowed to choose any
We do not need to output the operations, only the final array. So we just need to pick these values.
The trick
We want all final values to have gcd greater than . The easiest way is to force all of them to be divisible by the same number.
Use the divisor:
Why? Because modulo ,
So:
Now we want this to be , so we need:
The obvious choice is:
And this is perfect because a remainder modulo is always between and , exactly the range we can realize using at most operations. No awkward edge case hiding in the bushes.
Then the final value is:
Check divisibility:
Therefore every output number is divisible by . Since , we have , so the gcd of the whole output array is at least , definitely greater than .
Bounds
The largest added value is at most:
So every output value is at most:
which matches the required output bound. Use long long, because can be around .
Complexity
For each element, we do one modulo and one multiplication.
Time complexity: per test case.
Memory complexity: besides the input/output storage.
#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;
ll k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
ll x = a % (k + 1);
ll ans = a + x * k;
cout << ans << (i + 1 == n ? '\n' : ' ');
}
}
}#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;
ll k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
ll x = a % (k + 1);
ll ans = a + x * k;
cout << ans << (i + 1 == n ? '\n' : ' ');
}
}
}