POJ 2109 -- Power of Cryptography
Power of Cryptography
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 26622 | Accepted: 13301 |
Description
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
Output
Sample Input
2 16
3 27
7 4357186184021382204544
Sample Output
4
3
1234
Source
用pow函数求解:
k = pow(p, 1.0/n)
double的取值范围为10^(-307)~10^308,但小数精度只有前16位, 其误差范围在10^(-15)的数量级左右.
这个误差级数仅会对n的小数部分存在影响,四舍五入后对整数部分是无影响的.
而题目已经限定了,n、k、p均是整数,因此使用公式法可以直接得到准确结果.
假若题目不存在整数限制,当n极大时,k会极小(无限迫近1,对小数精度极高),
此时公式法则会因为精度问题而失效.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double n,p;
while(cin>>n>>p)
{
double k;
k = pow(p,1.0/n);
cout<<k<<endl;;
}
return ;
}
2)使用 高精度算法 + 二分法
首先,想要求 kn = p的k,不使用如上计算方法的公式法,只能枚举k,进行高精度乘法。
寻找k的方法,可以使用二分法。
那k的范围是什么呢,考虑样例7 4357186184021382204544,p是22位,22/7=3~4,向上取整,所以p是一个四位数,即1000<=p<=9999。
在这个范围进行二分查找,就可以找到k。
关于高精度算法,看过一个博文,想了解详情的可以移步=》从零开始学算法:高精度计算
c++ / % 四舍五入 向上取整ceil 向下取整floor
#include<iostream>
#include<math.h>
#include<cstring>
#include<stdio.h>
using namespace std;
const int maxp = ;
//const int maxk = 12;
int p[maxp];
int k[maxp]; int Compare(int a[],int b[])
{///如果相等返回0,>返回1,<返回-1
if(a[0] > b[0]) return 1;
else if(a[] < b[]) return -;
else//位数相等,需要逐位判断
{
for(int i=a[];i>;i--)
{
if(a[i]>b[i]) return ;
else if(a[i] < b[i]) return -;
}
}
return ;
} void bigEqual(int n)
{///计算k^n,将结果存在k中
int temp[maxp];int Equal[maxp];
memset(Equal,,sizeof(Equal));//用来存放另一个乘数
memset(temp,,sizeof(temp));//用来存放每次相乘的结果
for(int i = ;i<=k[];i++)
Equal[i] = k[i];
for(int turn = ;turn<n;turn++)
{
for(int i=;i<=k[];i++)///计算k * Equal,存在temp中
{
for(int j=;j<=Equal[];j++)
{
temp[i+j-] += k[i]*Equal[j];
}
temp[] = Equal[]+k[]-;
for(int j=;j<=temp[];j++)///处理进位
{
if(temp[j]>=)
{
temp[j+] += temp[j]/;temp[j] = temp[j]%;
}
}
while(temp[temp[]+])
{
temp[]++;
temp[temp[]+] = temp[temp[]]/;
temp[temp[]] = temp[temp[]]%;
}
}
for(int m=;m<=temp[];m++)
Equal[m] = temp[m];//转存temp作为下一次的乘数
memset(temp,,sizeof(temp));
}
for(int i=;i<=Equal[];i++)
k[i] = Equal[i];
} int main()
{
char s[maxp];
int n;
while(scanf("%d %s",&n,&s) != EOF)
{
memset(p,,sizeof(p));
///将p处理成数组
p[] = strlen(s);//第一位存储p的位数
for(int i=p[]-;i>=;i--)
{
p[p[]-i] = s[i]-'';
}
int kLength = ceil((double)p[]/n);//向上取整
int Min = ,Max = ;
for(int i=;i<kLength;i++)
{
Min *=;
}
for(int i=;i<kLength;i++)
{
Max *=;Max += ;
}
///使用二分法查找
double Mid = (Min+Max)/;
for(int low = Min,up = Max;low<=up;)
{
memset(k,,sizeof(k));
///给k赋值为Mid
int i=;int temp = Mid;
while(temp)
{
k[i] = temp%;
temp = temp/;
i++;
}
k[] = i-;//k[0]存储k的长度
bigEqual(n);///计算k^n,将结果存储在k中
int j = Compare(k,p);
if(j == )//相等
break;
else if(j == )//k>p,向Mid的左侧查找
{
up = Mid-;Mid = (low+up)/;
}
else{//k<p,向Mid的右侧查找
low = Mid+;Mid = (low+up)/;
}
}
cout<<Mid<<endl;
}
return ;
}

告诫自己:

s不可以用String类型
string不可以用cin>>进行赋值
POJ 2109 -- Power of Cryptography的更多相关文章
- 贪心 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: 16388 Ac ...
- 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 (double 精度)
题目:http://poj.org/problem?id=2109 题意:求一个整数k,使得k满足kn=p. 思路:exp()用来计算以e为底的x次方值,即ex值,然后将结果返回.log是自然对数,就 ...
- 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【高精度+二分 Or double水过~~】
题目链接: http://poj.org/problem?id=2109 参考: http://blog.csdn.net/code_pang/article/details/8263971 题意: ...
- 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 / 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: ...
随机推荐
- Java和操作系统交互(Java 代码是怎么执行)(转)
结合 CPU 理解一行 Java 代码是怎么执行的 根据冯·诺依曼思想,计算机采用二进制作为数制基础,必须包含:运算器.控制器.存储设备,以及输入输出设备,如下图所示. 我们先来分析 CPU 的工作原 ...
- linux物理地址和虚拟地址
- cnblogs排版样式预览
说明:关于本博主题及样式来源于[GitHub]:本博总体排版目录样式风格参照博文[修仙成神之路]进行预览:参照本博设置可参考博文[设置跟本博一样的效果]本博之前发表过的博文存在样式不协调,后期会逐一完 ...
- Java NIO 学习笔记一
缓冲区操作 进程执行I/O操作,归结起来就是向操作系统发出请求,它要么把缓存区例的数据排干(写),要么用数据把数据区填满(读).进程使用这一机制处理所有数据进出操作. 进程使用read()系统调用,要 ...
- 作业九——DFA最小化
1.将DFA最小化:教材P65 第9题 I {1, 2, 3, 4, 5} {6, 7} {1, 2}b->{1, 2, 3, 4, 5} {3, 4}b->{6, 7} {5}b-> ...
- 通过轻量级终端工具Tera Term远程向linux操作系统上传war文件
通过轻量级终端工具Tera Term远程向linux操作系统上传war文件 1.打开Tera Term终端工具,并输入正确的远程机器的IP地址以及端口号: 2.输入正确的用户名和密码进入到linux操 ...
- Mac环境下使用Appium Inspector进行元素定位
一.摘要 本篇博文介绍在Mac系统上使用AppiumI Inspector进行App页面元素定位 二.Finding elements by xpath WebElement digit_9 = dr ...
- python 中 open与with open 的区别
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...
- 步骤五 · 4-9 解决getElementsByClassName()兼容性 未解决
前端零基础入门 2019版 / 步骤五 · 4-9 解决getElementsByClassName()兼容性
- POJ2182 Lost Cows 树状数组
题意:有编号1~n乱序排列的奶牛,给出了每一个奶牛前小于自己编号的奶牛数目 维护一个树状数组,下标是编号,值为$0/1$标识是否存在,很显然最后一个牛的编号是知道的,我们在树状数组上二分出前缀和为小于 ...