UVa - 113 Power of Cryptography

#include<stdio.h>
#include<float.h>
#include<math.h>
#include<assert.h>

#define EPSILON 0.01


double guess;
double n,p;


double nth_root_rec_bf_bs(double low,double high){

    if(abs(low-high) < EPSILON) return guess;

    guess=(low+high)/2;

    if(abs((pow(guess, n)) - p) <= EPSILON) return guess;

    if(pow(guess, n) > p) return nth_root_rec_bf_bs(low,guess);

    return nth_root_rec_bf_bs(guess,high);

}


inline double nthRoot(double x, double n){

    if(x==1) return 1;
    if(n==1) return x;

    if(x >= 0 and x <= 1) return nth_root_rec_bf_bs(x,1);

    if(x>1) return nth_root_rec_bf_bs(1,x);


assert(1==0);
}




int main(){


//        freopen("input.txt","r",stdin);
//        freopen("output.txt","w",stdout);   


    while (scanf("%lf%lf", &n, &p) == 2){

        printf("%.0lf\n", nthRoot(p,n));

    }

}

No comments:

Post a Comment