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: ...
随机推荐
- 3.IOC的配置与应用(annotation的方式)
自动装载 @Autowired public class UserService { private UserDAO userDAO; public UserDAO getUserDAO() { re ...
- Spring Data JPA 大纲归纳
第一天: springdatajpa day1:orm思想和hibernate以及jpa的概述和jpa的基本操作 day2:springdatajpa的运行原理以及基本操作 day3:多表操作,复杂查 ...
- 【1】Git基础
一.Git概念 1.1.Git定义 Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发 ...
- RobHess的SIFT代码解析之kd树
平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码:SIFT+KD ...
- JAVA中AES对称加密和解密以及与Python兼容
引言:本文主要解决Java中用AES加密及解密,同时可通过Python脚本对Java加密后的字符进行解密的操作. 由于近期工作中用到需要使用Java对一串密钥进行加密,并且后台通过Python语言读取 ...
- 学习笔记:安装swig+用SWIG封装C++为Python模块+SWIG使用说明
这段时间一直在摸索swing,用它来封装C++代码来生成python脚步语言.并总结了swing从安装到配置再到代码封装编译生成动态库的整个过程,下面这篇文章都是我在实际的运用中的一些经验总结,分享给 ...
- Hadoop_33_Hadoop HA的搭建
Hadoop HA的搭建,可参考链接:https://blog.csdn.net/mrbcy/article/details/64939623 说明: 1.在hadoop2.0中通常由两个Nam ...
- Tensorflow目录
0.Tensorflow安装 1.创建会话,启动会话 2.变量 3.Fech_feed 4.线性回归 5.非线性回归 6.MNIST数据集简单分类 7.交叉熵 8.Dropout 9.正则化 10.优 ...
- springboot2集成activiti出错
报一个反射错误 java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy 解决方案:http ...
- 字符串搜索(strStr)--- C++版
上篇中是用JAVA实现的字符串搜索算法, 这次改用C++来实现,当然在C++就没有像JAVA那样方便的API可以很简便的实现了,其思想跟上篇类似,直接上具体实现代码: 编译运行: 下面分析下流程: 还 ...