[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.

输入示例


输出示例


数据规模及约定

见“输入

题解

高精度 + 二分。。。然而这不是最恶心的,最恶心的是 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的更多相关文章

  1. POJ2109——Power of Cryptography

    Power of Cryptography DescriptionCurrent work in cryptography involves (among other things) large pr ...

  2. POJ-2109 Power of Cryptography(数学或二分+高精度)

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

  3. Power of Cryptography(用double的泰勒公式可行分析)

    Power of Cryptography Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onli ...

  4. 贪心 POJ 2109 Power of Cryptography

    题目地址:http://poj.org/problem?id=2109 /* 题意:k ^ n = p,求k 1. double + pow:因为double装得下p,k = pow (p, 1 / ...

  5. poj 2109 Power of Cryptography

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

  6. UVA 113 Power of Cryptography (数学)

    Power of Cryptography  Background Current work in cryptography involves (among other things) large p ...

  7. 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 ...

  8. POJ 2109 :Power of Cryptography

    Power of Cryptography Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18258   Accepted: ...

  9. POJ 2109 -- Power of Cryptography

    Power of Cryptography Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26622   Accepted: ...

随机推荐

  1. console使用方法

    一般情况下我们用来输入信息的方法主要如下四个: 1.console.log 用于输出普通信息 2.console.info 用于输出提示性信息 3.console.error用于输出错误信息 4.co ...

  2. Linux 下 Oracle 内核参数优化

    数据库的性能优化涉及到整个数据库运行环境的方方面面,诸如操作系统,Oracle自身,存储,网络等等几个大块.而操作系统则是Oracle稳定运行与最大化性能的基石.本文主要描述基于Linux系统下 Or ...

  3. lua 闭包

    --匿名函数使用upvalue i保存他的计数, 闭包是一个函数加上它可以正确访问的upvalues function newCounter() return function() i = i + r ...

  4. easyUI学习笔记之tab组件的鼠标事件

    一.鼠标经过组件后的事件,自动打开选项卡内容 var tabs = $('#tt').tabs().tabs('tabs'); for(var i=0; i<tabs.length; i++){ ...

  5. CentOS配置163的yum源

    entOS系统自带的更新源的速度在国内非常慢,在国内为了让CentOS6使用速度较快快的YUM更新源,建议选择163(网易)的更新源. 1.下载repo文件wget http://mirrors.16 ...

  6. netstat--查看服务器[有效]连接数--统计端口并发数--access.log分析

    简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...

  7. centos 7.0 nginx 1.7.9 安装过程

    系统用的是centos 7.0最小化安装 我现在安装完了 写一下步骤 还没完全搞懂 首先安装GCC [root@localhost ~]# yum install -y gcc gcc-c++ 已加载 ...

  8. 使用CFURLCreateStringByAddingPercentEscapes进行URL编码

    iOS程序访问HTTP资源时需要对URL进行UTF8编码,特酷吧在之前一直都喜欢使用NSString的stringByAddingPercentEscapesUsingEncoding方法进行编码.今 ...

  9. kafka环境搭建及librdkafka测试

    kafka环境搭建及librdkafka测试 (2016-04-05 10:18:25)   一.kafka环境搭建(转自http://kafka.apache.org/documentation.h ...

  10. linuxMint下安装ftp工具--filezilla

    windows下ftp工具有好多,linux下推荐用filezilla 安装filezilla很简单,安装完后,使用方式和windows下面一样. 第一种方式: 直接去filezilla官网下载软件包 ...