HDU 1060 Leftmost Digit (数学/大数)

Leftmost Digit

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

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

Recommend

We have carefully selected several similar problems for you:  1018 1071 1573 1066 1004

没有思路,直接百度的

m=n^n;

linag两边同时log10

log10(m)=log10(n^n)=n*log10(n);

所以,m=10^(n*log10(n));

m的最左边一位由n*log10(n)的小数部分决定。m=10^((n*log10(n))的整数部分)+10^((n*log10(n))的小数数部分);10的整数次方肯定是1000……,最左边一位由10^((n*log10(n))的整数部分)决定。

#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int T;cin>>T;
    while(T--)
    {
        double n;
        cin>>n;
        double m=n*log10(n);
        double p=m-(long long)m;
        double q=pow(10,p);
        cout<<(long long)q<<endl;
    }

    return 0;
}

以后遇到大数,可以考虑log,考虑转换成指数形式,

另外大数数学的题,不要用int,用 long long,第一次就是用int,所以wa了一次。

时间: 2024-08-23 05:38:23

HDU 1060 Leftmost Digit (数学/大数)的相关文章

HDU 1060 [Leftmost Digit]数学

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060 题目大意:求N^N的最高位数字. 关键思想:换底公式使结果变为10^(N*log10(N)),再除以10^(digits-1)就OK了. 代码如下: //运用换底公式避免幂运算,取整处理. #include <iostream> #include <cmath> using namespace std; int main(){ int T; double N; cin>&g

HDU 1060 Leftmost Digit (数论)

Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13680    Accepted Submission(s): 5239 Problem Description Given a positive integer N, you should output the leftmost digit of N^N.

HDU 1060 Leftmost Digit (数学log)

题意:给定一个数n,让你求出n的n次方的第一位数. 析:一看这个n快到int极限了,很明显不能直接做,要转化一下.由于这是指数,我们可以把指数拿下来. 也就是取对数,设ans = n ^ n,两边取以10为底对数 lg(ans) = n * lg(10),然后这个整数部分都是10的多次方, 没什么用,也就是说我们要的小数部分,然后再取指数,就OK了.还要注意要用long long因为可能超int了,第一次忘了,WA了. 代码如下: #include <iostream> #include &l

HDU 1060 Leftmost Digit

http://acm.hdu.edu.cn/showproblem.php?pid=1060 题意: 求N^N的首位数字 解法: 取对数orz 不然肯定溢出 n=10^x*m => lgn=x+lg(m) 注意用long long取整 代码: 0MS  1068K #include <cstdio> #include <cmath> using namespace std; int main() { int t, n; scanf("%d", &t

HDU 1061 [Rightmost Digit] 数学方法

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061 题目大意:求N^N的个位数 关键思想:对1个N来说,乘方时末尾会循环.对所有N来说,结果以20为周期. 代码如下(第一个思想): //cnt为循环节长度 #include <iostream> #include <cmath> using namespace std; int main(){ int T; long long i,N,cnt; cin>>T; whil

&lt;hdu - 1600 - 1601&gt; Leftmost Digit &amp;&amp; Rightmost Digit 数学方法求取大位数单位数字

1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * 10^x;其中x也是未知的: 两边取log10有:lg(n^n) = lg(a * 10^x); 即:n * lg(n)  - x = lg(a); 现在就剩x一个变量了,我们知道x是值n^n的位数-1,a向下取整就是我们要求的数: 所以 按着上面的推导式翻译成代码就可以了(注意:数值的范围和之间的

Leftmost Digit(杭电1060)(求N^N的最高位)

Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13574    Accepted Submission(s): 5216 Problem Description Given a positive integer N, you should output the leftmost digit of N^N.

Leftmost Digit(数学)

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

HDU 1061 Rightmost Digit题解

求一个大数N^N的值的最右边的数字,即最低位数字. 简单二分法求解就可以了. 不过注意会溢出,只要把N % 10之后,就不会溢出了,不用使用long long. #include <stdio.h> int rightMost(int n, int N) { if (n == 0) return 1; int t = rightMost(n / 2, N); t = t * t % 10;; if (n % 2) t *= N; return t % 10; } int main() { in