All pastes #40848 Raw Edit

Something

public text v1 · immutable
#40848 ·published 2006-02-09 11:41 UTC
rendered paste body
#include<stdio.h>

#include<math.h>



#define YES 1

#define NO 0

#define mod(x) ((x<=0)? x*-1:x*1)

#define complexmod(a,b) sqrt(a*a+b*b)

#define cuberoot(a) (a<0)?(pow(a*-1,((double)1/(double)3))*-1):pow(a,((double)1/(double)3))



int solve_cubic(double ,double ,double ,double );

int solve_quadratic(double ,double ,double );

int solve_linear(double ,double );

double x1=0,x2=0,x3=0,x4=0,x2real=0,x2imaginary=0,imaginary_roots=NO;

int main() {

    double a,b,c,d,e;

    scanf("%lf %lf %lf %lf %lf",&a,&b,&c,&d,&e);

    if(a==0) {

        if(solve_cubic(b,c,d,e))

            printf("YES");

        else

            printf("NO");

    }

    else {



        double f=0,g=0,h=0;



        b=b/a;

        c=c/a;

        d=d/a;

        e=e/a;

        a=1;



        f = c-((3*b*b)/8);

        g = d+((b*b*b)/8)-((b*c)/2);

        h = e - ((3*b*b*b*b)/256)+((b*b*c)/16)-((b*d)/4);



        solve_cubic(1,(f/2), ((f*f-4*h)/16), ((-1*g*g)/64));

        {

            if(imaginary_roots == YES) {

                double r,x,y,s;

                r=sqrt(x2real*x2real + x2imaginary*x2imaginary);

                y = (r-x2real)/2;

                y=sqrt(y);

                x = x2imaginary/(2*y);

                //printf("\n%lf + i%lf, %lf - i%lf",x,y,x,y);

                r = (-g)/(8*(x*x+y*y));

                s= b/(4*a);

                //printf("\n%lf + i%lf, %lf - i%lf %lf %lf",x,y,x,y,r,s);

                x1= (2*x) + r - s;

                x2= -(2*x) + r - s;

                x2real = -r-s;

                x2imaginary = 2*y;

                //printf("\n%lf %lf",x1,x2);

                //printf("\n%lf + i%lf",x2real,x2imaginary);

                if(mod(x1)<=1 && mod(x2)<=1 && complexmod(x2real,x2imaginary)<=1)

                    printf("YES");

                else

                    printf("NO");

            }

            else {

                //printf("\n%lf %lf %lf",x1,x2,x3);

                double p=0,q=0,r=0,s=0;

                if((int)x1 == 0) {

                    if(x2 != 0 && x3 != 0) {

                        if(x2<=0 || x3<=0) {

                            p = sqrt(mod(x2));

                            q = sqrt(mod(x3));

                            imaginary_roots=YES;

                        }

                        else {

                            p = sqrt(x2);

                            q = sqrt(x3);

                        }

                   }

                }

                else if((int)x2 == 0) {

                    if(x1 != 0 && x3 != 0) {

                        if(x1<=0 || x3<=0) {

                            p = sqrt(mod(x1));

                            q = sqrt(mod(x3));

                            imaginary_roots=YES;

                        }

                        else {

                            p = sqrt(x1);

                            q = sqrt(x3);

                        }

                    }

                }

                else if((int)x3 == 0) {

                   if(x2 != 0 && x1 != 0) {

                       if(x2<=0 || x1<=0) {

                            p = sqrt(mod(x2));

                            q = sqrt(mod(x1));

                            imaginary_roots=YES;

                        }

                        else {



                            p = sqrt(x1);

                            q = sqrt(x2);

                        }

                    }

                }

                else {

                    if(x2<=0 || x1<=0) {

                        p = sqrt(mod(x2));

                        q = sqrt(mod(x1));

                        imaginary_roots=YES;

                    }

                    else {

                        p = sqrt(x1);

                        q = sqrt(x2);

                    }

                }

                s = b/(4*a);

                //printf("\n%lf %lf %lf %lf",p,q,r,s);

                if(imaginary_roots!=YES) {

                    r = (-1*g)/(8*p*q);

                    x1 = p + q + r - s;

                    x2 = p - q - r - s;

                    x3 = (-1*p) + q - r - s;

                    x4 = (-1*p) - q + r - s;

                    printf("\n%lf %lf %lf %lf",x1,x2,x3,x4);

                    if(mod(x1) <=1 && mod(x2)<=1 && mod(x3)<=1 && mod(x4)<=1)

                        printf("YES");

                    else

                        printf("NO");

                }

                else {

                    r = g/(8*p*q);

                    //printf("\n%lf %lf %lf %lf",p,q,r,s);

                    double v,v1,v2,v3;

                    v=p+q;

                    v1=r-s;

                    v2=p-q;

                    v3=-r-s;

                    printf("\n%lf + i%lf %lf + i%lf",v,v1,v2,v3);

                    if(complexmod(v,v1)<=1 && complexmod(v2,v3)<=1)

                        printf("YES");

                    else

                        printf("NO");

                }

            }

        }

    }

    return 0;

}



int solve_cubic(double a,double b,double c,double d) {



    double f=0,g=0,h=0;

    if(a==0) {

        if(solve_quadratic(b,c,d))

            return YES;

        else

            return NO;

    }

    else {



        f = (((3*c)/a) - ((b*b)/(a*a)))/3;

        g = (((2*b*b*b)/(a*a*a)) - ((9*b*c)/(a*a)) + ((27*d)/a))/27;

        h = ((g*g)/4) + ((f*f*f)/27);



        if(f==0 && g==0 && h==0) {

            x1=cuberoot((d/a)) * -1;

            x2=x1;

            x3=x1;

            //printf("\n%lf ",x1);

            if(mod(x1)<=1)

                return YES;

            else

                return NO;

        }

        else if(h<=0) {

            double i=0,j=0,k=0,l=0,m=0,n=0,p=0;



            i = sqrt((g*g/4) - h);

            j = cuberoot(i);

            k = acos (-1*(g /(2*i)));

            l = j * -1;

            m = cos (k/3);

            n = sqrt(3) * sin (k/3);

            p = (b/(3*a)) * -1;



            x1 = ((2*j)*cos(k/3))-(b/(3*a));

            x2 = (l*(m+n))+p;

            x3 = (l*(m-n))+p;

            //printf("\n%lf %lf %lf",x1,x2,x3);



            if(mod(x1)<=1 && mod(x2)<=1 && mod(x3)<=1)

                return YES;

            else

                return NO;

        }

        else if(h>0) {



            double r=0,s=0,t=0,u=0;



            //printf("\n%lf %lf %lf",f,g,h);

            r = (-1*(g/2)) + sqrt(h);

            s = cuberoot(r);

            t = (-1*(g/2)) - sqrt(h);

            u = cuberoot(t);



            //printf("\n%lf %lf %lf %lf",r,s,t,u);

            x1 = (s+u)-(b/(3*a));

            x2real =  ((-1*(s + u))/2) - (b/(3*a));

            x2imaginary = ((s-u)*sqrt(3))/2;

            x2=0;

            x3=0;

            imaginary_roots=YES;

            //printf("\n%lf %lf %lf",x1,x2real,x2imaginary);



            if(mod(x1)<=1 && complexmod(x2real,x2imaginary)<=1)

                return YES;

            else

                return NO;

        }

    }

}



int solve_quadratic(double a,double b,double c) {



    if(a==0) {

        if(solve_linear(b,c))

            return YES;

        else

            return NO;

    }

    else {

        double d=0;

        d = (b*b) - (4*a*c);

        if(d>=0) {

            double x1=0,x2=0;

            x1 = ((b*-1) + sqrt(d)) / (2*a);

            x2 = ((b*-1) - sqrt(d)) / (2*a);



            if (mod(x1)<=1 && mod(x2)<=1)

                return YES;

            else

                return NO;

        }

        else {

            double x1real=0,x1imaginary=0;

            d = mod(d);

            x1real = (b*-1)/(2*a);

            x1imaginary = (sqrt(d))/(2*a);

            if(complexmod(x1real,x1imaginary)<=1)

                return YES;

            else

                return NO;

        }

    }

}



int solve_linear(double a,double b) {

    double x1=0;

    x1 = (b*-1)/a;

    if (mod(x1)<=1)

        return YES;

    else

        return NO;

}