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.
Look at prefix sums. A cut after position only cares about , so equal-sum blocks are really asking for prefix sums sitting on an arithmetic progression.
Let . If a partition has equal blocks, each block has sum . So when , the block sum is forced to be a divisor of ; no mystical Disney magic, just divisibility.
Every step of the prefix sum changes by exactly or . So a walk from to cannot skip integer levels. For every divisor-sized jump, the needed prefix levels are forced to exist.
This gives a lower bound: if , there is at least one valid partition for every positive divisor of . If , the full array already gives the unavoidable lower bound .
Use the array . Its prefix walk goes up once, then down once. On the relevant side of , the divisor levels are hit exactly once, so there are no extra partitions. Therefore the answer is if , and otherwise.
Let
The total sum is
Suppose we split the array into subarrays, all with the same sum . Then
So if , is completely tied to a divisor of . More precisely, is a positive divisor of . That already smells like the answer is going to be a divisor count, because yes, this problem is wearing a constructive mask over a tiny number theory face.
Assume first. Pick any positive divisor of . Since every array element is either or , the prefix sum changes by exactly in absolute value each step. A walk starting at and ending at cannot jump over .
So there exist indices
such that
Then every segment has sum
That is one valid partition for this divisor . Different divisors give different block sums, hence different partition types. Therefore, for every possible ordering,
where is the number of positive divisors of .
For , the same argument works downward using levels , so
If , the full array is always a valid partition, so
That is the lower bound. Now we need to hit it exactly, because otherwise we are just doing math cosplay.
Take the brutally simple array:
If , then , and the prefix sums look like
For a divisor , the internal cut levels are
All of these are strictly less than , and each of them appears exactly once in this prefix walk. The final level is handled by the end of the array. So for this , there is exactly one partition.
No other block sum can appear, because any valid block sum must satisfy . Thus the construction has exactly valid partitions.
For , the same construction works symmetrically: the required negative prefix levels are each hit exactly once while the walk descends.
For , the prefix sums are
The value appears only at the start and the end. Since every valid partition must have block sum , there is no internal place to cut. Only the whole array works, so .
Therefore the minimum is
We output this value modulo , and output the constructed array above.
Counting divisors by trial division costs
per test case, and printing the array costs . Since , this is easily fast enough.
Overall:
time and extra memory.
Reference checked: official Codeforces editorial.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 676767677LL;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll divisors(int n) {
ll res = 0;
for (int d = 1; 1LL * d * d <= n; d++) {
if (n % d == 0) {
res++;
if (d != n / d) res++;
}
}
return res % MOD;
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int x, y;
cin >> x >> y;
int s = x - y;
ll ans = (s == 0 ? 1 : divisors(abs(s)));
cout << ans << '\n';
bool first = true;
auto print_value = [&](int v) {
if (!first) cout << ' ';
first = false;
cout << v;
};
for (int i = 0; i < x; i++) print_value(1);
for (int i = 0; i < y; i++) print_value(-1);
cout << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 676767677LL;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll divisors(int n) {
ll res = 0;
for (int d = 1; 1LL * d * d <= n; d++) {
if (n % d == 0) {
res++;
if (d != n / d) res++;
}
}
return res % MOD;
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int x, y;
cin >> x >> y;
int s = x - y;
ll ans = (s == 0 ? 1 : divisors(abs(s)));
cout << ans << '\n';
bool first = true;
auto print_value = [&](int v) {
if (!first) cout << ' ';
first = false;
cout << v;
};
for (int i = 0; i < x; i++) print_value(1);
for (int i = 0; i < y; i++) print_value(-1);
cout << '\n';
}
}