Given a positive integer N, you should output the leftmost digit of N^N. 

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 (数论,快速幂)的更多相关文章

  1. HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用)

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. HDU 1060 Leftmost Digit【log10/求N^N的最高位数字是多少】

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. 题解报告:hdu 1061 Rightmost Digit(快速幂取模)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  4. HDU 1060 Left-most Digit

    传送门 Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. HDU 1060  Leftmost Digit

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. HDU 1061 Rightmost Digit (快速幂取模)

    题意:给定一个数,求n^n的个位数. 析:很简单么,不就是快速幂么,取余10,所以不用说了,如果不会快速幂,这个题肯定是周期的, 找一下就OK了. 代码如下: #include <iostrea ...

  7. HDU 1060 Leftmost Digit (数学/大数)

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. HDU 1061 Rightmost Digit( 快速幂水 )

    链接:传送门 题意:求 N^N 的个位 思路:快速幂水题 /********************************************************************** ...

  9. HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc

    Best Solver Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Tota ...

随机推荐

  1. C#判断字符串是否为数字字符串

    在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现.[方法一]:使用 try{} catch{} 语句.      我们可以在try语句块中试图将str ...

  2. hdu6121 build a tree(树)

    题解: 可以考虑每一层结点的子树大小 必定满足下面的情况,即 a,a,a,a,a,a,b,c,c,c,c........ 然后每一层依次往上更新,结果是不变的 一共有logn层,所以依次扫上去,统计结 ...

  3. Citrix Netscaler版本管理和选择

    Citrix Netscaler版本管理和选择 来源 http://blog.51cto.com/caojin/1898164 随着Citrix Netscaler的快速发展,有很多人在维护设备时经常 ...

  4. 【C++ 拾遗】Function-like Macros

    Macro expansion is done by the C preprocessor at the beginning of compilation. The C preprocessor is ...

  5. [Leetcode] 3sum-closest 给定值,最为相近的3数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  6. git使用笔记(九)操作原理

    By francis_hao    Nov 27,2016   参考[1]的一张图已经把git的基本原理描述的很清楚了,如下:   下面以实例演示其过程,需要用到两个命令cat-file和ls-fil ...

  7. spring中<bean>中parent标签的使用

    简介:spring 中parent标签是指:某个<bean>的父类.这个类可以覆盖parent的属性, 代码如下: Parent类的代码如下: package com.timo.domai ...

  8. 动态性能视图v$session_longops

    v$session_longops This view displays the status of various operations that run for longer than 6 sec ...

  9. CSS中的@ AT规则

    大家可能在CSS中见到过字符@然后加一些关键字的用法,这种用法就称之为AT规则,在CSS中,种类还是很多的,这里总结列举下. 常规规则 所谓“常规规则”指的是语法类似下面的规则: @[KEYWORD] ...

  10. php 计算两个日期的间隔天数

    使用php内部自带函数实现 1.使用DateTime::diff 实现计算 参考阅读>>PHP DateTime::diff() 上代码: <?php $start = " ...