poj 2109 Power of Cryptography

6 篇文章 0 订阅
1 篇文章 0 订阅

题意:给出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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值