POJ 2109 Power of Cryptography【高精度+二分 Or double水过~~】
题目链接:
http://poj.org/problem?id=2109
参考:
http://blog.csdn.net/code_pang/article/details/8263971
题意:
给定n,p,求k使得kn=p(1≤n≤200, 1≤p<10101, 1≤k≤109)
分析:
高精度+二分~~
k的位数为p的位数除以n的向上取整,这样知道k的位数便可以在范围内二分了~注意这里的答案是向下取整~~
代码:
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
#define MAXN 9999
#define MAXSIZE 1000
#define DLEN 4
const int maxn = 205;
char p[maxn];
class BigNum
{
private:
int a[500];
int len;
public:
BigNum(){
len = 1;
memset(a, 0 ,sizeof(a));
}
BigNum(const int);
BigNum(const char*);
BigNum(const BigNum &);
BigNum &operator = (const BigNum &);
BigNum operator +(const BigNum &)const ;
BigNum operator*(const BigNum &)const ;
BigNum operator^(const int &)const ;
bool operator >(const int &)const;
bool operator >(const BigNum &T)const;
void print();
};
BigNum::BigNum(const int b)
{
int c, d = b;
len = 0;
while(d > MAXN){
c = d % (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const char*s)
{
int t, k, index;
memset(a,0,sizeof(a));
int l = strlen(s);
len = l / DLEN;
if(l % DLEN)
len++;
index=0;
for(int i = l - 1; i >= 0; i -= DLEN){
t = 0;
k = i - DLEN + 1;
if(k<0) k=0;
for(int j = k; j <= i; j++)
t = t * 10 + s[j] - '0';
a[index++] = t;
}
}
BigNum::BigNum(const BigNum & T) : len(T.len)
{
memset(a, 0, sizeof(a));
for(int i = 0 ; i < len ; i++)
a[i] = T.a[i];
}
BigNum & BigNum::operator=(const BigNum & n)
{
len = n.len;
memset(a, 0, sizeof(a));
for(int i = 0 ; i < len ; i++)
a[i] = n.a[i];
return *this;
}
BigNum BigNum::operator+(const BigNum & T) const
{
BigNum t(*this);
int big; //位数
big = T.len > len ? T.len : len;
for(int i = 0; i < big; i++){
t.a[i] +=T.a[i];
if(t.a[i] > MAXN){
t.a[i + 1]++;
t.a[i] -=MAXN+1;
}
}
if(t.a[big] != 0) t.len = big + 1;
else t.len = big;
return t;
}
BigNum BigNum::operator*(const BigNum & T) const
{
BigNum ret;
int up;
int temp,temp1;
int i, j;
for(i = 0 ; i < len ; i++){
up = 0;
for(j = 0 ; j < T.len ; j++){
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN){
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else{
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
BigNum BigNum::operator^(const int & n) const
{
BigNum t,ret(1);
if(n < 0) exit(-1);
if(n == 0) return 1;
if(n == 1) return *this;
int m = n;
int i;
while(m > 1){
t = *this;
for(i = 1; i<<1<=m; i <<= 1)
t = t * t;
m -= i;
ret = ret * t;
if(m == 1) ret=ret*(*this);
}
return ret;
}
bool BigNum::operator>(const BigNum & T) const
{
int ln;
if(len > T.len)
return true;
else if(len == T.len){
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0)
ln--;
if(ln >= 0 && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigNum::operator >(const int & t) const
{
BigNum b(t);
return *this>b;
}
void BigNum::print()
{
printf("%d",a[len-1]);
for(int i = len - 2 ; i >= 0 ; i--) printf("%04d",a[i]);
printf("\n");
}
int main(void)
{
int n, len, MIN, MAX, MID;
while(~scanf("%d%s", &n, &p)){
len = (int)ceil((double)strlen(p) / n);
MIN = 1, MAX = 9;
for(int i = 0; i < len - 1; i++){
MAX *= 10;
MAX += 9;
MIN *= 10;
}
while(MIN < MAX){//[]
MID = (MIN + MAX) / 2;
if(BigNum(p) > (BigNum(MID) ^ n)) MIN = MID +1;
else if((BigNum(MID) ^ n) > BigNum(p)) MAX = MID - 1;
else break;
}
if(MAX == MIN) MID = MIN;
if((BigNum(MID) ^ n) > BigNum(p)) MID--;
printf("%d\n",MID);
}
return 0;
}
代码:
double类型能表示10−307到10308, 足够这个题用。
而double超过16位后面都变成0,这样正好满足向下取整。
12337=4332529576639313702577
12347=4357186184021382204544
12357=4381962969567270546875
值不同的地方(从高到低第三位)没有超过double的精度,所以不会导致错误答案~
#include<iostream>
#include<cmath>
using namespace std;
int main (void)
{
double n, m;
while(cin>>n>>m){
cout<<pow(m, 1/n)<<endl;
}
return 0;
}
//或者
#include<iostream>
#include<cmath>
using namespace std;
int main (void)
{
double n, p;
while(cin>>n>>p){
cout<< exp(log(p)/n) <<endl;
}
return 0;
}
//pow明显更快,只是想说明有时候取对数也是个不错的方法~
POJ 2109 Power of Cryptography【高精度+二分 Or double水过~~】的更多相关文章
- POJ - 2109 Power of Cryptography(高精度log+二分)
Current work in cryptography involves (among other things) large prime numbers and computing powers ...
- POJ 2109 Power of Cryptography 大数,二分,泰勒定理 难度:2
import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger p,l,r,d ...
- 贪心 POJ 2109 Power of Cryptography
题目地址:http://poj.org/problem?id=2109 /* 题意:k ^ n = p,求k 1. double + pow:因为double装得下p,k = pow (p, 1 / ...
- POJ 2109 -- Power of Cryptography
Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26622 Accepted: ...
- POJ 2109 Power of Cryptography 数学题 double和float精度和范围
Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21354 Accepted: 107 ...
- poj 2109 Power of Cryptography
点击打开链接 Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16388 Ac ...
- poj 2109 Power of Cryptography (double 精度)
题目:http://poj.org/problem?id=2109 题意:求一个整数k,使得k满足kn=p. 思路:exp()用来计算以e为底的x次方值,即ex值,然后将结果返回.log是自然对数,就 ...
- Poj 2109 / OpenJudge 2109 Power of Cryptography
1.Link: http://poj.org/problem?id=2109 http://bailian.openjudge.cn/practice/2109/ 2.Content: Power o ...
- POJ 2109 :Power of Cryptography
Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18258 Accepted: ...
随机推荐
- Angular JS中变量定义的基本原则
在Angular JS开发中,经常需要定义一些变量,关于这些变量的定义方法及作用域应该注意以下几点: 1. 如果能用局部变量解决问题,尽量不要用全局变量. 2. 需要与界面双向绑定的变量采用$scop ...
- 指针-动态开点&合并线段树
一个知识点不在一道题里说是没有灵魂的 线段树是用来处理区间信息的咯 但是往往因为需要4倍空间让许多人退却,而动态开点的线段树就非常棒 仿佛只用2倍就可以咯 指针保存位置,即节点信息,是很舒适的,所以用 ...
- 暑假集训 || 区间DP
区间DP 经典石子合并问题V1 复杂度 On3 int a[SZ], sum[SZ], f[SZ][SZ]; int main() { int n; scanf("%d", ...
- ios 自定义URL Scheme 设计
在 iOS 里,程序之间都是相互隔离,目前并没有一个有效的方式来做程序间通信,幸好 iOS 程序可以很方便的注册自己的 URL Scheme,这样就可以通过打开特定 URL 的方式来传递参数给另外一个 ...
- shim和polyfill的区别
今天看vue的响应原理,突然被提到shim这个词,翻阅些许资料然后整理出以下这些内容. 在JavaScript的世界里,有两个词经常被提到,shim和polyfill. 首先理解这两个词之前我们先来了 ...
- 洛谷P1421 小玉买文具
这道题其实就是编程最基础的逻辑,没什么好讲的输入,输出就完了,非常简单! code: #include<cstdio> #include<iostream> using nam ...
- POJ-3278 抓住这头牛
广搜解决. 广搜搜出最短路,直接输出返回就行了. 每个点只搜一次,而且界限进行一次判断. else 语句里面不要用if else if,这样的话就直走一条路了. #include <ios ...
- 串的基本操作(KMP算法实现)
#include <iostream> #include <math.h> using namespace std; void StrAssign(char *T) { cha ...
- 10. GLOBAL_STATUS 与 SESSION_STATUS
10. GLOBAL_STATUS 与 SESSION_STATUS 注意 从MySQL 5.7.6开始,show_compatibility_56系统变量的值会影响此处描述的表中的可用信息. 有关详 ...
- Java:post请求
文章来源:https://www.cnblogs.com/hello-tl/p/9140870.html 0.post请求返回json import java.io.BufferedInputStre ...