poj2109Power of Cryptography

背景:题目不难,但由于是第一次用二分法写代码,在结束条件那个地方纠结了半天。

思路:简单二分法。

学习:二分法:当数据量很大适宜采用该方法。采用二分法查找时,数据需是排好序的。主要思想是:(设查找的数组区间为array[low, high])(1)确定该期间的中间位置K(2)将查找的值T与array[k]比较。若相等,查找成功返回此位置;否则确定新的查找区域,继续二分查找。区域确定如下:a.array[k]>T
由数组的有序性可知array[k,k+1,……,high]>T;故新的区间为array[low,……,K-1]b.array[k]<T 类似上面查找区间为array[k+1,……,high]。每一次查找与中间值比较,可以确定是否查找成功,不成功当前查找区间缩小一半。

#include <iostream>
#include <cmath>
using namespace std;
#define WUCHA 1e-6
void erfen(double n,double p)
{
    double first=0,last=1e10,mid;
    while (last>WUCHA+first)
    {
        mid=(first+last)/2;
        if(pow(mid,n)>p) last=mid;
        else if(pow(mid,n)<p) first=mid;
    }
    cout<<(int)(mid+WUCHA)<<endl;
}
int main(void)
{
    double n,p;
    while(cin>>n>>p)
        erfen(n,p);
    return 0;
}

当看了网上一些人的代码,发现了自己一直理解错的函数,我以为pow()函数只能用于幂运算,结果还可以反着用来开方。下面附pow()函数代码

#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
    double n,p;
    while(cin>>n>>p)
    {
        int k;
        k=(int)(pow(p,1.0/n)+0.5);
        cout<<k<<endl;
    }
    return 0;
}
时间: 2024-11-02 22:52:57

poj2109Power of Cryptography的相关文章

(转)poj2109-Power of Cryptography

Power of Cryptography 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

转:system.Security.Cryptography C# 加密和解密

以下文转自: http://www.360doc.com/content/13/0122/05/19147_261678471.shtml 总结:注册的时候经过MD5加密存进数据库,在登录的时候需要先加密输入的密码,再进行和数据库里的比对,因为同一字符串加密后是一样的,并不是无规则的:实例: string name = this.TextBox1.Text;        string pwd = System.Web.Security.FormsAuthentication.HashPassw

poj2109 Power of Cryptography(数学题)

题目链接:http://poj.org/problem?id=2109 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 num

fabric使用错误,安装cryptography高版本问题

在使用fab的时候报错: pkg_resources.DistributionNotFound: The 'cryptography>=1.1' distribution was not found and is required by paramiko 语义翻译: cryptography 版本过低,需要安装1.1以上版本: 所以执行: pip install cryptography 报类似错误: build/temp.linux-x86_64-2.6/_openssl.c:72268: 错

暑假练习赛 007 B - Weird Cryptography

Weird Cryptography Description standard input/outputStatements Khaled was sitting in the garden under an apple tree, suddenly! , well... you should guess what happened, an apple fell on his head! , so he came up with a new Cryptography method!! The m

ACM: Gym 100935B Weird Cryptography - 简单的字符串处理

Weird Cryptography Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935B Description standard input/output Khaled was sitting in the garden under an apple tree, suddenly! , well... you should guess what happened, an

ZOJ 2671 -Cryptography ( 矩阵乘法 + 线段树 )

ZOJ 2671 - Cryptography ( 矩阵乘法 + 线段树 ) 题意: 给定模数r, 个数n, 询问数m 然后是n个矩阵,每次询问,输出矩阵联乘之后的结果. 分析: 矩阵乘法 + 线段树优化 这里线段树只有询问没有更新操作. PS:顺便仰慕一下watashi.... 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using names

poj2109(Power of Cryptography)

神水. 求n=log(k p)-> k=pow(p,1/n). G++和C++输出不同,编译器原因. 代码: 1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 7 using namespace std; 8 9 int main() 10 { 11 double n,p; 12 whil

POJ2109——Power of Cryptography

Power of Cryptography DescriptionCurrent 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