poj 2109 Power of Cryptography

題意:給出n和p,求出 k=pn(0<p10101,0<n200,1k109)

分析:我只想到二分+高精度

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

struct bigInteger {
    int x[300];
    bigInteger() {
        memset(x, 0, sizeof(x));
        x[0] = 1;
    }
    bigInteger(int _x) {
        x[0] = 0;
        do {
            x[++x[0]] = _x % 10;
            _x /= 10;
        }while(_x);
    }
    void reset(int _x) {
        x[0] = 0;
        do {
            x[++x[0]] = _x % 10;
            _x /= 10;
        }while(_x);
    }
    bigInteger(char s[]) {
        x[0] = strlen(s);
        for(int i = x[0] - 1, j = 1; i >= 0; i--, j++) {
            x[j] = s[i] - '0';
        }
    }
    bigInteger operator * (const bigInteger y) {
        if(x[0] == 1 && x[1] == 0) return *this;
        if(y.x[0] == 1 && y.x[1] == 0) return y;
        if(x[0] == 1 && x[1] == 1) return y;
        if(y.x[0] == 1 && y.x[1] == 1) return *this;
        bigInteger z;
        for(int i = 1; i <= y.x[0]; i++)
            for(int j = 1; j <= x[0]; j++) {
                int k = i + j - 1;
                z.x[k] += y.x[i] * x[j];
                if(k > z.x[0]) z.x[0] = k;
                while(z.x[k] > 9) {
                    z.x[k + 1] += z.x[k] / 10;
                    z.x[k++] %= 10;
                    if(k > z.x[0])
                        z.x[0] = k;
                }
            }
        return z;
    }
    void print() {
        for(int i = x[0]; i > 0; i--) {
            printf("%d", x[i]);
        }
        puts("");
    }
};

int cmp(const bigInteger &a, const bigInteger &b) {
    if(a.x[0] < b.x[0]) return -1;
    else if(a.x[0] > b.x[0]) return 1;
    else {
        for(int i = a.x[0]; i > 0; i--) {
            if(a.x[i] != b.x[i]) {
                return a.x[i] - b.x[i];
            }
        }
        return 0;
    }
}

char s[200];
int n;

int main() {
    while(scanf("%d%s", &n, s) != EOF) {
        bigInteger p(s), a, b;
        int l = 1, r = 1000000000, nosul = 1;
        while(l <= r) {
            int m = (l + r) >> 1, i;
            a.reset(1);
            b.reset(m);
            for(i = 0; i < n && cmp(a, p) <= 0; i++) {
                a = a * b;
            }
            int jud = cmp(a, p);
            //a.print();
            //cout << l << ' ' << r << ' ' << m << ' ' << jud << endl;
            if(jud == 0) {
                printf("%d\n", m);
                nosul = 0;
                break;
            }
            else if(jud > 0) {
                r = m - 1;
            }
            else {
                l = m + 1;
            }
        }
        if(nosul) {
            printf("%d\n", r);
        }
    }
    return 0;
}

下面是高端解法:

http://blog.csdn.net/synapse7/article/details/11672691

發佈了31 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章