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.
Don’t try to search all palindromes. Look at what the condition says about modulo .
Since , we need
So the whole game is: find a palindrome with the same remainder as modulo .
For residues , life is embarrassingly easy: the one-digit number equal to the residue is already a palindrome.
The only remaining residues are and . For , use . For , notice that and is a palindrome.
Therefore:
The only impossible case is .
We need non-negative integers such that
is a palindrome, and is divisible by .
Since , the divisibility condition becomes
So we do not need a palindrome close to . We only need any palindrome with the same remainder modulo as . That’s the whole trick. Everything else is noise.
Let
For most residues, there is an absurdly small palindrome with that residue:
| residue | palindrome | |---:|---:| | | | | | | | | | | | |
Why does this work?
Then we output
By construction, , so is divisible by .
For , our representative is . This requires .
The smallest positive number with remainder modulo is itself. Can work?
If and , then since , we must have , so . But is not a palindrome.
Equivalently, all palindromes are
and none of them is congruent to modulo .
So is impossible, and every other works.
For each test case:
We prove that the algorithm outputs a valid beautiful pair whenever one exists, and outputs only when impossible.
If the algorithm outputs a pair , then .
Proof. The algorithm always sets , so directly
If the algorithm outputs a pair , then is a palindrome.
Proof. The possible values of are:
All one-digit numbers are palindromes, and both and are palindromes. Therefore is always a palindrome. ∎
If the algorithm outputs a pair , then is divisible by .
Proof. The chosen always satisfies
Therefore
so . ∎
If , no beautiful pair exists.
Proof. We need with divisible by and . Since , the only possible multiple of is . Hence , but is not a palindrome. Contradiction. ∎
For every test case, the algorithm prints a valid beautiful pair if one exists, and prints otherwise.
Proof. For , Lemma 4 shows that printing is correct. For every other , the algorithm outputs a pair. By Lemmas 1, 2, and 3, that pair satisfies all required conditions. ∎
Each test case uses only a few arithmetic operations, so the complexity is
per test case, and
overall. Memory usage is
#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--) {
ll n;
cin >> n;
ll r = n % 12;
ll a;
if (r == 10) {
if (n == 10) {
cout << -1 << '\n';
continue;
}
a = 22;
} else if (r == 11) {
a = 11;
} else {
a = r;
}
cout << a << ' ' << n - a << '\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--) {
ll n;
cin >> n;
ll r = n % 12;
ll a;
if (r == 10) {
if (n == 10) {
cout << -1 << '\n';
continue;
}
a = 22;
} else if (r == 11) {
a = 11;
} else {
a = r;
}
cout << a << ' ' << n - a << '\n';
}
return 0;
}