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: ...
随机推荐
- CSS 常用语法与盒模型分析
CSS基础知识 CSS规则由两个主要的部分构成:选择器,以及一条或者多条声明 selector { property: value; property: value; ... property: va ...
- jquery hover最后解决 - 不再疑惑 - 例子在这里
hover具有动画累计的bug, 可以使用 stop 或 filter(:not(:animated))来消除, 但是, 即使这样, 当鼠标反复滑入或滑出的时候, 虽然没有动画累计的问题, 但是 下面 ...
- Springboot2.x 拦截器
一,单个拦截器,实现接口 HandlerInterceptor @Component public class MyInterceptor1 implements HandlerIntercepto ...
- 51nod 1021 石子归并 区间DP
1021 石子归并 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 取消关注 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆 ...
- 51nod 1266 蚂蚁
蚂蚁这道题 就是 不管两只蚂蚁相撞 他们会朝自己的反方向走 不过可以这么想 有蚂蚁1 和 蚂蚁2 并且相向而行 如果撞了以后 蚂蚁1和蚂蚁2 就往回走 ,这里可以理解成蚂蚁1,蚂蚁2 继续 ...
- MariaDB / MySQL数据类型
MariaDB / MySQL 数据类型 有三种主要的类型:Text(文本).Number(数字)和 Date/Time(日期/时间)类型. Text 类型: 数据类型 描述 CHAR(size) 保 ...
- python 元组列表转为字典
#create a list l = [(), (), (), (), (), ()] d = {} for a, b in l: d.setdefault(a, []).append(b) prin ...
- shell 注释
以 # 开头的行就是注释,会被解释器忽略. 通过每一行加一个 # 号设置多行注释,像这样: #-------------------------------------------- # 这是一个注释 ...
- YOLO V3论文理解
YOLO3主要的改进有:调整了网络结构:利用多尺度特征进行对象检测:对象分类用Logistic取代了softmax. 1.Darknet-53 network在论文中虽然有给网络的图,但我还是简单说一 ...
- 一步一步实现JS拖拽插件
js拖拽是常见的网页效果,本文将从零开始实现一个简单的js插件. 一.js拖拽插件的原理 常见的拖拽操作是什么样的呢?整过过程大概有下面几个步骤: 1.用鼠标点击被拖拽的元素 2.按住鼠标不放,移动鼠 ...