POJ-2109 Power of Cryptography

Power of Cryptography

原题链接
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 26641 Accepted: 13309
Description

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

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<10^101 and there exists an integer k, 1<=k<=10^9 such that kn = p.
Output

For each integer pair n and p the value k should be printed, i.e., the number k such that k n =p.
Sample Input

2 16
3 27
7 4357186184021382204544
Sample Output

4
3
1234
Source

México and Central America 2004

密码学的力量

[原题链接](http://poj.org/problem?id=2109
时间限制:1000MS内存限制:30000K
总提交内容:26641接受:13309
描述

目前在密码学工作涉及(其中包括)大素数和这些素数中的数字计算能力。 在这方面的工作导致数论和其他数学分支结果的实际应用一度被认为只是理论上的兴趣。
这个问题涉及数字的整数根的有效计算。
给定一个整数n> = 1和一个整数p> = 1,你必须编写一个程序来确定p的第n个正根。 在这个问题中,给定这样的整数n和p,p将始终是k到n的形式。 答案,为一个整数k(这个整数是你的程序必须找到的)。
输入

输入由一系列整数对n和p组成,其中每一个整数都在一行上。 对于所有这样的对1 <= n <= 200,1 <= p <10^101并且存在整数k,1 <= k <= 10^9使得k^n = p。
输出

对于每个整数对n和p,应该打印值k,即数k使得k^n = p。
示例输入

2 16
3 27
7 4357186184021382204544
示例输出

4
3
1234
来源

墨西哥和中美洲2004年

题意

题目的意思大概就是给你两个整数 n 和 p ,让你求一个 k 满足 kn=p k n = p

题解

第一种方法是写高精度,再用二分查找答案。
第二种是用数学方法求, k=p1n k = p 1 n

代码

这段是 k=p1n k = p 1 n

#include<cmath>
#include<cstdio>
using namespace std;
double n,p,k;
int main()
{
    while (scanf("%lf%lf",&n,&p)!=EOF)
    {
        k=pow(p,1.0/n);//pow(a,b) 相当于 a^b
        printf("%d\n",(int)floor(k+0.5));//floor(x) 是求x的绝对值
    }
    return 0;
}

这段是高精度。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxL=505,tt=10000;
char s[maxL<<2];
int n,L,R;
struct bigint{
    int a[maxL],len;
    bigint(){memset(a,0,sizeof a);len=0;}
    bigint(int x)//int类型 转成 bigint 类型
    {
        memset(a,0,sizeof a);
        for (len=0;x;x/=tt) a[++len]=x%tt;
    }
    bigint (char *s)//字符串类型 转成 bigint类型,因为p很大,要用字符型数组读入
    {
        memset(a,0,sizeof a);int x=L-3;
        for (len=0;x+3>=1;x-=4)
        {
            len++;
            for (int i=max(x+3,1),y=1;i>=x&&i>0;i--)
              a[len]+=(s[i]-'0')*y,y*=10;
        }
    }
    bigint operator *(const bigint &b)const//重载乘号
    {
        bigint c;c.len=len+b.len;
        for (int i=1;i<=len;i++)
         for (int j=1;j<=b.len;j++)
           c.a[i-1+j]+=a[i]*b.a[j],c.a[i+j]+=c.a[i-1+j]/tt,c.a[i-1+j]%=tt;
        while (c.len>1&&!c.a[c.len]) c.len--;
        return c;
    }
    bool operator ==(const bigint &b)const//重载 ==
    {
        if (len!=b.len) return 0;
        else for (int i=1;i<=len;i++) if (a[i]!=b.a[i]) return 0;
        return 1;
    }
    bool operator <(const bigint &b)const//重载小于号
    {
        if (len<b.len) return 1;else if (len>b.len) return 0;
        for (int i=len;i;i--)
          if (a[i]<b.a[i]) return 1;else
          if (a[i]>b.a[i]) return 0;
        return 0;
    }
}p,ans,a;
void qsm(bigint &a,int b,bigint &w)//快速幂
{
    for (w=1;b;b>>=1)
    {
        if (b&1) w=w*a;
        a=a*a;
    }
}
int main()
{
    while (1)
    {
        if (scanf("%d%s",&n,s+1)==EOF) break;
        L=strlen(s+1);p=s;
        L=1,R=1e9;bool f=1;
        while (L<=R)//二分查找
        {
            int mid=(R-L>>1)+L;
            a=mid;qsm(a,n,ans);
            if (ans==p) {printf("%d\n",mid);f=0;break;}
            if (ans<p) L=mid+1;else R=mid-1;
        }
        if (f) printf("%d\n",R);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值