Power of Cryptography

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 26622   Accepted: 13301

Description

Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers among these primes. Work in this area has resulted in the practical use of results from number theory and other branches of mathematics once considered to be only of theoretical interest. 
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

The input consists of a sequence of integer pairs n and p with each integer on a line by itself. For all such pairs 1<=n<= 200, 1<=p<10101 and there exists an integer k, 1<=k<=109 such that kn = p.

Output

For each integer pair n and p the value k should be printed, i.e., the number k such that k n =p.

Sample Input

2 16
3 27
7 4357186184021382204544

Sample Output

4
3
1234

Source

 
题意:
求一个整数k,使得k满足kn=p。
 
题意分析:
1)
 1<=n<= 200, 1<=p<10101 ,1<=k<=109 , kn = p. 
最直观的解题思路是使用 高精度算法,但是这道题可以使用double巧妙地避开。

用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的更多相关文章

  1. 贪心 POJ 2109 Power of Cryptography

    题目地址:http://poj.org/problem?id=2109 /* 题意:k ^ n = p,求k 1. double + pow:因为double装得下p,k = pow (p, 1 / ...

  2. poj 2109 Power of Cryptography

    点击打开链接 Power of Cryptography Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16388   Ac ...

  3. POJ 2109 Power of Cryptography 数学题 double和float精度和范围

    Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21354 Accepted: 107 ...

  4. poj 2109 Power of Cryptography (double 精度)

    题目:http://poj.org/problem?id=2109 题意:求一个整数k,使得k满足kn=p. 思路:exp()用来计算以e为底的x次方值,即ex值,然后将结果返回.log是自然对数,就 ...

  5. POJ - 2109 Power of Cryptography(高精度log+二分)

    Current work in cryptography involves (among other things) large prime numbers and computing powers ...

  6. POJ 2109 Power of Cryptography【高精度+二分 Or double水过~~】

    题目链接: http://poj.org/problem?id=2109 参考: http://blog.csdn.net/code_pang/article/details/8263971 题意: ...

  7. POJ 2109 Power of Cryptography 大数,二分,泰勒定理 难度:2

    import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger p,l,r,d ...

  8. 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 ...

  9. POJ 2109 :Power of Cryptography

    Power of Cryptography Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18258   Accepted: ...

随机推荐

  1. vue学习(10)-vue-resource

    下载:cnpm i vue-resource --save 在main.js导入包:import VueResource from 'vue-resource' 安装:Vue.use(VueResou ...

  2. 6.移动端自动化测试-小知识 if __name__==’__main__:是什么意思?

    1 引言 在Python当中,如果代码写得规范一些,通常会写上一句“if __name__==’__main__:”作为程序的入口,但似乎没有这么一句代码,程序也能正常运行.这句代码多余吗?原理又在哪 ...

  3. 【Day4】2.详解Http请求协议

    Http请求协议

  4. 01_Redis简述

    一:关系型数据库和非关系型数据库的区别: 1:关系型数据库(SQL):数据和数据之间,表和字段之间,表和表之间是存在关系的: 优点:数据之间有关系,进行数据的增删改查时非常方便的:关系型数据库有事务操 ...

  5. PAT Basic 1083 是否存在相等的差 (20 分)

    给定 N 张卡片,正面分别写上 1.2.…….N,然后全部翻面,洗牌,在背面分别写上 1.2.…….N.将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否存在相等的差? 输入格式: ...

  6. Python:出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 0: invalid continuation byte问题

    我在导入一个csv文件的时候出现了一个问题 报错的内容是这样的: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in positio ...

  7. JavaScript中定义类的方式详解

    本文实例讲述了JavaScript中定义类的方式.分享给大家供大家参考,具体如下: Javascript本身并不支持面向对象,它没有访问控制符,它没有定义类的关键字class,它没有支持继承的exte ...

  8. Map遍历效率 : entrySet > keySet

     1    //entrySet()  2     for (Entry<String, String> entry : map.entrySet()) {  3         Stri ...

  9. avcodec_decode_video2函数

    转自 https://www.xuebuyuan.com/2156374.html 该函数的作用是实现压缩视频的解码.在avcodec.h中的声明方式如下: int avcodec_decode_vi ...

  10. httpclient发邮件

    package com.chuanglan; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.L ...