2015年1月9日 星期五

2014/01/09 vijos P1010清帝之惑之乾隆

// https://vijos.org/p/1010
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

struct ll
{
    int _num[2002];
    const static int n = 1000;

    int& operator[](int x)
    {
        if( x < 0 ) return _num[x+n];
        else return _num[x+n+1];
    }
    static void carry(ll &p)
    {
        for(int i = -n; i < n; i++)
            if( p[i] >= 10 )
            {
                p[i+1] += p[i]/10;
                p[i] %= 10;
            }
            else if( p[i] < 0 )
            {
                p[i+1]--;
                p[i] += 10;
            }
    }

    ll(){ fill(_num, _num+n*2+2, 0); }
    ll(char *c)
    {
        ll &now = *this;
        fill(_num, _num+n*2+2, 0);

        char *ptr = strchr(c, '.');
        int pos = ptr ? ptr-c : strlen(c) ;

        for(int i = 0; i < pos; i++)
            now[pos-i-1] = c[i]-'0';

        for(int i = pos+1, cn = strlen(c); i < cn; i++)
            now[pos-i] = c[i]-'0';
    }

    ll operator*(ll p)
    {
        ll &now = *this;
        ll re = ll();

        int is, js;
        int ie, je;

        for(int i = n; ; i--)
            if( now[i] ){ ie = i; break; }

        for(int j = n; ; j--)
            if( p[j] ){ je = j; break; }

        for(int i = -n; ; i++)
            if( now[i] ){ is = i; break; }

        for(int j = -n; ; j++)
            if( p[j] ){ js = j; break; }

        for(int i = is; i <= ie; i++)
            for(int j = js; j <= je; j++)
                if( now[i] && p[j] )
                    re[i+j] += now[i]*p[j];

        carry(re);
        return re;
    }

    char* str()
    {
        ll &now = *this;
        char *c = new char[2002];

        for(int i = 2001; i >= 0; i--)
            c[2001-i] = _num[i]+'0';

        c[2001-n] = '.';

        for(int i = 2000; i >= 0; i--)
            if( c[i] == '.' )
            {
                c[i] = '\0';
                break;
            }
            else if( c[i] != '0' )
            {
                c[i+1] = '\0';
                break;
            }


        for(int i = 0; i <= 2000; i++)
            if( c[i] != '0' ) return c+i;
    }
};

char p[10]; int x;

int main()
{
    while( scanf("%s %d", p, &x) != EOF )
    {
        ll a = (char*)"1";

        for(int xi = 0; xi < x; xi++)
            a = a*p;

        printf("%s\n", a.str());
    }
}

沒有留言:

張貼留言