CSES - Putka Open 2020 – 1/5 - Results
Submission details
Task:Lista
Sender:Grez
Submission time:2020-09-06 12:55:28 +0300
Language:C++17
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:72:13: error: invalid conversion from 'Solver*' to 'int' [-fpermissive]
  Solver s = new Solver(amo);
             ^~~~~~~~~~~~~~~
input/code.cpp:18:1: note:   initializing argument 1 of 'Solver::Solver(int)'
 Solver::Solver(int amount) {
 ^~~~~~
input/code.cpp:74:9: error: type 'class Solver' argument given to 'delete', expected pointer
  delete s;
         ^

Code

#include <iostream>

using namespace std;

class Solver
{
	public:
		Solver(int amount);
		~Solver();
		void Solve();
	private:
		bool* sieve;
		int* ans;
		int amo;
		void swap(int a, int b);
		bool recurse(int pos);
};
Solver::Solver(int amount) {
	amo = amount;
	sieve = new bool(amo);
	ans = new int(amo);
}
Solver::~Solver() {
	delete sieve;
	delete ans;
}
void Solver::swap(int a, int b)
{
	if (a == b) return;
	int t = ans[a];
	ans[a] = ans[b];
	ans[b] = t;
}
bool Solver::recurse(int pos)
{
	for (int other = pos; other >= 0; other -= 2)
	{
		if (sieve[(ans[other] + ans[pos + 1]) / 2]) continue;
		if (pos == 0) return true;
		swap(pos, other);
		if (recurse(pos - 1)) return true;
		swap(pos, other); //swap back
	}
	return false;
}

void Solver::Solve()
{
	int maxPrime = amo * 2;
	for (int mul = 3; (mul * mul) < maxPrime; mul += 2)
	{
		for (int hole = mul / 2 + mul; hole < amo; hole += mul)
		{
			sieve[hole] = true;
		}
	}


	for (int i = 0; i < amo; i++) { ans[i] = i + 1; }

	if (recurse(amo - 2))
	{
		for (int i=0; i<amo; i++) {
			cout << ans[i];
		}
		cout << endl;
	}
}
int main() {
    int amo;
    cin >> amo;
	Solver s = new Solver(amo);
	s.Solve();
	delete s;
}