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. Linux Change The I/O Scheduler For A Hard Disk

    ow do I change the I/O scheduler for a particular hard disk without rebooting my Linux server system ...

  2. 在Linux 64位系统下使用hugepage

    首先,为什么要介绍/使用HugePage? 在步入正题之前,先讲一个非常普遍的数据库性能问题. 众所周知,Oracle数据库使用共享内存(SGA)来管理可以共享的一些资源;比如shared pool中 ...

  3. java 代码,练习ip,主机名的获取方法。InetAddress类

    package clientFrame; import java.io.IOException; import java.net.*; public class tai { public static ...

  4. XAMPP配置8080端口

    IIS需要HTTP服务,这个服务占用了80端口. Apache启动不了,为了都可以使用,将Apache端口改为8080.

  5. Sqoop导出MySQL数据

    导入所有表: sqoop import-all-tables –connect jdbc:mysql://ip:port/dbName --username userName --password p ...

  6. TIMEQUEST学习之黑金动力(三)

    不知不觉,学到的第四章.但是对于TQ的内部模型和外部模型的完整分析还是没有很好的理解.接着学习......... 我们也了解静态时序分析的第一步骤,亦即时钟方面的约束.此外,也稍微对 Report T ...

  7. python's twenty eithth day for me 模块和包

    模块: 什么是模块: 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀,但其实import加载的模块分为四个通用类别: 1,使用python编写的代码 ...

  8. JavaScript高级程序设计学习

    1.变量 变量使用var操作符定义,var message,定义一个message变量,可用来保存任何类型的变量.未经初始化的变量值为undifided: 如果没变量没有被var定义,那么被执行后会成 ...

  9. JAVA中跨平台分隔符

    在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常. 比如说要在temp目录下建立一个te ...

  10. jetbrains idea/webstorm等(注册,激活,破解码,一起支持正版,最新可用)(2017.3.16更新)【转】

    选择 License server (2017.3.16更新) http://idea.imsxm.com/ 详细请参考:  http://www.cnblogs.com/ys-wuhan/p/584 ...