程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> UVA 113

UVA 113

編輯:關於C++

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

Description

Download as PDF


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 positive tex2html_wrap_inline38 root of p. In this problem, given such integers n and p, p will always be of the form tex2html_wrap_inline48 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 tex2html_wrap_inline56 , tex2html_wrap_inline58 and there exists an integer k, tex2html_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

題意:給出你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 
#include 
#include 
#include 
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

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

題意:給出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
#include
int main()
{
    double a, b;
    while(scanf("%lf%lf",&a,&b) != EOF)
        printf("%.0lf\n",pow(b,1/a))
    return 0;
}



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved