답안 #1089539

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089539 2024-09-16T16:06:17 Z Gervid Lutrija (COCI19_lutrija) C++17
42 / 70
1589 ms 524288 KB
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <limits.h>
#include <algorithm>
#include <math.h>

using namespace std;

set<long long> been;
vector<long long> out;

bool isprime(long long n)
{
	for (long long i = 2; i*i <= n; i++)
	{
		if (n % i == 0) return false;
	}
	return true;
}

bool dfs(int node, int goal)
{
	if (node == goal)
	{
		out.push_back(node);
		return true;
	}

	for (long long neighbour : {2, node + 2, node - 2 > 2 ? node - 2 : 4})
	{
		if (!been.count(neighbour) && isprime(neighbour) && isprime(abs(node - neighbour)))
		{
			been.insert(neighbour);
			if (dfs(neighbour, goal))
			{
				out.push_back(node);
				return true;
			}
		}
	}
	return false;
}

int main()
{
	iostream::sync_with_stdio(0);
	cin.tie(0);

	long long a, b;
	cin >> a >> b;

	if (!dfs(a, b))
	{//need to go down to 2, or not possible
		if (been.count(2))
		{
			been.clear();

			dfs(b, a);

			if (been.count(2))
			{
				out.clear();
				been.clear();
				dfs(a, 2);

				vector<long long> firsthalf;
				out.swap(firsthalf);

				been.clear();
				dfs(b, 2);

				cout << out.size() + firsthalf.size() - 1 << '\n';
				for (int i = firsthalf.size() - 1; i > 0; i--)
				{
					cout << firsthalf[i] << ' ';
				}
				for (int i = 0; i < out.size(); i++)
				{
					cout << out[i] << ' ';
				}
				return 0;
			}
		}

		cout << -1;
		return 0;
	}

	cout << out.size() << '\n';
	for (int i = out.size() - 1; i >= 0; i--)
	{
		cout << out[i] << ' ';
	}
}

Compilation message

lutrija.cpp: In function 'int main()':
lutrija.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (int i = 0; i < out.size(); i++)
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 760 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1589 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -