Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// O(200K + 100M) vo najlos slucaj
- /*
- input
- broj na cekori niza(100000): -1 znaci ne visited
- for za palindromi {
- dodadi vo vector
- }
- bfs (!queue.empty()) {
- queue rabota
- for za palindromi {
- dodadi vo queue
- broj na cekori niza dodadi
- }
- }
- output
- */
- #include <bits/stdc++.h>
- using namespace std;
- bool isPalindrome(int i)
- {
- int reversedI = 0, iCopy = i;
- while (i)
- {
- reversedI *= 10;
- reversedI += i % 10;
- i /= 10;
- }
- return iCopy == reversedI;
- }
- int main()
- {
- int s, e;
- cin >> s >> e;
- vector <int> broj_na_cekori(100000), palindromi;
- for (int i = 0; i < 100000; i++)
- {
- if (isPalindrome(i))
- palindromi.push_back(i);
- }
- for (int &x : broj_na_cekori)
- x = -1;
- queue <int> broevi_pending;
- broevi_pending.push(s);
- broj_na_cekori[s] = 0;
- while (broevi_pending.front() != e)
- {
- for (int i = 2; palindromi[i] < broevi_pending.front(); i++)
- {
- if (palindromi[i] + broevi_pending.front() <= e && broj_na_cekori[palindromi[i] + broevi_pending.front()] == -1)
- {
- broevi_pending.push(palindromi[i] + broevi_pending.front());
- broj_na_cekori[palindromi[i] + broevi_pending.front()] = broj_na_cekori[broevi_pending.front()] + 1;
- }
- }
- broevi_pending.pop();
- }
- cout << broj_na_cekori[e];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement