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. POJ 3259 Wormholes【Bellman_ford判断负环】

    题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说 ...

  2. 转:Emmet:快速编写HTML,CSS代码的有力工具

    http://www.cnblogs.com/xiazdong/p/3562179.html  试着用用

  3. find命令下的atime,ctime,mtime

    Linux下的find命令在目录结构中搜索文件,并执行指定的操作.linux下的find命令提供了相当多的查找条件,功能很强大,由于find的功能很强大,所以他的选项也很多,今天我们来细说一下find ...

  4. python处理ajax请求

    先要起一个服务 server.py,可以支持python的cgi脚本. #!coding:utf8 from BaseHTTPServer import HTTPServer from CGIHTTP ...

  5. acdream 1683 村民的怪癖(KMP,经典变形)

    Problem Description 娜娜费劲九牛二虎之力终于把糖果吃完了(说好的吃不完呢?骗人,口亨~),于是,缘溪行,忘路之远近.忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷,娜娜甚异之 ...

  6. (转)每天一个Linux命令(4): mkdir

    http://www.cnblogs.com/peida/archive/2012/10/25/2738271.html linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前 ...

  7. Android 签名详解

    Android 签名详解 AndroidOPhoneAnt设计模式Eclipse  在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...

  8. aspose.words复制插入同一word文档中的某个页面

    选择word模板 Document doc = new Document(Server.MapPath("~\\templet") + "\\" + name. ...

  9. marginCollapse之兄弟关系的DIV

    废话不说,直接上图 基本代码如下: 效果图如下: 给两个div分别加marginBottom和marginTop看一下效果 实际效果如下: 我们可以看出两个div之间的距离并不是50+50,而是只显示 ...

  10. C++宏定义详解

    一.#define的基本用法     #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一定程度上提高程序的运行效率,但学生在学习时往往不能 理解该命令的本质 ...