La soluzione produce l’output giusto ma va in tle, aiuto?
#include <bits/stdc++.h>
using namespace std;
void occulta(int N, int M, set<long long>& result, long long current, int lastDigit = 0) {
if (N == 0) {
result.emplace(current % M);
return;
}
result.emplace(current % M);
if (lastDigit != 3) {
occulta(N - 1, M, result, current * 10 + 3, 3);
}
if (lastDigit != 6) {
occulta(N - 1, M, result, current * 10 + 6, 6);
}
if (lastDigit != 9) {
occulta(N - 1, M, result, current * 10 + 9, 9);
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T;
cin >> T;
for(int i = 0; i != T; i++){
int N, M;
cin >> N >> M;
set<long long>nums;
long long current;
occulta(N, M, nums, current);
cout << *nums.rbegin() << " ";
}
}