poj 2109 高精度幂和二分查找

该博客详细介绍了如何解决POJ 2109问题,涉及高精度计算中的幂运算以及利用二分查找优化算法的过程,通过实例输入输出展示了解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Power of Cryptography
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 17712 Accepted: 8933

Description

Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers among these primes. Work in this area has resulted in the practical use of results from number theory and other branches of mathematics once considered to be only of theoretical interest. 
This problem involves the efficient computation of integer roots of numbers. 
Given an integer n>=1 and an integer p>= 1 you have to write a program that determines the n th positive root of p. In this problem, given such integers n and p, p will always be of the form k to the n th. power, for an integer k (this integer is what your program must find).

Input

The input consists of a sequence of integer pairs n and p with each integer on a line by itself. For all such pairs 1<=n<= 200, 1<=p<10 101 and there exists an integer k, 1<=k<=10 9 such that k n = p.

Output

For each integer pair n and p the value k should be printed, i.e., the number k such that k n =p.

Sample Input

2 16
3 27
7 4357186184021382204544

Sample Output

4
3
1234

Source

本题的主要思路在实现高精度大整数+二分查找。要注意的是数据中有可能存在无解的情况,所以要所求得的高精度向下取整。

#include <iostream>
#include <string>
#include <sstream>
class BigInteger
{
public:
    BigInteger():s("0"){}
    BigInteger(const std::string & ss):s(ss.rbegin(),ss.rend()){}
    BigInteger(int ii);
    ~BigInteger(){}
    friend std::ostream& operator <<(std::ostream &, const BigInteger &);
    friend BigInteger operator +(const BigInteger &, const BigInteger &);
    friend BigInteger operator *(const BigInteger &, const BigInteger &);
    friend BigInteger operator -(const BigInteger &, const BigInteger &);
    friend bool operator > (const BigInteger &, const BigInteger &);
    friend bool operator < (const BigInteger &, const BigInteger &);
    friend bool operator >= (const BigInteger &, const BigInteger &);
    friend bool operator <= (const BigInteger &, const BigInteger &);
    bool operator == (const BigInteger &);
    bool operator != (const BigInteger &);
    BigInteger& operator ++();
    BigInteger operator ++ (int);
    BigInteger divideby2();
    BigInteger pow(int n);
    std::string::size_type getDigitCount() const{return s.size();}
private:
    std::string s;
    void reverse();
};
typedef std::string::size_type sz_type;
BigInteger::BigInteger(int ii)
{
    std::ostringstream osformat;
    osformat << ii; 
    std::istringstream is(osformat.str());
    is >> s;
    reverse();
}
void BigInteger::reverse()
{
   
    sz_type i = 0, k = s.size() - 1; 
    while(i < k)
    {
        char c = s[i];
        s[i] = s[k];
        s[k] = c;
        i++;
        k--;
    }
}
std::ostream& operator <<(std::ostream & os, const BigInteger & b)
{
    std::string s(b.s.rbegin(),b.s.rend());
    os << s;
    return os;
}
BigInteger operator +(const BigInteger &a, const BigInteger &b)
{
    BigInteger c;
    c.s.clear();
    sz_type sizeA = a.s.size();
    sz_type sizeB = b.s.size();
    sz_type size = sizeA < sizeB ? sizeA : sizeB;
    int add = 0;
    int bit = 0;
    sz_type i = 0;
    for(i = 0; i != size; i++)
    {
        bit = a.s[i] - '0' + b.s[i] - '0' + add;
        c.s.push_back((char)(bit % 10 + '0'));
        add = bit / 10;
    }
    while(i < sizeA)
    {
        bit = a.s[i] - '0' + add;
        c.s.push_back((char)(bit % 10 + '0'));
        add = bit / 10;
        i++;
    }
    while(i < sizeB)
    {
        bit = b.s[i] - '0' + add;
        c.s.push_back((char)(bit % 10 + '0'));
        add = bit / 10;
        i++;
    }
    if(add > 0)
        c.s.push_back(char(add + '0'));
    return c;
}
BigInteger operator *(const BigInteger &a, const BigInteger &b)
{
    std::string c(a.s.size() + b.s.size(), '0');
    sz_type sizeA = a.s.size();
    sz_type sizeB = b.s.size();
    int add = 0;
    int bit = 0;
    for(sz_type i = 0; i < sizeA; i++)
    {
        add = 0;
        for(sz_type j = 0; j < sizeB; j++)
        {
            bit = (a.s[i] - '0') * (b.s[j] - '0') + add + c[j + i] - '0';
            add = bit / 10;
            bit = bit % 10;
            c[j + i] = bit + '0';
        }
        c[i + sizeB] = add + '0';
    }
    while(c.size() > 1 && *c.rbegin() == '0')
        c.erase(c.end() - 1);
    BigInteger big;
    big.s = c;
    return big;
}
bool BigInteger::operator == (const BigInteger &a)
{
    return s == a.s;
}
bool BigInteger::operator != (const BigInteger &a)
{
    return !(s == a.s);
}
BigInteger& BigInteger::operator ++()
{
    int add = 1;
    sz_type size = s.size();
    int bit = 0;
    for(sz_type i = 0; i < size; i++)
    {
        bit = s[i] - '0' + add; 
        s[i] = char(bit % 10 + '0');
        add = bit / 10;
    }
    if(add > 0)
        s.push_back(add + '0');
    return *this;
}
BigInteger BigInteger::operator ++ (int)
{
    BigInteger ret(*this);
    int add = 1;
    sz_type size = s.size();
    int bit = 0;
    for(sz_type i = 0; i < size; i++)
    {
        bit = s[i] - '0' + add; 
        s[i] = char(bit % 10 + '0');
        add = bit / 10;
    }
    if(add > 0)
        s.push_back(add + '0');
    return ret;
}
BigInteger operator - (const BigInteger &a, const BigInteger &b)
{
    BigInteger c;
    c.s.clear();
    std::string big,little;
    if(a > b)
    {
        big = a.s;
        little = b.s;
    }
    else
    {
        big = b.s;
        little = a.s;
    }
    int minus = 0;
    typedef std::string::size_type sz_type;
    sz_type sizeli = little.size();
    int bit;
    sz_type i = 0;
    for(i = 0; i != sizeli; i++)
    {
        bit = big[i] - little[i] + minus;
        if(bit >= 0)
           minus = 0;
        else
        {
           minus = -1;
           bit = bit + 10;
        }
        c.s.push_back(bit + '0');    
    }
    sz_type sizela = big.size();
    while(i < sizela)
    {
        bit = big[i] - '0' + minus;
        if(bit < 0)
        {
            minus = -1;
            bit = bit + 10;
        }
        else
            minus = 0;
        c.s.push_back(bit + '0');
        i++;
    }
    while(c.s.size() > 1 && *c.s.rbegin() == '0')
    {
        c.s.erase(c.s.end() - 1);
    }  
    return c;
}
bool operator > (const BigInteger &a, const BigInteger &b)
{
    if(a.s.size() == b.s.size())
    {
        std::string as(a.s.rbegin(), a.s.rend());
        std::string bs(b.s.rbegin(), b.s.rend());
        return as > bs;
    }
    return a.s.size() > b.s.size();
}
bool operator < (const BigInteger &a, const BigInteger &b)
{
    if(a.s.size() == b.s.size())
    {
        std::string as(a.s.rbegin(), a.s.rend());
        std::string bs(b.s.rbegin(), b.s.rend());
        return as < bs;
    }
    return a.s.size() < b.s.size();
}
bool operator >= (const BigInteger &a, const BigInteger &b)
{
    return !(a < b);
}
bool operator <= (const BigInteger &a, const BigInteger &b)
{
    return !(a > b);
}
BigInteger BigInteger::divideby2()
{
    std::string ss;
    int bit = 0;
    int add = 0;
    for(std::string::const_reverse_iterator it = s.rbegin(); it != s.rend(); it++)
    {
        bit = add * 10 + *it - '0';
        add = bit % 2;
        bit = bit / 2;
        ss.push_back(bit + '0');
    }
    while(s.size() > 1 && *ss.begin() == '0')
    {
        ss.erase(ss.begin());
    }    
    return BigInteger(ss);
}
BigInteger BigInteger::pow(int n)
{
    BigInteger base = 1;
    for(int i = 0; i < n; i++)
    {
        base = base * *this;
    }
    return base;
}
BigInteger searchBydivide2(int n, const BigInteger &target)
{
    BigInteger source;
    int digitCount = target.getDigitCount();
    int bit = digitCount / n;
    if(digitCount % n != 0)
        bit = bit + 1;
    std::string start(bit - 1, '0');
    start.insert(start.begin(), '1');
    BigInteger left(start), right(std::string(bit, '9'));
    BigInteger mid;
    BigInteger rs;
    while(left <= right && left != 0 && right != 0)
    {
        mid = (left + right).divideby2();
        rs = mid.pow(n);
        if(rs == target)
            return mid;
        else if(rs > target)
            right = mid - 1;
        else
            left = mid + 1;
    }
    return right;  
}
using namespace std;
int main()
{
   int n;
   string s;
   BigInteger rs;
   while(cin >> n >> s)
   {
       rs = searchBydivide2(n, BigInteger(s));
       cout << rs << endl;
   }
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值