[POJ2109]Power of Cryptography

Posted xjr_01

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ2109]Power of Cryptography相关的知识,希望对你有一定的参考价值。

[POJ2109]Power of Cryptography

试题描述

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 nth. power, for an integer k (this integer is what your program must find).

输入

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<10101 and there exists an integer k, 1<=k<=109 such that kn = p.

输出

For each integer pair n and p the value k should be printed, i.e., the number k such that k n =p.

输入示例

2 16
3 27
7 4357186184021382204544

输出示例

4
3
1234

数据规模及约定

见“输入

题解

高精度 + 二分。。。然而这不是最恶心的,最恶心的是 POJ 上数据是错的。题目说好了“p will always be of the form k to the nth”,然而实际 p 并不一定是 k 的 n 次方,并且我的二分找的是大于 k 且最接近 k 的整数,而数据要求的是小于 k 且最接近 k 的整数,于是我就被坑的调了一个晚上 + 一个上午。。。。

下面是 AC 代码。(这题可以用 double 水过)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
using namespace std;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = getchar(); }
	return x * f;
}

#define maxn 1010
int n;

struct LL {
	int len, A[maxn];
	LL() { len = 1; memset(A, 0, sizeof(A)); }
	LL operator = (const int& t) {
		len = 1; A[1] = t;
		while(A[len] > 9) A[len+1] += A[len] / 10, A[len] %= 10, len++;
		return *this;
	}
	LL operator = (const LL& t) {
		len = t.len;
		memset(A, 0, sizeof(A));
		for(int i = 1; i <= len; i++) A[i] = t.A[i];
		return *this;
	}
	LL operator + (const LL& t) const {
		LL ans; ans.len = max(len, t.len);
		for(int i = 1; i <= len; i++) ans.A[i] = A[i];
		for(int i = 1; i <= t.len; i++) ans.A[i] += t.A[i];
		for(int i = 1; i < ans.len; i++) ans.A[i+1] += ans.A[i] / 10, ans.A[i] %= 10;
		while(ans.A[ans.len] > 9) ans.A[ans.len+1] += ans.A[ans.len] / 10, ans.A[ans.len] %= 10, ans.len++;
		return ans;
	}
	LL operator * (const LL& t) const {
		LL ans; ans.len = len + t.len - 1;
		if((len == 1 && !A[1]) || (t.len == 1 && !t.A[1])) return ans = 0;
		for(int i = 1; i <= len; i++)
			for(int j = 1; j <= t.len; j++)
				ans.A[i+j-1] += A[i] * t.A[j];
		for(int i = 1; i < ans.len; i++) ans.A[i+1] += ans.A[i] / 10, ans.A[i] %= 10;
		while(ans.A[ans.len] > 9) ans.A[ans.len+1] += ans.A[ans.len] / 10, ans.A[ans.len] %= 10, ans.len++;
		return ans;
	}
	LL operator *= (const LL& t) {
		*this = *this * t;
		return *this;
	}
	LL operator / (const int& t) const {
		LL ans; int f = 0;
		for(int i = len; i; i--) {
			f = f * 10 + A[i];
			if(f >= t) ans.len = max(ans.len, i);
			ans.A[i] = f / t; f %= t;
		}
		return ans;
	}
	bool operator < (const LL& t) const {
		if(len != t.len) return len < t.len;
		for(int i = len; i; i--) if(A[i] != t.A[i]) return A[i] < t.A[i];
		return 0;
	}
	void print() {
		for(int i = len; i; i--) putchar(A[i] + ‘0‘); putchar(‘\n‘);
		return ;
	}
} p;

LL Lread() {
	LL x, f, ten; x = 0; f = 1; ten = 10; char c = getchar();
	while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = getchar(); }
	while(isdigit(c)) {
		LL tmp; tmp = (int)(c - ‘0‘);
		x = x * ten + tmp; c = getchar();
	}
	return x * f;
}

LL Pow(LL x, int a) {
	LL ans; ans = 1;
	for(int i = 1; i <= a; i++) {
		ans *= x;
		if(p < ans) return ans;
	}
	return ans;
}

int main() {
	while(scanf("%d", &n) == 1) {
		p = Lread();
		LL l, r, one, l1; one = 1; l = 0; l1 = 0; r = p + one;
		while(l + one < r) {
			LL mid; mid = (l + r) / 2;
			LL tmp = Pow(mid, n);
			if(p < tmp) r = mid; else l = mid;
		}
		
		l.print();
	}
	
	return 0;
}

 

以上是关于[POJ2109]Power of Cryptography的主要内容,如果未能解决你的问题,请参考以下文章

?POJ 2109 -- Power of Cryptography

POJ2109 Power of Cryptography

POJ 2109 Power of Cryptography

POJ_2109 Power of Cryptography 数学

poj 2109 Power of Cryptography

poj2109 Power of Cryptography坑~泪目水过