poj1001

#include <cstdio>

#include <cstring>

using namespace std;

#define eps 1e-8

#define D 150

void m(long long b[D], long long a[D])

{

    long long t[D << 1];

    memset(t, 0, sizeof(t));

    for(int i = 0; i < D; i++)

        for(int j = 0; j < D; j++)

        {

            t[i + j] += a[i] * b[j];

            t[i + j + 1] += t[i + j] / 10;

            t[i + j] %= 10; 

        }

    for(int i = 0; i < D; i++)

    {

        b[i] = t[i] % 10;

        t[i + 1] += t[i] / 10;

    }

}

void multi(long long a[D], int n)

{

    long long b[D];

    memset(b, 0, sizeof(b));

    b[0] = 1;

    while(n)

    {

        if(n & 1)

            m(b, a);

        n >>= 1;

        long long t[D];

        memcpy(t, a, sizeof(t));

        m(t, a);

        memcpy(a, t, sizeof(t));

    }

    memcpy(a, b, sizeof(b));

}

int main()

{

    double R;

    int n;

    while(scanf("%lf%d", &R, &n) == 2)

    {

        long long a[D];

        int pos, posn = 0;

        memset(a, 0, sizeof(a));

        R *= (pos = 100000);

        a[0] = (long long)(R + eps);

        while(a[0] % 10 == 0 && pos > 1)

        {

            a[0] /= 10;

            pos /= 10;

        }

        for(int i = 0; i < D - 1; i++)

        {

            a[i + 1] += a[i] / 10;

            a[i] %= 10;

        }

        while(pos > 1)

        {

            pos /= 10;

            posn++;

        }

        posn *= n;

        multi(a, n);

        pos = D - 1;

        while(a[pos] == 0 && pos >= posn)

            pos--;

        while(pos >= 0)

        {

            if(pos == posn - 1)

                printf(".");

            printf("%lld", a[pos--]);

        }

        printf("\n");

    }

    return 0;

}

 

你可能感兴趣的:(poj)