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. mysql导入导出表结构

    mysql导入导出表结构 导出整个库的表结构如下:mysqldump -uroot -p -d databasename > createtab.sql 如果只想导出表: test1,test2 ...

  2. 如何配置数据库ODBC数据源

    在<调整计算机的设置>中,点击<系统和安全>.   点击<管理工具>.   点击<数据源(ODBC)>.   点击<系统用户>,然后,点击按 ...

  3. FPGA层次结构和复位策略

    FPGA设计中,层次结构设计和复位策略影响着FPGA的时序.在高速设计时,合理的层次结构设计与正确的复位策略可以优化时序,提高运行频率. 设计中,合理的层次结构是我们所追求的. 划分时,按照逻辑分区将 ...

  4. 【转】Jmeter性能测试报告解析

    Jmeter报告解析 1.Aggregate Report 解析 Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告 ...

  5. java的错误代码。。。。

    总结:从键盘输入一组数,输出其最大值,这段代码是错的,因为每次都错在这里,所以自己还是没有理解~~~~ import java.util.Scanner; //键盘输入一组数据,并输出最小值 //从键 ...

  6. html 文字垂直居中

    html  文字垂直居中 <span style="float:right; padding-right:30px;line-height:64px" class=" ...

  7. Elasticsearch5.6安装

    Elasticsearch5.6安装 1.下载 5.6.8 sudo curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/e ...

  8. Linux MTD系统剖析

    MTD,Memory Technology Device即内存技术设备,在Linux内核中,引入MTD层为NOR FLASH和NAND FLASH设备提供统一接口.MTD将文件系统与底层FLASH存储 ...

  9. pandas层级索引

    层级索引(hierarchical indexing) 下面创建一个Series, 在输入索引Index时,输入了由两个子list组成的list,第一个子list是外层索引,第二个list是内层索引. ...

  10. mssql server修改数据库文件位置 此种方法暂未测试成功

    --查看当前的存放位置 select database_id,name,physical_name AS CurrentLocation,state_desc,size from sys.master ...