题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1178

题目描述

Given a positive integer N, you should output the leftmost digit of $N^N$.

思路

先把 $n^n$ 的长度l提取出来,然后$LF = n^n / 10^l$。

设 $n^n = x * 10^l,则 l = log_{10}(n^n) = nlog_{10}(n)$

则 $LD = n^n / 10^l = 10^{nlog_{10}n} / 10^l = 10^{nlog_{10}n - l}$

AC代码

/*************************************************************************
    > File Name: 1060.cpp
      > Author: Netcan
      > Blog: http://www.netcan.xyz
      > Mail: 1469709759@qq.com
      > Created Time: Mon 11 May 2015 08:28:54 PM CST
 ************************************************************************/

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <sstream>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <stack>
#include <set>
#include <numeric>
#include <utility>
#include <cstring>
using namespace std;

int main()
{
//    设 n^n = x * 10^l,则 l = log10(n^n) = nlog10(n)
//    则 LD = n^n / 10^l = 10^(nlog10n) / 10^l = 10^(nlog10n - l);
    int T;
    int N;
    cin >> T;
    while(T--) {
        cin >> N;
        long long l = N*log10(N);
        cout << (int)pow(10, N*log10(N) - l) << endl;
    }

    return 0;
}