HDU 1060 Leftmost Digit (数论,快速幂)
InputThe input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
OutputFor each test case, you should output the leftmost digit of N^N.
Sample Input
2
3
4
Sample Output
2
2
Hint
In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2.
In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.
感想:一看到大数求幂就想到了快速幂,之前还想着用字符数组装大数,输出字符数组第一位再转换成数值,后来想想好像不太可行···然后没绷住搜了题解,思路是这样的:
需要用到科学记数法和对数运算的知识。
我们把num*num的值记作:num * num=a * 10^n,其中1<a<10;
那么,通过两边取对数的方法得到num * log10(1.0 * num)=log10(a)+n,这时0<log10(a)<1;
令x=n+log10(a),得到log10(a)=x-n;所以a=10^(x-n);
n为整数部分,log10(a)为小数部分,由x=n+log10(a),可知(int)x=n;
最终a=10^(x-n)=10^(x-(int)x)!
m=n^n(_int64);两边同取对数,得到,log10(m)=n*log10(n);再得到,m=10^(n*log10(n));
然后,对于10的整数次幂,第一位是1,所以,第一位数取决于n*log10(n)的小数部分。
1.求a=n^n的对数取整即位数m;【m=n*log10(n)
2.a除以10的m次方取整即最高位;【pow(n,n)/pow(10,m)
/*关键在于公式,以及在于num*log10(num)得到的结果要用long long转换为整数,而不能用int,因为int已经存不下了。*/ #include "stdio.h"
#include "string.h"
#include "math.h"
int main(int n)
{
int C;
double num;
double a;
double x;
scanf("%d", &C);
while(C--)
{
scanf("%lf", &num);
x = num * log10(num);
a = pow(10, x - (long long)x);
printf("%d\n", (int)a);
} }
HDU 1060 Leftmost Digit (数论,快速幂)的更多相关文章
- HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用)
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1060 Leftmost Digit【log10/求N^N的最高位数字是多少】
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 题解报告:hdu 1061 Rightmost Digit(快速幂取模)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- HDU 1060 Left-most Digit
传送门 Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1060 Leftmost Digit
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1061 Rightmost Digit (快速幂取模)
题意:给定一个数,求n^n的个位数. 析:很简单么,不就是快速幂么,取余10,所以不用说了,如果不会快速幂,这个题肯定是周期的, 找一下就OK了. 代码如下: #include <iostrea ...
- HDU 1060 Leftmost Digit (数学/大数)
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1061 Rightmost Digit( 快速幂水 )
链接:传送门 题意:求 N^N 的个位 思路:快速幂水题 /********************************************************************** ...
- HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc
Best Solver Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Tota ...
随机推荐
- android中常见的命名及其特点详解
Paseal命名法 Paseal命名法特点:String MyName-DelphiInt MyAge每个单词首字母大写 Camel命名法 Camel(驼峰的意思)命名法特点:String myNam ...
- $.ajax()方法参数总结
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和d ...
- Virtualization solutions on Linux systems - KVM and VirtualBox
Introduction Virtualization packages are means for users to run various operating systems without &q ...
- jquery实现通用结构折叠面板效果
效果截图: 说明:可以任意添加多个类似结构样式,点击标题栏图片对应隐藏.显示. jquery代码: 思路一:基本方法 <script src="http://apps.bdimg.co ...
- ES6(ECMAScript2015) 基础知识 浅析
1.块级作用域(let) { let fruit = “apple”; } console.log(fruit) 会报错,因为{ }大括号包含的区域为块级作用域,let在其中申明的变量只能在该块中生效 ...
- 【BZOJ】1708: [Usaco2007 Oct]Money奶牛的硬币
[算法]DP [题解] 如果每个排列算一种,则令f[i]表示凑成面值为i的方案数,容易推出f[i]+=f[i-a[j]]. 现在是每个组合才算一种,令f[i][j]第二维表示只使用前j种面值,f[i] ...
- CF 200 div.1 A
2013-10-11 16:45 Rational Resistance time limit per test 1 second memory limit per test 256 megabyte ...
- mysql导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1
mysql导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1 使用 sho ...
- strcpy函数的实现【转】
转自:http://www.cnblogs.com/chenyg32/p/3739564.html 已知strcpy函数的原型是: char *strcpy(char *dst, const char ...
- Linux 的源码安装工具 CheckInstall
Linux 的源码安装工具 CheckInstall Checkinstall 是一个能从 tar.gz 类的 https://www.ibm.com/developerworks/cn/linux/ ...