现在的位置: 首页 > 综合 > 正文

113 – Power of Cryptography

2018年01月12日 ⁄ 综合 ⁄ 共 3016字 ⁄ 字号 评论关闭

 Power of Cryptography 

Background

Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers modulo functions of 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 of only theoretical interest.

This problem involves the efficient computation of integer roots of numbers.

The Problem

Given an integer tex2html_wrap_inline32 and an integer tex2html_wrap_inline34 you
are to write a program that determines tex2html_wrap_inline36 , the positivetex2html_wrap_inline38 root
of p. In this problem, given such integers n and pp will always be of the form tex2html_wrap_inline48 for an integerk (this
integer is what your program must find).

The 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 tex2html_wrap_inline56 , tex2html_wrap_inline58 and
there exists an integer ktex2html_wrap_inline62 such that tex2html_wrap_inline64 .

The Output

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

Sample Input

2
16
3
27
7
4357186184021382204544

Sample Output

4
3
1234

一开始看到这道题的时候,一点想法都没有。。后来看了POJ里面的discuss,可以用二分加高精度。。
然后我就很悲催的写了。。。最后再UVa OJ上面RE了,不过在POJ上面AC了。。

然后。。。。原来可以用double类型直接做。。
补充一下float和double类型的知识 来自 http://blog.sina.com.cn/s/blog_6f901d67010189nh.html

延伸一下:

float和double的范围和精度。

1. 范围   float和double的范围是由指数的位数来决定的。   float的指数位有8位,而double的指数位有11位,分布如下:   float:   1bit(符号位) 8bits(指数位) 23bits(尾数位)   double:   1bit(符号位) 11bits(指数位) 52bits(尾数位)   于是,float的指数范围为-127~+128,而double的指数范围为-1023~+1024,并且指数位是按补码的形式来划分的。   其中负指数决定了浮点数所能表达的绝对值最小的非零数;而正指数决定了浮点数所能表达的绝对值最大的数,也即决定了浮点数的取值范围。   float的范围为-2^128 ~ +2^128,也即-3.40E+38 ~ +3.40E+38;double的范围为-2^1024 ~ +2^1024,也即-1.79E+308 ~ +1.79E+308。

2.  精度   float和double的精度是由尾数的位数来决定的。浮点数在内存中是按科学计数法来存储的,其整数部分始终是一个隐含着的“1”,由于它是不变的,故不能对精度造成影响。   float:2^23 = 8388608,一共七位,这意味着最多能有7位有效数字,但绝对能保证的为6位,也即float的精度为6~7位有效数字;   double:2^52 = 4503599627370496,一共16位,同理,double的精度为15~16位。

/*
//一开始毫无头绪。。看了discuss才有想法 二分+高精度。。。
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<ctype.h>
#include<cstdio>
#include<cmath>
using namespace std;
int n;
char p[120];
int num(int mid)
{
    int ji[120]= {0},chen[12]= {0};
    int i,j,len=0;
    while(mid)
    {
        chen[len++]=mid%10;
        mid/=10;
        ji[len-1]=chen[len-1];
    }
    int LEN=len;
    for (int t=0; t<n-1; t++)
    {
        int ji_copy[120]={0};
        for (i=0; i<len; i++)
            for (j=0; j<LEN; j++)
            {
                ji_copy[i+j]+=chen[i]*ji[j];
                if (ji_copy[i+j]>9)
                {
                    ji_copy[i+j+1]+=ji_copy[i+j]/10;
                    ji_copy[i+j]=ji_copy[i+j]%10;
                }
            }
        int k=len+LEN+3;
        while(ji_copy[k]==0) k--;
        LEN=k+1;
        for (i=0; i<LEN; i++)
            ji[i]=ji_copy[i];
    }
    if (LEN<strlen(p)) return 1;
    else if (LEN>strlen(p)) return -1;
    else
    {
        char p_fight[120];
        j=0;
        for (i=LEN-1; i>=0; i--)
            p_fight[j++]=ji[i]+'0';
        p_fight[j]='\0';
        return strcmp(p,p_fight);
    }
}
int binary_search(int wei)
{
    int top,mid,bot,i;
    bot=pow(10,wei-1);
    top=pow(10,wei);
    while(bot<=top)//一开始=没有,二分都没学扎实
    {
        mid=(bot+top)/2;
        int temp=num(mid);
        if (temp>0)
            bot=mid+1;
        else if (temp<0)
            top=mid-1;
        else break;
    }
    return mid;
}
int main ()
{
    int wei;
    while(cin>>n)
    {
        memset(p,0,sizeof(p));
        getchar();
        gets(p);
        int len=strlen(p);
        int tt;
        tt=len/n;
        if (tt*n-len==0) wei=tt;
        else wei=tt+1;
        int mid=binary_search(wei);
        cout<<mid<<endl;
    }
    return 0;
}
*/

//double 类型的
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main ()
{
    double k,n,p;
    while(scanf("%lf%lf",&n,&p)!=EOF)
    {
        printf("%.0lf\n",pow(p,(1/n)));
    }
    return 0;
}

抱歉!评论已关闭.