HD1060Leftmost Digit
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的更多相关文章
- [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 ...
- [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 ...
- [Leetcode] Number of Digit Ones
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 【Codeforces715C&716E】Digit Tree 数学 + 点分治
C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...
- kaggle实战记录 =>Digit Recognizer
date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...
- [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_ ...
- 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 ...
- POJ3187Backward Digit Sums[杨辉三角]
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6350 Accepted: 36 ...
- Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
随机推荐
- Python3 学习第五弹:类与面向对象
对于面向对象总是要提到,万物皆对象.好似博大精深的感觉. 接下来一起看看python的面向对象的例子 创建一个对象 class Person: type = 'person' def __init__ ...
- Scrum Agile
Scrum Agile 迭代式增量软件开发,敏捷开发,源于丰田汽车的制造流程. HMC测试流程: 1.hmc改配置 2.上ui验证 3.还原hmm(有的需要,有的不需要) 4.addReferal,在 ...
- UVa 10161 Ant on a Chessboard
一道数学水题,找找规律. 首先要判断给的数在第几层,比如说在第n层.然后判断(n * n - n + 1)(其坐标也就是(n,n)) 之间的关系. 还要注意n的奇偶. Problem A.Ant o ...
- SQL语句方法语法总结(三)
1.时间相关的操作 月份.星期.日期.时间格式转换.第几周 ,'2014-4-1') as '时间间隔', --在所给时间上加上时间间隔,转换成DATETIME DATEDIFF(DAY,'2014- ...
- UVALive 3415 Guardian of Decency(二分图的最大独立集)
题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...
- 【英语】Bingo口语笔记(29) - Run系列
- php 二维数组的排序
写这是之前一直二维数组排名困扰.自己写的好复杂. 正题: array_mutisort 官方帮助文档 <?php// 取得列的列表foreach ($data as $key => $ro ...
- Maven与Ant的区别
相同点: Ant和Maven都是基于Java的构建(build)工具. 理论上来说,有些类似于(Unix)C中的make ,但没有make的缺陷.Ant是软件构建工具,Maven的定位是软件项目管理和 ...
- 【转】itunes connect 如何修改主要语言
原文网址:http://blog.csdn.net/yuedong56/article/details/50662181 刚开始提交app,没有做国际化(本地化),提交的app只有简体中文一种语言,第 ...
- Hadoop2.6.0完全分布式安装
本文地址:http://www.cnblogs.com/myresearch/p/hadoop-full-distributed-operation.html,转载请注明源地址. 我这边是使用了两台主 ...