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秒杀实战 (六) 服务级高并发秒杀优化(RabbitMQ+接口优化)
转自:https://blog.csdn.net/qq_41305266/article/details/81146716 一.思路:减少数据库访问 1.系统初始化,把商品库存数量加载到Redis 2 ...
- mysql启动失败“MySQL Daemon failed to start”
CentOS上,用命令:service mysqld restart 启动mysql报错: # service mysqld restart Stopping mysqld: [ OK ] MySQL ...
- 解释mysql 语句
一.在我们创建mysql数据库的时候我们经常会用到这句SQL: CREATE DATABASE TEST DEFAULT CHARACTER SET utf8 COLLATE utf8_general ...
- 简单介绍 Java 构造器
导读 构造器是编程的强大组件.使用它们来释放 Java 的全部潜力. 在开源.跨平台编程领域,Java 无疑(?)是无可争议的重量级语言.尽管有许多伟大的跨平台框架,但很少有像 Java 那样统一和直 ...
- 05_ Flume多级Agent之间串联案例
多级agent之间串联: 从tail命令获取数据发送到avro端口,另一个节点可配置一个avro源来获取数据,发送外部存储 启动两个Agent节点: 使用scp命令拷贝flume安装包到另一台虚拟机; ...
- Linux查找并杀死僵尸进程(转)
1.查看系统是否有僵尸进程 使用Top命令查找,当zombie前的数量不为0时,即系统内存在相应数量的僵尸进程. 2.定位僵尸进程 使用命令ps -A -ostat,ppid,pid,cmd |gre ...
- 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)
7-5 Tree Traversals Again (25 分) An inorder binary tree traversal can be implemented in a non-recu ...
- CentOS7 - Package does not match intended download 问题解决
yum 安装软件,有时会出现 Error: Package does not match intended download,这时需要彻底清除已有的下载,然后重新安装即可. $ sudo yum cl ...
- jenkins"控制台输出"乱码问题解决
今天在搭建Jenkins环境时,安装完Tomcat.Jenkins.创建项目进行构建后,在查看控制台输出时,结果中文全部显示乱码.然后呢,就是漫长的解决历程,最终呢,解决乱码问题的时间终于超过了环境搭 ...
- P5650 基础字符串练习题
设定'0'权值为1,设定'1'权值为-1 然后就是最大子段和 #include <cstdio> #include <algorithm> #include <cstri ...