[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. CSS 中如何把 Span 标签设置为固定宽度

    一.形如<span>ABC</span>独立行设置SPAN为固定宽度方法如下: span {width:60px; text-align:center; display:blo ...

  2. ajax请求下拉列表框的实现(面向对象封装类)

    实现的效果图 <?php class Car{ private $carColor; private $carType; public function __construct($carColo ...

  3. 自然语言9_NLTK计算中文高频词

    以下代码仅限于python2 NLTK计算中文高频词 >>> sinica_fd=nltk.FreqDist(sinica_treebank.words()) >>> ...

  4. Linux下开发常用 模拟 Http get和post请求

    1.get请求 curl "http://www.baidu.com"      如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "htt ...

  5. background属性

    background: url(images/01.jpg) 0 10px; 效果: background: url(images/01.jpg) 0 -10px; 效果: *注释:10px 是网上去 ...

  6. 一个有趣的模拟光照的shader

    一个有趣的模拟光照的shader(类似法线贴图) http://www.cnblogs.com/flytrace/p/3395911.html -----  可否用于需UI中需要加灯的模型.

  7. Windows Server 2008修改IE浏览器级别便于使用

    1.降低IE安全级别  Win 2008默认IE的安全级别为“高”,并且不能随意调整,在浏览网页的时候有些会有一些限制,可以打开注册表编辑器进行设置,定位到 [HKEY_LOCAL_MACHINE\S ...

  8. Struts2的模板和主题theme及自定义theme的使用

    Struts2的模板和主题theme及自定义theme 标签: struts2 2016-03-29 11:22 190人阅读 评论(0) 收藏 举报  分类: javaweb(8)  Struts2 ...

  9. Spring4学习笔记-AOP

    1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEAS ...

  10. YII2 实现后台操作记录日志(转)

    一.连接linux服务器,创建数据文件 php yii migrate/create user_log 二.修改数据文件 console/migrations/m150721_032220_admin ...