[POJ2109]Power of Cryptography

时间:2021-09-07 10:06:05

[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&lpar;数学或二分&plus;高精度&rpar;

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

  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 &lpar;数学&rpar;

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

  7. Poj 2109 &sol; 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. Service服务

    Android多线程: 定义线程的2种方式: 1.继承Thread类,重写run()方法,new一个实例,用start()方法启动:new MyThread().start(); 2.实现Runnab ...

  2. SpringMVC拦截器(资源和权限管理)

    1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet.    DispatcherServle ...

  3. 移动端的头部标签和meta

    <!DOCTYPE html><!--HTML5 doctype--> <html> <head> <title>xxx</title ...

  4. minimum-number-of-arrows-to-burst-balloons(还挺好)

    https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/ 与会议室排期问题,很相似. package com. ...

  5. iOS局部刷新

    iOS: TableView如何刷新指定的cell 或section //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithInd ...

  6. 根据用户的ID查用户的名字

    awk -F: '{if($3==0){print $1}}'  /etc/passwd

  7. 为Linux用ISO制作U盘启动及基本原理

    制作成功后的基本最简文件夹文件图 一.系统的基本引导流程: 首先系统要引导isolinux.bin可执行程序,此程序是移动介质上引导用的,isolinux.bin执行成功后会载入其配置文件syslin ...

  8. android Intent&period;createChooser 应用选择

    在微博案例: 1.public void onClickShare(View view) { 2. 3. Intent intent=new Intent(Intent.ACTION_SEND); 4 ...

  9. 内联函数 inline 漫谈

    内联函数存在的结论是: 引入内联函数是为了解决函数调用效率的问题 由于函数之间的调用,会从一个内存地址调到另外一个内存地址,当函数调用完毕之后还会返回原来函数执行的地址.函数调用会有一定的时间开销,引 ...

  10. pc端结合canvas实现简单签名功能

    需求:业务员做提交时要签名... 代码不多简单易懂,直接看代码 <!DOCTYPE html> <html> <head> <meta charset=&qu ...