Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11011 Accepted Submission(s): 4214

Problem Description

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

Input

The 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).

Output

For 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.   一开始用java做,果断超时,不过用java给我的感觉是,几天没用,就感觉有点陌生了,把这个代码记录下来,还是挺不错的:  
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int i;
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for(i=0;i<n;i++){
BigInteger b =cin.nextBigInteger();
int c = b.intValue();
b = b.pow(c);
String e = b.toString();
System.out.println(e.charAt(0));
}
}
}

题解思路,利用公式n=10^x*m=>lgn=x+lg(m);

具体步骤:

1.对M=N^N两边取对数得log10(M)=N*log10(N),即M=10^(N*log10(N))

2.要求M的最高位,则令N*log10(N)=a+b;b是小数(0<=b<1),a是整数。

3.因为10的任何整数次幂首位一定为1,所以,M的首位只和N*log10(N)的小数部分有关,

#include<iostream>
using namespace std;
int main(){
int n,i,result;
long long s;
cin>>n;
while(n--){
cin>>s;
double x1 = s*log10( 1.0*s);
double x2 = x1 - (long long)x1;
result = 0;
result = (int)pow(10.0,x2);
cout<<result<<endl;
}
return 0;
}

所以只用求10^b就可以了。(1<=10^b<10)

4.求出b也很简单,只要用double类型的(N*log10(N))去减去long long类型的(N*log10(N))。

HD1060Leftmost Digit的更多相关文章

  1. [LeetCode] Nth Digit 第N位

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...

  2. [LeetCode] Number of Digit One 数字1的个数

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  3. [Leetcode] Number of Digit Ones

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  4. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  5. kaggle实战记录 =>Digit Recognizer

    date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...

  6. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  7. Last non-zero Digit in N!(阶乘最后非0位)

    Last non-zero Digit in N! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  8. POJ3187Backward Digit Sums[杨辉三角]

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6350   Accepted: 36 ...

  9. Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

随机推荐

  1. Mac下配置idk

    Mac下配置java #以下进入啰嗦模式演示添加jdk7 #下载jdk7的mac版 #官网下载地址http://www.oracle.com/technetwork/java/javase/downl ...

  2. bzoj4197

    这题现场想的思路方向都是对的,但限于现场和实力因素没能A 很显然我们会想到质因数的选取 如果某个质数p被W选了,那G就不能选含有质因子p的数 因此我们不难想到状压质数的选取情况,令f[i][j]为w质 ...

  3. [转载] mysql5.6 删除之前的ibdata1文件后再重新生成,遇到[Warning] Info table is not ready to be used. Table 'mysql.slave_master_info' cannot be opened.问题

    [转载] mysql5.6 删除之前的ibdata1文件后再重新生成,遇到[Warning] Info table is not ready to be used. Table 'mysql.slav ...

  4. Java 图片提取RGB数组 RGBOfCharMaps (整理)

    package demo; /** * Java 图片提取RGB数组 RGBOfCharMaps (整理) * 声明: * 和ImageCombining配合使用的工具,这里是提取图片的R.G.B生成 ...

  5. MIPI DSI 和 D-PHY 初始化序列

    MIPI DSI 和 D-PHY 初始化序列 -- 深圳 南山平山村 曾剑锋 参考文档: i.MX 6Dual/6Quad Multimedia Applications Processor Refe ...

  6. watch 命令实时命令执行监控

    watch 命令   watch -d -n 1 'df; ls -FlAt /path' 在使用这条命令时你需要替换其中的 /path 部分,watch 是实时监控工具,-d 参数会高亮 显示变化的 ...

  7. RequireJS入门(一) 转

    RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...

  8. C# winform 自定义控件

    近来因为项目的问题,开始研究winform自定义控件,这篇主要是将自定义控件的属性在属性编辑器中可编辑,如果你对自定义控件比较了解的,就不用继续往下看了 首先,我创建了一个类UserButton,继承 ...

  9. ubuntu myeclipse 启动时提示 A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be avail ....

    jdk已经安装过但是启动eclipse时提示“A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be avail  ...

  10. CMDB反思4

    CMDB模型设计2 http://blog.vsharing.com/xqscool/A1275233.html 估计大家看到破子的这两篇都有点晕哈,我也有点晕. 两篇对比来看.   第1处,属性部分 ...