POJ - 2109 Power of Cryptography(高精度log+二分)
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 n th. 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
题意:给出n和p(n<=200,p<=10^101),求方程k^n=p的k的正整数解,保证k<=10^9 题解:这道神题传说有非常神奇的解f♂a
然而并没有什么卵用,你只会收到一连串的WA
该题的意图应该是贪心,至于怎么贪……わかない……
好吧,我太菜了,只能用最暴力的方法,设x^y=p
对于y>n的解来说,x必然小于k
对于y<n的解来说,x必然大于k
对于x来说单调性
所以可以二分
至于y该怎么求……想必一个高精度的log就行了!而且只需要保留个位即可
感觉我的代码还是有问题的,但莫名1A了
代码如下
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct big
{
int len;
int num[];
}; int n; void trans(char* a,big &b)
{
memset(b.num,,sizeof(b.num));
int len=strlen(a);
for(int i=; i<len; i++)
{
b.num[len-i-]=a[i]-'';
}
b.len=len;
} void trans_(int a,big &b)
{
memset(b.num,,sizeof(b.num));
int len=;
while(a)
{
b.num[len++]=a%;
a/=;
}
b.len=len;
} void print(big a)
{
for(int i=a.len-; i>=; i--)
{
printf("%d",a.num[i]);
}
puts("");
} int comp(big x,big y)
{
if(x.len>y.len)
{
return ;
}
if(x.len<y.len)
{
return -;
}
for(int i=x.len-; i>=; i--)
{
if(x.num[i]>y.num[i])
{
return ;
}
if(x.num[i]<y.num[i])
{
return -;
}
}
return ;
} big sub(big a,big b)
{
big c;
int len=a.len;
int lenc=len;
for(int i=; i<len; i++)
{
c.num[i]=a.num[i]-b.num[i];
if(c.num[i]<)
{
c.num[i]+=;
a.num[i+]--;
}
}
while(c.num[lenc-]==&&lenc>)
{
lenc--;
}
c.len=lenc;
return c;
} void mul_ten(big &x)
{
int len=x.len;
len++;
for(int i=len-; i>=; i--)
{
x.num[i+]=x.num[i];
}
x.num[]=;
while(x.num[len-]==&&len>)
{
len--;
}
x.len=len;
} big div(big x,big y)
{
big f,m;
memset(f.num,,sizeof(f.num));
memset(m.num,,sizeof(m.num));
m.len=;
int len=x.len;
for(int i=x.len-; i>=; i--)
{
mul_ten(m);
m.num[]=x.num[i];
while(comp(m,y)!=-)
{
m=sub(m,y);
f.num[i]++;
}
}
while(f.num[len-]==&&len>)
{
len--;
}
f.len=len;
return f;
} int check(big x,big y)
{
big z;
int cnt=;
z.len=;
z.num[]=;
while(!comp(x,z)==)
{
if(comp(x,y)==-)
{
break;
}
cnt++;
x=div(x,y);
}
if(cnt<n)
{
return ;
}
else
{
return ;
}
} int main()
{
char b[];
int a;
big x,y;
while(scanf("%d %s",&n,b)==)
{
memset(x.num,,sizeof(x.num));
memset(y.num,,sizeof(y.num));
int l=,r=;
int mid;
trans(b,y);
while(l<r)
{
mid=(l+r)>>;
trans_(mid,x);
int flag=check(y,x);
if(flag)
{
l=mid;
}
else
{
r=mid-;
}
if(r-l<=)
{
trans_(r,x);
if(check(y,x))
{
printf("%d\n",r);
break;
}
else
{
printf("%d\n",l);
break;
}
}
}
}
}
POJ - 2109 Power of Cryptography(高精度log+二分)的更多相关文章
- 贪心 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【高精度+二分 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 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(数学或二分+高精度)
题目链接: https://vjudge.net/problem/POJ-2109 题目大意: 有指数函数 k^n = p , 其中k.n.p均为整数且 1<=k<=10^9 , 1< ...
随机推荐
- 三种实现Ajax的方式
本文主要是比较三种实现Ajax的方式 1. prototype.js 2. jquery1.3.2.min.js 3. json2.js Java代码 收藏代码 后台处理程序(Servlet),访问路 ...
- Quartz.net 2.x 学习笔记02-Quartz.net 2.x在MVC站点中结合Log4net的使用
Quartz.net 2.x在MVC站点中结合Log4net的使用 首先新建一个MVC的空站点: 第二步,添加Quartz.net的引用 在搜索处输入quartz.net搜索安装即可(目前是2.3) ...
- Py修行路 python基础 (二十一)logging日志模块 json序列化 正则表达式(re)
一.日志模块 两种配置方式:1.config函数 2.logger #1.config函数 不能输出到屏幕 #2.logger对象 (获取别人的信息,需要两个数据流:文件流和屏幕流需要将数据从两个数据 ...
- leetcode609
public class Solution { public IList<IList<string>> FindDuplicate(string[] paths) { Dict ...
- c# 程序调用cmd执行命令如SVN.exe
c# 程序调用cmd执行命令如SVN.exe string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Di ...
- spark 中文编码处理
日志的格式是GBK编码的,而hadoop上的编码是用UTF-8写死的,导致最终输出乱码. 研究了下Java的编码问题. 网上其实对spark输入文件是GBK编码有现成的解决方案,具体代码如下 impo ...
- 字节流之文件输入流FileInputStream
- 1 JPA入门----项目搭建以及CRUD
maven搭建JPA开发环境 1 依赖的maven pom文件 主要有hibernate-core.hibernate-entitymanager.javax-persistence.mysq ...
- js如何解析后台传过来的json字符串
1.js如何解析后台传过来的json字符串? 注意:js是无法直接接收和使用json或者Php的数据,用的话会出现undefined,所以要转换一下. 方式一: var str = '{"r ...
- CSS 伪类与伪元素
CSS的元素选择器除了根据id(#).class(.).属性([ ])选取元素以外,还有很重要的一类,就是根据元素的特殊状态来选取元素.它们就是伪类和伪元素.跟id选择器.类选择器.属性选择器以及派生 ...
