CSES - Datatähti 2021 alku - Results
Submission details
Task:2021-luvut
Sender:Eljas
Submission time:2020-10-09 10:43:44 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void fill(std::__cxx11::string&, std::__cxx11::string&, int&)':
input/code.cpp:178:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int j = oneLoc + 1; j < s.size(); j++)
                           ~~^~~~~~~~~~
input/code.cpp:184:12: error: 'pow' is not a member of 'std'
  i += std::pow(10, length);
            ^~~

Code

#include <string>
#include <iostream>
#include <algorithm>




int getState(std::string& s) {

	int state = 0;
	for (char c : s)
	{
		if (state == 0 && c == '2')
		{
			state = 1;
		}
		else if (state == 1 && c == '0')
		{
			state = 2;
		}
		else if (state == 2 && c == '2')
		{
			state = 3;
		}
		else if (state == 3 && c == '1')
		{
			state = 4;
		}
	}
	return state;
}


void addThousand(std::string& s, std::string fh, int state) {
	int size = s.size();

	std::string sh = s.substr(size - 3, 4);


	if (fh == "")
	{
		s = "1" + sh;
	}
	else {
		long long l = std::stoll(fh);
		l++;

		std::string ls = std::to_string(l);

		s = ls + sh;
		while (std::count(s.begin(), s.end(), '2') < 2)
		{
			l++;
			ls = std::to_string(l);
			s = ls + sh;
		}


		if (s[0] == '2' && fh[0] != '2' && std::count(s.begin(), s.end(), '2') > 2)
		{
			int it = 1;
			while (s[it] != '2') {
				it++;
			}
			s[it] = '0';
		}
	}
}

void addHundred(std::string& s, std::string& fh, int state) {
	int size = s.size();

	if (s[size - 3] == '9') {
		s[size - 3] = '0';
		addThousand(s, fh, state);
		return;
	}
	else if (state >= 2)
	{
		s[size - 3]++;
	}
	else
	{
		s[size - 3] = '0';
		addThousand(s, fh, state);
		return;

	}
}

void addTen(std::string& s, std::string& fh, int state) {

	int size = s.size();

	if (state >= 3)
	{
		if (s[size - 2] == '9')
		{
			s[size - 2] = '0';
			addHundred(s, fh, state);
			return;
		}
		s[size - 2]++;
		return;
	}


	if (s[size - 2] == '2')
	{
		if (s[size - 3] == '0')
		{
			addHundred(s, fh, state);
			return;
		}
		else if (s[size - 3] == '1')
		{
			s[size - 2] = '0';
			addHundred(s, fh, state);
			return;
		}
		else if (s[size - 3] == '2')
		{
			s[size - 2]++;
		}
		else
		{
			addHundred(s, fh, state);
		}
	}
	else if (s[size - 2] == '9')
	{
		s[size - 2] = '2';
		s[size - 3] = '3';
	}
	else {
		s[size - 2]++;
	}
}

void addOne(std::string& s, std::string& fh, int state) {
	int size = s.size();
	if (s[size - 1] == '1')
	{
		if (s[size - 2] == '0')
		{
			s[size - 1] = '0';
			s[size - 2] = '1';
			return;
		}
		else if (s[size - 2] == '1')
		{
			s[size - 1] = '2';
			return;
		}
		else
		{
			addTen(s, fh, state);
			return;
		}
	}
	else if (s[size - 1] == '9')
	{
		s[size - 1] = '1';
		addTen(s, fh, state);
	}
	else {
		s[size - 1]++;
	}
}



void fill(std::string& s, std::string& fh, int& i) {
	int oneLoc = fh.rfind('1');

	int length = s.size() - oneLoc;

	for (int j = oneLoc + 1; j < s.size(); j++)
	{
		s[j] = '0';
	}

	s[oneLoc] = '2';
	i += std::pow(10, length);

}

int main() {
	int n;
	std::cin >> n;


	std::string s = "2021";

	for (int i = 0; i < n - 1; )
	{
		int size = s.size();
		std::string fh = s.substr(0, size - 3);

		int state = getState(fh);
		if (state == 4)
		{
			fill(s, fh, i);
		}
		else
		{
			addOne(s, fh, state);
			i++;
		}
	}
	std::cout << s;
}