[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. https 页面中引入 http 资源的解决方式

    相对协议 应用场景 浏览器默认是不允许在 https 里面引用 http 资源的,一般都会弹出提示框. 用户确认后才会继续加载,用户体验非常差. 而且如果在一个 https 页面里动态的引入 http ...

  2. hadoop2.6.4运行wordcount

    hadoop用户登录,启动服务: start-dfs.sh && start-yarn.sh 创建输入目录: hadoop df -mkdir /input 把测试文件导入/input ...

  3. SVN服务器配置说明

    1.前 言 花了72小时,终于把 Subversion 初步掌握了.从一个连“什么是版本控制”都不知道的门外汉,到配置出精确至每目录访问的入门者,中间还卡了一天时间.其中费了许多气力,摸索实验了多次, ...

  4. Python获取文件名

    本文实例讲述了python实现从URL地址提取文件名的方法.分享给大家供大家参考.具体分析如下: 如:地址为 http://www.jb51.net/images/logo.gif 要想从该地址提取l ...

  5. Integer 与int 赋值比较

    测试代码: @Test public void IntegerTest() { Integer i01 = 59; int i02 = 59; Integer i03 = Integer.valueO ...

  6. 9月20日上午JavaScript函数

    函数 一.  函数定义 函数又叫方法,在程序里面函数是用来执行某些特定功能的代码.为了减少重复使用代码,可以把特定功能的代码做成函数,需要使用时拿出来调用.alert();就是一个很常见的.简单的函数 ...

  7. HTTP错误404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求

    http://www.cnblogs.com/JKqingxinfeng/archive/2012/10/29/2744663.html HTTP错误404.13 - Not Found 请求筛选模块 ...

  8. Lua 之string库

    标准string库 基础字符串函数 string.len(s) 返回一个字符串的长度,例如 string.rep(s, n) 返回一个新的字符串,该字符串是参数s重复n次得到的结果,例如 )) -- ...

  9. border opacity

    div { border: 1px solid rgb(127, 0, 0); border: 1px solid rgba(255, 0, 0, .5); -webkit-background-cl ...

  10. 用C语言将搜狗输入法词库转换成QQ拼音输入法词库

    搜狗输入法词库格式: 'ni'kan'xia 你看下 'ni'kan'xia'gai'hou 你看下改后 'ni'kan'xing'ma 你看行吗 'ni'kan'zen'me'yang 你看怎么样 ...