[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. js初学—js全自定义单选框

    代码如下: <script type="text/javascript"> window.onload=function() { var oIput=document. ...

  2. matlab和C/C++混合编程--Mex

    最近的项目需要matlab和C的混合编程,经过一番努力终于完成了项目要解决的问题.现在就将Mex的一些经验总结一下,当然只是刚刚开始,以后随着学习的深入继续添加.首先讲讲写Mex的一些常规规定,然后我 ...

  3. Java适配器设计模式

    适配器设计模式,一个接口首先被一个抽象类先实现(此抽象类通常称为适配器类),并在此抽象类中实现若干方法(但是这个抽象类中的方法体是空的),则以后的子类直接继承此抽象类,就可以有选择地覆写所需要的方法. ...

  4. 关于软件工程个人作业 Word frequency program 的总结

    一.预计花在程序各部分的时间: (1)对所给命令行参数的判断和处理,看它是否合法.是哪种模式.预计用时20min; (2)关于目录操作:遍历给定目录下的所有文件,包括子目录和非目录文件.预计用时40m ...

  5. codeforces 723B Text Document Analysis(字符串模拟,)

    题目链接:http://codeforces.com/problemset/problem/723/B 题目大意: 输入n,给出n个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...

  6. Ajax与DOM实现动态加载

    阅读目录 DOM如何动态添加节点 Ajax异步请求 Chrome处理本地Ajax异步请求 参考: 首先说下问题背景:想要通过异步请求一个文本文件,然后通过该文件的内容动态创建一个DOM节点添加到网页中 ...

  7. aufomaper Queryable Extensions ProjectTo

    When using an ORM such as NHibernate or Entity Framework with AutoMapper's standard Mapper.Map funct ...

  8. jQuery监听键盘事件及相关操作使用教程

    一.首先需要知道的是: 1.keydown() keydown事件会在键盘按下时触发. 2.keyup() keyup事件会在按键释放时触发,也就是你按下键盘起来后的事件 3.keypress() k ...

  9. C# 获取指定接口的所有实现类

    var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes().Where(t => t ...

  10. [MongoDB]count,gourp,distinct

    摘要 上篇文章介绍了CRUD的操作,会了这些,基本上可以完成很多工作了.但如果遇到统计类的操作,那么就需要学习下本篇的内容了. 相关文章 [MongoDB]入门操作 [MongoDB]增删改查 cou ...