POJ 2109 Power of Cryptography 大数,二分,泰勒定理 难度:2
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static BigInteger p,l,r,div;
static int n;
public static int cmp(BigInteger mid){
BigInteger sum=mid.pow(n);
return sum.compareTo(p);
}
public static BigInteger calc(){
l=BigInteger.ZERO;
r=BigInteger.valueOf(1000000000);
BigInteger div=BigInteger.valueOf(2);
while(l.compareTo(r)<0){
BigInteger mid=l.add(r).divide(div);
int fl=cmp(mid);
if(fl==0){
return mid;
}
else if(fl==-1){
l=mid.add(BigInteger.ONE);
}
else r=mid;
}
int fl=0;
if((fl=cmp(r))==0)return r;
if(fl==-1){
while(p.subtract(r.pow(n)).compareTo(BigInteger.ONE)>0)r=r.add(BigInteger.ONE);
return r;
}
else {
while(r.pow(n).subtract(p).compareTo(BigInteger.ONE)>0)r=r.subtract(BigInteger.ONE);
return r;
}
}
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
n=scanner.nextInt();
p=scanner.nextBigInteger();
BigInteger ans=calc();
System.out.println(ans);
}
}
}
有种更为优雅的姿势
#include <cstdio>
#include <cmath> int main()
{
double n , m ;
int ans ;
while ( scanf( "%lf%lf" , &m , &n ) != EOF )
printf( "%.0f\n" , exp(log(n)/m) ) ;
}
另附大神证明思路:泰勒公式证明相差不会超过9
http://blog.csdn.net/synapse7/article/details/11672691
POJ 2109 Power of Cryptography 大数,二分,泰勒定理 难度:2的更多相关文章
- 贪心 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: 26622 Accepted: ...
- 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
点击打开链接 Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16388 Ac ...
- 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 (double 精度)
题目:http://poj.org/problem?id=2109 题意:求一个整数k,使得k满足kn=p. 思路:exp()用来计算以e为底的x次方值,即ex值,然后将结果返回.log是自然对数,就 ...
- 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: ...
随机推荐
- mac下的一些操作
mac 下修改Hosts文件 : http://www.cnblogs.com/zhangqs008/p/3773623.html mac下装Tomcat服务器: 在苹果系统安装Tomcat:首先下载 ...
- Eclipse的快捷键使用总结
最近一直在使用Idea开发项目,导致之前一直使用的Eclipse快捷键忘记的差不多了,现在稍微整理了一些,方便以后可以快速切换回来. 常用的Eclipse快捷键总结: Ctrl+S 保存当前正在编辑的 ...
- 完美解决vim在终端不能复制的问题
以前 用xshell,或者其他工具ssh到远程服务器,vim不能复制,搜索说是vim的 -xterm_clipboard没有开启. 今天发现,至少鼠标复制是不需要这个东东的! 在stackoverfl ...
- linux下安装微信小程序开发工具
一.环境:: ubuntu 16.04 二.安装过程: 2.1 安装wine sudo apt-get install wine 2.2 安装nwjs-sdk 2.2.1 下载linux版nwjs-s ...
- 分布式系统一致性协议--Paxos算法
Paxos: Paxos算法背景介绍: Paxos算法是分布式技术大师Lamport提出的,主要目的是通过这个算法,让参与分布式处理的每个参与者逐步达成一致意见.用好理解的方式来说,就是在一个选举过程 ...
- UVa 10382 喷水装置(贪心)
https://vjudge.net/problem/UVA-10382 题意: 有一个长为l,宽为w的草坪,在其中心线不同位置有n个点状的喷水装置,喷水坐标为p,喷水半径为r.求喷到所有草坪的最少喷 ...
- 获取公网IP地址
https://ipip.yy.com/get_ip_info.php http://pv.sohu.com/cityjson?ie=utf-8 http://www.ip168.com/json.d ...
- Linux——shell简单学习(一)
首先来一个小程序,来查看所在目录,以及该目录下的文件 #!/bin/sh # “#!”指定用sh执行shell脚本 #this is to show workstation # “#” 表示注释 ec ...
- java 基本数据类型及自动类型提升
1.Java的8种基本数据类型及其所占空间大小: boolean 8bit/1byte byte 8bit/1byte char 16bit/2byte ...
- dataframe使用笔记
dates=pd.date_range(',periods=6) #创建固定频度的时间序列 df=pd.DataFrame(np.random.randn(6,4),index=dates,colum ...