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: ...
随机推荐
- elment-UI中表头和内容错位
当出现纵向滚动条的时候就错位了 网上找了很多方法发现对我的不生效 //把这样式添加到index.html中.或者app.vue中(必须是入口文件,才能全局起作用!)body .el-table th. ...
- try,except,finally的用法
try,except,finally try...except形式:指定一个或多个异常处理器(异常子句).. 当在try子句中没有异常发生时,,异常处理器将不被执行. 当在try子句中有异常发生时,首 ...
- 一秒钟教会你如何 使用jfreechart制作图表,扇形图,柱形图,线型图,时序图,附上详细代码,直接看效果
今天有小伙伴问到我怎么使用jfreeChat生成图标,去年就有一个这方便的的总结,今天再遇到,就总结出来,供大家参考: 第一个: 创建柱状图,效果图如下: 柱状图代码如下: package cn.xf ...
- C#面向对象(五大基本原则 )
五大原则 单一职责原则(SRP)开放封闭原则(OCP) 里氏替换原则(LSP) 依赖倒置原则(DIP) 接口隔离原则(ISP) 一.单一职责原则SRP(Single Responsibility P ...
- 【Mac】 开启原生的 NTFS 硬盘格式支持
一.MacOS 10.13 之前 二.MacOS 10.13 及之后 一.MacOS 10.13 之前 直接跳到引用地址查看,下面的草记只是为了防止链接丢失 引用地址 打开终端 切换至root身份,输 ...
- 【python】多进程、多线程、序列
一.多进程 1.子进程永远返回0,而父进程返回子进程的ID.这样做的理由是,一个父进程可以fork出很多子进程,所以,父进程要记下每个子进程的ID,而子进程只需要调用getppid()就可以拿到父进程 ...
- JavaSpring【二、IOC】
概述: 接口及面向接口编程 接口:用于沟通的中介物的抽象,实体把自己提供给外界的方法的抽象化说明,将声明和实现分离,使其能够改变内部而不影响与外部的交互方式 面向接口编程:在结构设计中,分清层次及调用 ...
- Computer Vision_1_Active Appearance Models :Active Appearance Models——1998
此为计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面. 1. Active Appearance Models 活动表观模型和活动轮廓模型基本思想来源 Snake,现在 ...
- C和指针--编程题9.14第10小题--判断回文函数
题目: 编写函数 int palindrom( char *string); 如果参数字符串是个回文,函数就返回真,否则就返回假.回文就是指一个字符串从左向右读和从右向左读是一样的.函数应忽略所有的非 ...
- Hive2.0常用函数(对编辑器很无语😓)
Hive内部提供了很多函数给开发者使用,包括数学函数,类型转换函数,条件函数,字符函数,聚合函数,表生成函数等等,这些函数都统称为内置函数. 参考:https://cwiki.apache.org/c ...