HDU 1060 Leftmost Digit(数论)

来源:互联网 发布:js压缩 tomcat 编辑:程序博客网 时间:2024/06/06 03:02

Description
给出一整数n,输出n^n的最高位
Input
第一行为用例组数T,每组用例占一行为一整数n
Output
对于每组用例,输出n^n的最高位
Sample Input
2
3
4
Sample Output
2
2
Solution
这道题直接算肯定不行,也不能用快速幂之类的算法,所以只能找公式了,显然n可以表示成此形式这里写图片描述,我们要求的即为x
同时我们有
不妨令这里写图片描述
那么则有
Code

#include<cstdio>#include<iostream>#include<cmath>using namespace std;int main(){    int t;    cin>>t;    while(t--)    {        double n;        cin>>n;        double temp=n*(log(n)/log(10.0))-floor(n*(log(n)/log(10.0)));        int ans=(int)(pow(10.0,temp));        cout<<ans<<endl;    }    return 0;}
0 0
原创粉丝点击