Problem Statement
You're given four integers , b , c и d где:
Код: Выделить всё
1 ≤ a, b, c, d < 10^18
Код: Выделить всё
possible
a' b'
< /code>
В противном случае, печати: < /p>
impossible
образец 1
input:Код: Выделить всё
163 326 1 2
Код: Выделить всё
possible
1 2
пример 2
input:Код: Выделить всё
871 1261 13 39
Код: Выделить всё
possible
87 261
пример 3
input:Код: Выделить всё
123 267 12339 23679
Код: Выделить всё
impossible
пример 4
input:Код: Выделить всё
13 363 1 36
Код: Выделить всё
possible
1 36
образец 5
input:Код: Выделить всё
2113 11343 2 34
Код: Выделить всё
possible
2 34
< /code>
Мой подход < /h3>
Я написал рекурсивное решение с памятью, которое пытается все действительные делеции цифр. Если новый a '/b' #include
#include
#include
#include
#include
using namespace std;
map memo;
string resultA, resultB;
bool found = false;
long long stringToLong(const string &s) {
if (s.empty()) return 0;
if (s.length() > 1 && s[0] == '0') return -1;
long long result = 0;
for (char c : s) {
result = result * 10 + (c - '0');
}
return result;
}
bool solve(string a, string b, long long c, long long d) {
if (found) return true;
pair state = {a, b};
if (memo.count(state)) return memo[state];
long long numA = stringToLong(a);
long long numB = stringToLong(b);
if (numA > 0 && numB > 0 && numA * d == numB * c) {
resultA = a;
resultB = b;
found = true;
return memo[state] = true;
}
for (char digit = '0'; digit > a >> b >> c >> d;
string sa = to_string(a);
string sb = to_string(b);
if (solve(sa, sb, c, d)) {
cout
Подробнее здесь: https://stackoverflow.com/questions/796 ... ty-problem
Мобильная версия