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 n th. 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<10 101 and there exists an integer k, 1<=k<=10 9 such that k n = 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
题意:给出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+二分)的更多相关文章

  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: 26622   Accepted: ...

  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

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

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

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

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

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

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

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

  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(数学或二分+高精度)

    题目链接: https://vjudge.net/problem/POJ-2109 题目大意: 有指数函数 k^n = p , 其中k.n.p均为整数且 1<=k<=10^9 , 1< ...

随机推荐

  1. dubbox下载编译运行demo

    最近公司要搞微服务改造,拿了一个小项目开刀,找来找去,还是偏向当当的dubbox作为分布式服务框架.这里介绍下怎么一条龙跑起一个demo. 1.下载代码 因为代码放在github上,所以我们直接用Ec ...

  2. Python中if __name__ == 'main' 的作用和原理

    参考网址:http://mp.weixin.qq.com/s/kxxhOQ7KB_VMwWeUENX7OQ t1.py: print('Loving Python') def main(): prin ...

  3. javascript深入浅出学习笔记

    一.数据类型:1.对象与对象是不相等的,比如:console.log(new Object() == new Object())//结果是false;console.log([1,2] == [1,2 ...

  4. Server Error in '/' Application. IIS拒绝访问

    昨天将改好的网站重写发布更新了一下,就出现这种问题.那是一个头两个大呀. 刚开始以为是文件夹没有IIS的访问权限,在网上找的好多答案都是temp文件夹没有权限,,但将IIS的权限都加上后,还是不行,同 ...

  5. 【转】Jmeter应用评估

    Jmeter应用评估 发布时间: 2008-9-03 16:17    作者: 未知    来源: 网络转载 字体:  小  中  大  | 上一篇 下一篇 | 打印  | 我要投稿  | 推荐标签: ...

  6. spring bean id重复覆盖的问题解决

    问题: 当我们的web应用做成一个大项目之后,里面有很多的bean配置,如果两个bean的配置id是一样的而且实现类也是一样的,例如有下面两份xml的配置文档: beancontext1.xml &l ...

  7. proc文件系统详解(原创)

    Linux系统上的/proc目录是一种文件系统,即proc文件系统.与其它常见的文件系统不同的是,/proc是一种伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件,用户可以通过 ...

  8. List转Datatable 新方法

    方法1,最简单的转换 DataTable dt = new DataTable(); dt.Columns.Add("id"); dt.Columns.Add("name ...

  9. Django项目部署-01

    1. 安装Python 下载链接:https://www.python.org/getit/ 我这边下载的是3.6.5的版本的执行版本,安装过程中选择自动安装pip 2.安装django pip in ...

  10. Tornado 高并发源码分析之五--- IOLoop 对象

    IOLoop主要工作 1.将TCPServer 注册到 IOLoop 的事件记到 _handlers 字段,同时注册 READ 和 ERROR 事件到 epoll 2.IOLoop 启动一个大循环,负 ...