Problem:
Problem C: Complex Integer Solutions

Let f(x) = a0 + a1x + a2x2 + ... + adxd be the function where each ai (0 ≤ i ≤ d) is a constant integer (and ad is non-zero) and x is a variable. Your task is to write a program that finds all complex integer solutions of the equation f(x) = 0 for a given f(x). Here, by complex integers, we mean complex numbers whose
real and imaginary parts are both integers.

Input

The input consists of two lines. The first line of the input contains d, the degree of f(x). The second line contains (d + 1) integers a0, ... , ad, the coeffcients of the equation. You may assume all the following: 1 ≤ d ≤ 10, |ai| ≤ 106 and ad ≠ 0.

Output

There should be two lines in the output. In the first line, print the number m of complex integer solutions. In the second line, print m solutions separated by space. Each solution should be counted and printed exactly once even if it is a multiple root. The solutions should be printed in ascending order of their real parts then their imaginary parts, and in the following fashion: 0, -2, i, -3i, 2+i, and 3-4i.

Sample Input 1

4
-2 0 0 0 2

Output for the Sample Input 1

4
-1 -i i 1

Sample Input 2

8
0 0 25 15 17 -10 1 -1 1

Output for the Sample Input 2

5
-1-2i -1+2i 0 2-i 2+i

Solution in C++14 (judge verdict: Wrong Answer)

#include "bits/stdc++.h"
using namespace std;
using ld = long double;
ld eps = -1e11;
vector<complex<ld>>quad_equa(complex<ld>a,complex<ld>b,complex<ld>c) {
	auto root = b*b - 4.0l * a*c;
	auto D = sqrt(root);
	
	auto ans = - b / (2.0l * a);
	vector<complex<ld>>anss(2);
	anss[0] = (-b + D) / (2.0l*a);
	anss[1] = (-b - D) / (2.0l*a);
	return anss;
	
}

//f(x) = a0xn + a1xn - 1 + ??\??\??\ + an - 1x + an = 0
void beast(const vector<ld>as,vector<complex<ld>>&ans) {

	const int N = as.size()-1;
	if (N==1) {
		ans.push_back(-as[1] / as[0]);
		return;
	}
	else if (N==2) {
		auto aa= quad_equa(as[0], as[1], as[2]);
		ans.insert(ans.end(), aa.begin(), aa.end());
		return;
	}
	else {

		int k = 1;
		vector<ld>ps, qs;
		ps.push_back(3.52462);
		qs.push_back(-5.32562);
		const ld epsir = 1e-8;
		while (true) {
			
			vector<ld>bs(N+1);
			bs[0] = as[0];
			bs[1] = as[1] - ps[k - 1]*bs[0];
			for (int i = 2; i <=N; ++i) {
				bs[i] = as[i] - ps[k - 1] * bs[i - 1] - qs[k - 1] * bs[i - 2];
				
			}
			vector<ld>cs(as.size());
			cs[0] = bs[0];
			cs[1] = bs[1] - ps[k - 1] * cs[0];
			for (int i = 2; i <=N; ++i) {
				cs[i] = bs[i] - ps[k - 1] * cs[i - 1] - qs[k - 1] * cs[i - 2];
			}

			auto D = powl(cs[N - 2], 2) - cs[N - 3]*(cs[N - 1] - bs[N - 1]);
			auto deltap = (bs[N - 1] * cs[N - 2] - bs[N] * cs[N - 3]) / D;
			auto deltaq = (bs[N] * cs[N - 2] - bs[N - 1] * (cs[N - 1] - bs[N - 1])) / D;

			if (k==1e5||abs(deltap) < epsir&&abs(deltaq) < epsir) {
				auto aaa = quad_equa(1, ps[k - 1], qs[k - 2]);
				ans.insert(ans.end(), aaa.begin(), aaa.end());
				 beast(vector<ld>(bs.begin(), bs.end() - 2),ans);
				return;
			}
			else {
				ps.push_back( ps[k-1] + deltap);
				qs.push_back( qs[k-1] + deltaq);
				k++;
			}
		}
		
	}
	assert(false);
	return;
}
int main() {
	int N; cin >> N;
	vector<ld>as(N + 1);
	for (int i = 0; i < N + 1; ++i) {
		cin >> as[i];
	}
	reverse(as.begin(), as.end());
	vector<complex<ld>>ans; 
	beast(as, ans);
	{
		vector<complex<ld>>bb;
		for (auto aa : ans) {
			ld re(aa.real());
			ld im(aa.imag());
			re = abs(re);
			im = abs(im);
			re += 1e-5;
			im += 1e-5;
			if (abs(re - ld(int(re))) < 1e-4&&abs(im - ld(int(im))) < 1e-4) {
				bb.push_back(aa);
			}
			
		}
		ans = bb;
	}
	vector<pair<int, int>>outputs;
	for (int i = 0; i < ans.size();++i){
		auto aa(ans[i]);
		int re = int(round(aa.real()));
		int im = int(round(aa.imag()));

		outputs.emplace_back(re, im);
	}
	sort(outputs.begin(), outputs.end());
	outputs.erase(unique(outputs.begin(), outputs.end()), outputs.end());
	cout << outputs.size() << endl;
	for (int i = 0; i < outputs.size(); ++i) {
		auto aa(outputs[i]);
		if (aa.first) {
			if (aa.second == 1) {
				cout << aa.first << "+i";
			}
			else if (aa.second == -1) {
				cout << aa.first << "-i";
			}
			else if (aa.second > 0) {
				cout << aa.first << "+" << aa.second << "i";
			}
			else if(aa.second==0){
				cout << aa.first;

			}
			else {
				cout << aa.first << aa.second << "i";
			}
		}
		else {
			if (aa.second == 1) {
				cout << "i";
			}
			else if (aa.second == -1) {
				cout <<  "-i";
			}else if (aa.second == 0) {
				cout << 0;
			}
			else {
				cout << aa.second << "i";
			}
		}
		if (i == outputs.size() - 1)cout << endl;
		else cout << " ";
	}
	return 0;
}