POJ - 2109 Power of Cryptography(高精度log+二分)

时间:2021-08-31 11:26:45
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
POJ - 2109 Power of Cryptography(高精度log+二分)

   然而并没有什么卵用,你只会收到一连串的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[200];
};

int n;

void trans(char* a,big &b)
{
memset(b.num,
0,sizeof(b.num));
int len=strlen(a);
for(int i=0; i<len; i++)
{
b.num[len
-i-1]=a[i]-'0';
}
b.len
=len;
}

void trans_(int a,big &b)
{
memset(b.num,
0,sizeof(b.num));
int len=0;
while(a)
{
b.num[len
++]=a%10;
a
/=10;
}
b.len
=len;
}

void print(big a)
{
for(int i=a.len-1; i>=0; i--)
{
printf(
"%d",a.num[i]);
}
puts(
"");
}

int comp(big x,big y)
{
if(x.len>y.len)
{
return 1;
}
if(x.len<y.len)
{
return -1;
}
for(int i=x.len-1; i>=0; i--)
{
if(x.num[i]>y.num[i])
{
return 1;
}
if(x.num[i]<y.num[i])
{
return -1;
}
}
return 0;
}

big sub(big a,big b)
{
big c;
int len=a.len;
int lenc=len;
for(int i=0; i<len; i++)
{
c.num[i]
=a.num[i]-b.num[i];
if(c.num[i]<0)
{
c.num[i]
+=10;
a.num[i
+1]--;
}
}
while(c.num[lenc-1]==0&&lenc>1)
{
lenc
--;
}
c.len
=lenc;
return c;
}

void mul_ten(big &x)
{
int len=x.len;
len
++;
for(int i=len-1; i>=0; i--)
{
x.num[i
+1]=x.num[i];
}
x.num[
0]=0;
while(x.num[len-1]==0&&len>1)
{
len
--;
}
x.len
=len;
}

big div(big x,big y)
{
big f,m;
memset(f.num,
0,sizeof(f.num));
memset(m.num,
0,sizeof(m.num));
m.len
=1;
int len=x.len;
for(int i=x.len-1; i>=0; i--)
{
mul_ten(m);
m.num[
0]=x.num[i];
while(comp(m,y)!=-1)
{
m
=sub(m,y);
f.num[i]
++;
}
}
while(f.num[len-1]==0&&len>1)
{
len
--;
}
f.len
=len;
return f;
}

int check(big x,big y)
{
big z;
int cnt=0;
z.len
=1;
z.num[
0]=1;
while(!comp(x,z)==0)
{
if(comp(x,y)==-1)
{
break;
}
cnt
++;
x
=div(x,y);
}
if(cnt<n)
{
return 0;
}
else
{
return 1;
}
}

int main()
{
char b[202];
int a;
big x,y;
while(scanf("%d %s",&n,b)==2)
{
memset(x.num,
0,sizeof(x.num));
memset(y.num,
0,sizeof(y.num));
int l=2,r=1000000000;
int mid;
trans(b,y);
while(l<r)
{
mid
=(l+r)>>1;
trans_(mid,x);
int flag=check(y,x);
if(flag)
{
l
=mid;
}
else
{
r
=mid-1;
}
if(r-l<=1)
{
trans_(r,x);
if(check(y,x))
{
printf(
"%d\n",r);
break;
}
else
{
printf(
"%d\n",l);
break;
}
}
}
}
}