poj 2109 高精度幂和二分查找
来源:互联网 发布:大阪旅游攻略 知乎 编辑:程序博客网 时间:2024/04/30 13:00
Power of Cryptography
Time Limit: 1000MS Memory Limit: 30000KTotal 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 nth. power, for an integer k (this integer is what your program must find).
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 nth. 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<10101 and there exists an integer k, 1<=k<=109 such that kn = 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 163 277 4357186184021382204544
Sample Output
431234
Source
México and Central America 2004
本题的主要思路在实现高精度大整数+二分查找。要注意的是数据中有可能存在无解的情况,所以要所求得的高精度向下取整。#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;}
0 0
- poj 2109 高精度幂和二分查找
- POJ 3122 Pie 二分查找 +高精度
- poj 1064 高精度 二分
- Expanding Rods+二分高精度+POJ
- POJ 2109 Power of Cryptography 二分+高精度 【未AC】
- POJ 3122 二分查找
- poj 3273 二分查找
- poj 3122 水高精度二分 Pie
- poj 3122 Pie (高精度+二分)
- poj 1064 一道高精度二分题
- poj 2318 直线和点位置关系(二分查找)
- 二分查找和二分排序
- poj-1001 高精度幂
- POJ 1001 高精度幂
- 【二分查找】-POJ-2002-Squares
- poj 3122 Pie (二分查找)
- Pie(POJ--3122【二分查找】
- POJ 3061 Subsequence (二分查找)
- 游戏引擎开发系列——消息循环篇
- 苏州理财:收益率宣传低调多了
- SQL使用中一些不为人注意的技巧
- jvisualvm监测JVM
- Java排序算法2:选择排序
- poj 2109 高精度幂和二分查找
- 类似微信陌陌app百万级并发系统源码转让
- 数字图像处理的就业前景
- Android Emulator and Weinre
- Java模拟登录微信平台,主动推送消息给用户,自动绑定url、token等
- Python Twisted介绍
- CentOS-min 操作记录(5)-配置自己的VPN
- U盘UEFI启动制作方法
- DIV滚动条一直居底