Submission #217591

# Submission time Handle Problem Language Result Execution time Memory
217591 2020-03-30T10:23:40 Z bayemirov Lutrija (COCI19_lutrija) C++17
0 / 70
352 ms 480 KB
//bayemirov                                       
#include <bits/stdc++.h>               

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;

#define pb push_back

const int INF = 1e9;
const int d[] = {0, 2, -2};

map<ll, int> dist, p;
map<ll, vector<ll>> adj;
vector<ll> v;
ll a, b;

bool isPrime(ll x) {
	if (x < 2) return 0;
	for (ll i = 2; i * i <= x; i++)
		if (x % i == 0)
			return 0;
	return 1;
}

void bfs() {
	for (auto x : v) dist[x] = INF;
	dist[a] = 0;
	queue<ll> q;
	q.push(a);
	while (!q.empty()) {
		ll v = q.front();
		q.pop();
		for (auto to : adj[v]) {
			if (dist[to] > dist[v] + 1) {
				dist[to] = dist[v] + 1;
				p[to] = v;
				q.push(to);
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> a >> b;
	for (int i = 0; i < 3; i++) {
		if (isPrime(a+d[i])) v.pb(a+d[i]);
		if (isPrime(b+d[i])) v.pb(b+d[i]);
	}
	for (int i = 0; i < v.size(); i++)
		for (int j = 0; j < v.size(); j++)
			if (isPrime(abs(v[i]-v[j])))
				adj[v[i]].pb(v[j]);
	bfs();
	if (dist[b] == INF) cout << -1, exit(0);
	p[a] = -1;
	vector<ll> ans;
	while (b != -1) {
		ans.pb(b);
		b = p[b];
	}
	reverse(ans.begin(), ans.end());
	cout << ans.size() << "\n";
	for (auto x : ans) cout << x << ' ';
	return 0;
}                    

Compilation message

lutrija.cpp: In function 'int main()':
lutrija.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < v.size(); i++)
                  ~~^~~~~~~~~~
lutrija.cpp:55:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < v.size(); j++)
                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Incorrect 4 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Incorrect 5 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 337 ms 480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 313 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 352 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 256 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -