/*@cj`@p̉@vO@*/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <malloc.h>
#include <complex.h>

/*
	f(x) = c[0]*x^n + c[1]*x^(n-1) + ..... + c[n]
	̌WzcɁC̎nɁC[v̏Ilepsɓ
	̊֐sƁC̉zresultɓ
*/
int Dka( double *c, complex *result, int n, double eps )
{
	int m = 100;
	int i, j, k, flag;
	complex f1, f2, b, z, zmax, *ratio;
	double rmax, cf, p0, p1;
	complex *ri;
	
	ratio = (complex *)malloc( (n+1)*sizeof(complex));
	ri = (complex *)malloc( (n+1)*sizeof(complex));
	
	for( i = 0; i < n; i++ )
	{
		result[i] = complex(0.0, 0.0);
	}
	
	for( i = 1; i < n+1; i++ )
	{
		c[i] = c[i] / c[0];
	}
	c[0] = 1.0;

	rmax = 0.0;
	b = -1.0 * c[1] / complex(n, 0.0);
	for(i = 2; i < n+1; i++ )
	{
		cf = (double)n * pow( abs( c[i] ), 1.0/i );
		if( rmax < cf )
			rmax = cf;
	}

	p0 = 3.0 / (double)(2*n);
	p1 = 2.0 * M_PI / (double)n;
	
	zmax = complex(rmax, 0.0);
	for( i = 0; i < n; i++ )
	{
		z = exp( complex(0.0, p0 + (double)i*p1));
		result[i] = b + z*zmax;
	}
	
	k = 0;
	while( k++ < m )
	{
		for( j = 0; j < n; j++ )
			ri[j] = result[j];
		for( i = 0; i < n; i++ )
		{
			f1 = c[0];
			f2 = complex(1.0, 0.0);
			for( j = 0; j < n; j++ )
			{
				f1 = f1*ri[i] + c[j+1];
				if( j != i )
				{
					f2 = f2 * (ri[i] - ri[j]);
				}
			}
			ratio[i] = f1/f2;
			result[i] = ri[i] - ratio[i];
		}
		
		flag = 1;
		for( i = 0; i < n; i++ )
		{
			if( abs(ratio[i]) > eps )
			{
				flag = 0;
				break;
			}
		}
		if( flag )
		{
			free(ratio);
			return(1);
		}
	}
	free(ratio);
	return(0);
}
