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.
The number of digits is tiny: only , , or . So don't overthink this into some cursed combinatorics monster.
Generate every permutation of the digits of , then sort them in ascending order. The largest case has only permutations.
After sorting, the two codes are simply perms[j-1] and perms[k-1] because the indices are 1-based.
Count A by checking positions where the two strings have the same digit.
Because both codes are permutations of the exact same distinct digits, every digit matches somewhere. So B = length - A.
Core Idea
This problem looks like it wants you to do something fancy with permutation indexing, but the constraints quietly say: absolutely not.
The base code is always one of:
So the maximum number of permutations is only:
That is tiny. Generate all permutations, sort them, grab the requested two, compare them. Done.
Permutation Order
The statement says all permutations are sorted in ascending order.
For fixed-length digit strings with no leading zero issues here, lexicographic order is the same as numeric ascending order. Since the digits in 12, 123, and 1234 are already sorted, we can just use next_permutation starting from the original string.
For example, for 123, the sorted permutations are:
123, 132, 213, 231, 312, 321So if and , we compare:
132 vs 312Counting A
An A means same digit, same position.
So loop over each index :
For 132 and 312:
1 vs 3 -> different
3 vs 1 -> different
2 vs 2 -> sameSo .
Counting B
Normally, Bulls and Cows style problems require careful frequency counting for B, especially when digits repeat.
But here, the digits are distinct and both strings are permutations of the same base number. That means every digit in one code appears exactly once in the other code.
So every digit is either:
ABTherefore:
where is the number of digits.
No frequency maps needed. No drama. The problem is literally just implementation.
Algorithm
For each test case:
next_permutation.perms[j - 1] and perms[k - 1].A.B = length - A.xAyB.Complexity
At most permutations are generated per test case, each of length at most .
So the runtime is basically:
which is comically fine for .
Memory usage is also tiny.
#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, j, k;
cin >> n >> j >> k;
string s = to_string(n);
vector<string> perms;
sort(s.begin(), s.end());
do {
perms.push_back(s);
} while (next_permutation(s.begin(), s.end()));
string a = perms[j - 1];
string b = perms[k - 1];
int A = 0;
for (int i = 0; i < (int)a.size(); i++) {
if (a[i] == b[i]) A++;
}
int B = (int)a.size() - A;
cout << A << 'A' << B << 'B' << '\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, j, k;
cin >> n >> j >> k;
string s = to_string(n);
vector<string> perms;
sort(s.begin(), s.end());
do {
perms.push_back(s);
} while (next_permutation(s.begin(), s.end()));
string a = perms[j - 1];
string b = perms[k - 1];
int A = 0;
for (int i = 0; i < (int)a.size(); i++) {
if (a[i] == b[i]) A++;
}
int B = (int)a.size() - A;
cout << A << 'A' << B << 'B' << '\n';
}
return 0;
}