답안 #217597

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
217597 2020-03-30T10:31:26 Z bayemirov Lutrija (COCI19_lutrija) C++17
70 / 70
771 ms 508 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 ll INF = 1e9;
const int d[] = {0, 2, -2};

map<ll, ll> dist, p;
map<ll, vector<ll>> adj;
vector<ll> v = {2};
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++)
                   ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 643 ms 384 KB Output is correct
2 Correct 429 ms 384 KB Output is correct
3 Correct 771 ms 396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 624 ms 504 KB Output is correct
2 Correct 399 ms 384 KB Output is correct
3 Correct 561 ms 392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 693 ms 508 KB Output is correct
2 Correct 445 ms 504 KB Output is correct
3 Correct 541 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 510 ms 384 KB Output is correct
2 Correct 351 ms 384 KB Output is correct
3 Correct 187 ms 384 KB Output is correct