Poj Solution 3066

http://poj.org/problem?id=3066

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <algorithm>


int main( ) {
    int m, p, a, b, k, i;
    double sum, q_a, a_p, ans, t;
    while( scanf( "%d%d%d%d", &m, &p, &a, &b ) == 4 ) {
        q_a = sqrt((double)a);
        sum = b*q_a+m/q_a;
        
        k = int(sum/(q_a+1.0/q_a)+1e-6);
        
        sum -= k*(q_a+1.0/q_a) + 1/q_a;
        
        while( k > m )
            ;

        for( a_p=1, i=0; i<p/2; i++ )
            a_p *= a;
        
        ans = k*a_p;
        if( k != m ) {
            for( t=1, i=0; i<p; i++ )
                t *= sum;
            ans += (m-k-1)/a_p + t;
        }
        


        printf( "%.0lfn", ans );
    }
    return 0;
}

											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *