UVA 113-Power of Cryptography(二分+double處理大資料)

來源:互聯網
上載者:User

標籤:

 Power of Cryptography Time Limit:3000MS      Memory Limit:0KB      64bit IO Format:%lld & %lluSubmit Status

Description


 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  and an integer  you are to write a program that determines  , the positive  root of p. In this problem, given such integers n and pp will always be of the form  for an integer k (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  ,  and there exists an integer k,  such that  .

The Output

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

Sample Input

21632774357186184021382204544

Sample Output

431234

題意:給出你n,p,n的k次方等於p,讓你求k

思路:首先得瞭解acm中常用的極限值

int和long都是用32位來儲存最大值和最小值分別為2147483647(109), -2147483648;


long long 是用64位來儲存最大值和最小值分別為9223372036854775807(1018),-9223372036854775808;


float的最大值和最小值分別為3.40282e+038(1038),1.17549e-038(10-38);


double的最大值和最小值分別為1.79769e+308(10308),2.22507e-308(10-308)

題目給出的n在10^108,所以用double


#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>int main(){    double k,p,n;    while(~scanf("%lf %lf",&n,&p))    {        int low,high,mid;        low=1;        high=pow(10,9);        while(low<=high)        {            mid=(low+high)/2;            k=pow(mid,n);            if(k==p)            {                printf("%d\n",mid);                break;            }            else if(k<p)             low=mid+1;            else              high=mid-1;        }    }    return 0;}


看到網上大牛們的做法,感覺很神奇,特此膜拜

題意:給出n和p,求出 ,但是p可以很大()
如何儲存p?不用大數可不可以?
先看看double行不行:指數範圍在-307~308之間(以10為基數),有效數字為15位。
誤差分析:
令f(p)=p^(1/n),Δ=f(p+Δp)-f(p)

則由泰勒公式得
(Δp的上界是因為double的精度最多是15位,n有下界是因為 )
由上式知,當Δp最大,n最小的時候誤差最大。
根據題目中的範圍,帶入誤差公式得Δ<9.0e-7,說明double完全夠用(這從一方面說明有效數字15位還是比較足的(相對於float),這也是float用的很少的原因)
這樣就滿足題目要求,所以可以用double過這一題。

#include<stdio.h>#include<math.h>int main(){    double a, b;    while(scanf("%lf%lf",&a,&b) != EOF)        printf("%.0lf\n",pow(b,1/a))    return 0;}



UVA 113-Power of Cryptography(二分+double處理大資料)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.