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 ...
随机推荐
- 图解phpstorm常用快捷键
CTRL+N 查找类 CTRL+SHIFT+N 全局搜索文件 ,优先文件名匹配的文件 CIRL+B 找变量的来源,跳到变量申明处 (CTRL+ 鼠标单击 也可以) CTRL+G 定位行,跳转行 CTR ...
- ACM - ICPC World Finals 2013 F Low Power
原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 有n个机器,每个机器有2个芯片,每个 ...
- [ionic开源项目教程] - 第15讲 ionic用户个人中心登录注册的实现
第15讲 ionic用户个人中心登录注册的实现 这一讲包括登陆.注册.个人中心.个人资料页面的实现. 在一讲的改动有四个地方: 在config.js里配置接口地址 完善个人中心的服务层(service ...
- Swustoj题目征集计划
SWUST OJ题目征集计划 鉴于SWUST OJ长时间没有新题添加,题目数量和类型有限,同时也为加强同学们之间的算法交流,享受互相出题AC的乐趣,提高算法水平,现在启动题目征集计划啦~ 当你遇到 ...
- (六)6.18 cnn 的反向传导算法
本文主要内容是 CNN 的 BP 算法,看此文章前请保证对CNN有初步认识,可参考Neurons Networks convolutional neural network(cnn). 网络表示 CN ...
- 《C和指针》读书笔记 第2章-基本概念
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936485.html
- AsciiDoc
AsciiDoc Text based document generation AsciiDoc Home Page Table of Contents Introduction Overview a ...
- PHP中的cookie
第一次设置后,第二次访问才生效,决绝办法可以用js跳转首页实现刷新. 1.创建/更新cookie setCookie($cookieName,$value,time()+秒数): 例子:创建一个coo ...
- 可接受多个值的文件上传字段HTML5新特性
<input type="file" id="input" multiple="multiple"> 主要是多了个multip ...
- ADO.NET+Access: 1,标准表达式中数据类型不匹配
ylbtech-Error-ADO.NET+Access: 1,标准表达式中数据类型不匹配. 1.A,错误代码返回顶部 1,标准表达式中数据类型不匹配. 1.B,出错原因分析返回顶部 未解决 1. ...