Leftmost Digit

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

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.

 
Author
Ignatius.L
 
题目意思:
要你求N^N的值的第一位数字是什么
比如3^3=27
结果就是2
正常写肯定是不行的,因为n太大了
所以要换一种方法
我的思想:
m=n^n;两边同取对数,得到,log10(m)=n*log10(n);
    再得到,m=10^(n*log10(n));
    然后,对于10的整数次幂,第一位是1,
    所以,第一位数取决于n*log10(n)的小数部分
贴一下大佬的分析对我的思想做个补充(都是一个意思)

1.令M = N^N 
2.两边取对数,log10M = Nlog10N,得到M = 10^(Nlog10N) 
3.令N^(N*log10N) = a(整数部分) + b(小数部分),所以M = 10^(a+b) = 10^a *10^b,由于10的整数次幂的最高位必定是1,所以M的最高位只需考虑10^b 
4.最后对10^b取整,输出取整的这个数就行了。(因为0<=b<1,所以1<=10^b<=10对其取整,那么的到的就是一个个位,也就是所求的数)。

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
/*
m=n^n;两边同取对数,得到,log10(m)=n*log10(n);
再得到,m=10^(n*log10(n));
然后,对于10的整数次幂,第一位是1,
所以,第一位数取决于n*log10(n)的小数部分
*/
int t;
double x,y;
LL result;
cin>>t;
while(t--)
{
int n;
cin>>n;
x=n*log10(n);
y=(LL)x;
x=x-y;
result=(LL)pow(10.0,x);
cout<<result<<endl;
}
return ;
}
 

HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用)的更多相关文章

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

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

  2. HDU 1060 Leftmost Digit (数论,快速幂)

    Given a positive integer N, you should output the leftmost digit of N^N.  InputThe input contains se ...

  3. HDU 1060 Left-most Digit

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

  4. HDU 1060  Leftmost Digit

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

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

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

  6. 题解报告:hdu 1060 Leftmost Digit

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060 问题描述 给定一个正整数N,你应该输出N ^ N的最左边的数字. 输入 输入包含多个测试用例. ...

  7. HDU 1060 Leftmost Digit 基础数论

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060   这道题运用的是数学方法. 假设S=n^n.两边同时取对数,得到lgS=nlgn.即有S=10 ...

  8. HDU 1060 Leftmost Digit

    基本思路:(参考大神和加自己的思考) 考虑到此题需要输入这么大的数a,并且还的求aa,求出来会更大,更多位.当时考虑用大数方法求(数组实现),结果实现不行.看网上大神采用对数法,巧妙避开处理这么大的数 ...

  9. HDU 1060 Leftmost Digit (数学log)

    题意:给定一个数n,让你求出n的n次方的第一位数. 析:一看这个n快到int极限了,很明显不能直接做,要转化一下.由于这是指数,我们可以把指数拿下来. 也就是取对数,设ans = n ^ n,两边取以 ...

随机推荐

  1. 工作采坑札记:4. Hadoop获取InputSplit文件信息

    1. 场景 基于客户的数据处理需求,客户分发诸多小数据文件,文件每行代表一条记录信息,且每个文件以"类型_yyyyMMdd_批次号"命名.由于同一条记录可能存在于多个文件中,且处于 ...

  2. 表单提交前的confirm验证提示

    今天要做一个修改提交前的提示,点击修改按钮进行提示,然后根据confirm的结果来决定是否提交;发现平时很常见的一个功能,自己不会.所以只能去晚上找资料了; 举例如下: <form action ...

  3. [转]Asp.Net MVC EF各版本区别

    本文转自:http://www.cnblogs.com/liangxiaofeng/p/5840754.html 2009年發行ASP.NET MVC 1.0版 2010年發行ASP.NET MVC ...

  4. [转]JFreeChart简介及下载、配置

    JFreeChart简介 JFreeChart是完全基于Java语言的开源项目,因此可以使用在Java开发环境中,包括Java应用程序,或者是Java Web应用都没有任何问题.结合iText项目,可 ...

  5. c# 文件名排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  6. mvc表单如何绑定bool类型的属性或变量

    先来看一组代码: 视图代码: @model MvcTest.Controllers.Test @{ Layout = null; } <!DOCTYPE html> <html> ...

  7. TypeScript 入门笔记

    1.原始数据类型 JavaScript 的类型分为两种:原始数据类型和对象数据类型.原始数据类型包括布尔值.数值.字符串.null.undefined 以及 ES6 中的 Symbol. 前五种数据类 ...

  8. asp: suggest ajax

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. CRM——起步

    一.CRM简介 crm 客户关系管理软件 ( Customer Relationship Management ). 二.CRM起步 1.设计表结构和数据库迁移 from django.db impo ...

  10. yeoman-bower-grunt之间的关系

    npm install -g yo 前置技能 Node and NPM nodeJs就是基于谷歌v8引擎的一个javascript环境,使js不仅可以运行在浏览器端,也能在服务器端运行. NPM(No ...