ZOJ 2277 (数论)

The Gate to Freedom


Time Limit: 2 Seconds     
Memory Limit: 32768 KB



Background

It is dark at night.

It is silence at night.

It is she in the dark.

It is she in the silence.

Then a light appeared. A huge gate came into our sights, called

The Gate to Freedom

Problem

There‘re some words on the gate: "This gate will lead you to freedom. First, you have to open it. I have a problem for you to solve, if you answer it correctly, the gate will open!"

"Tell me, young boy, what is the leftmost digit of N^N?"

Input

This problem contains multiple test cases.

Each test case contains an integer N (N<=1,000,000,000).

Output

For each test case, output the leftmost digit of N^N.

Sample Input

3

4

Sample Output

2

2

Contest: A Great Beloved and My Gate to Freedom

There is a cx, there is a love_cx.

题意:

给定一个数字n , 问n的n次方的首位是多少.

解题思路:

直接暴力算n的n次方明显是不行的,用C写肯定会爆long long ,想一边暴力计算一边取模的同学可以打表看看,数字小的时候不会有误差,但是,数字大的时候会有1的误差。

而用JAVA的大数来进行计算则会超时。

那么只能从数学方面入手了。

n^n = k;

写成科学计数法后 k = a * 10^b

即  n^n = a * 10^b

两边取对数后

lg(n^n) = lg(a * 10^b)

即 n*lg(n) = lg(a) + b

那么b为多少呢?

因为 10^(n*lg(n)) = n^n

可知 [n*lg(n)] ([n*lg(n)] = n*lg(n) 向下取整)即等于 n^n 的位数  即 b

因此 n^n 的数字部分为 10^(n*lg(n) - [n*lg(n)])

我们只需要取第一位即可

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <sstream>
#define PI acos(-1.0)
#define eps 1e-8
const int  inf =  (1<<30) - 10;
using namespace std;

int main(){
    //freopen("input.txt","r",stdin);
    double n;
    while(cin>>n){
        double ans = ((log10(n))*n);
        ans -= floor(ans);
        ans = floor(pow(10.0,ans) + eps);
        printf("%.6f\n",ans);
    }
    return 0;
}

如有BUG,请大家务必指出,不胜感激~

联系方式:[email protected]

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-30 15:29:46

ZOJ 2277 (数论)的相关文章

zoj 3665 数论 二分 两个参数

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 两个参数的题,处理方法:枚举小的那个参数,然后二分大的参数 想到二分了,然后两个参数就不会了  然后暴力了下,但是其实K可以很大 所以时间不够 自己写的二分枚举+快速幂程序WA了很久....没明白哪里错了  参考了别人的... //#pragma comment(linker, "/STACK:102400000,102400000") #include &l

zoj 3665 数论 二分法 两个参数

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 标题两个参数,途径:小参数的枚举,然后二分法大参数 想想两个点.以后就不会了两个参数  然后在暴力,实上K能够非常大 所以时间不够 自己写的二分枚举+高速幂程序WA了非常久. .. .没明确哪里错了  參考了别人的... //#pragma comment(linker, "/STACK:102400000,102400000") #include <c

ZOJ 1136 Multiple(BFS + 数论 同余剪枝 搜索数字的倍数 )

ZOJ Problem Set - 1136 Multiple Time Limit: 10 Seconds Memory Limit: 32768 KB a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive mult

ZOJ 3785 What day is that day? 数论

LINK: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3785 题意:求 \(1^1+2^2+3^3+…+n^n\) 加1模7的值 思路:其实这题是找规律题...说数论是不甘心,这题卡了好久,借助python打表发现 指数每6循环 n值每7循环 6*7=42 但每42个数后它们的排列就会发生改变,共改变7次,所以总循环周期为42*7=294,预处理记录一下就好. /** @Date : 2017-03-23-23.0

Happy Equation ZOJ - 4123 (数论)

题目链接:ZOJ - 4123  题目大意:给你a和p,然后问你[1,2^p]中满足那个等式的值有多少个. 具体思路: 具体的证明:https://blog.csdn.net/v5zsq/article/details/79325038 打表发现当a为奇数的时候答案为1.当a为偶数的时候,x也必须为偶数. 然后我们将a分解为2*t.然后a^x就等于 (2^x)*(t^x).当x大于p的时候,此时取模后一定为0.对于所以在x属于[0,p]这段区间暴力算. 然后我们将b分解为(2^k)*t.然后b^

BZOJ 2277 Poi2011 Strongbox 数论

题目大意:给定n和k个整数,求mod n加法下的群G的一个子群G',满足a[1]~a[k-1]都不在群中而a[k]在群中 首先易证G'一定是一个循环群 证明:显然若a在群中则a的逆元在群中 那么我们就有了减法运算 由群的封闭性可得若a和b都在群中则gcd(a,b)一定在群中 不妨设g为G'中所有元素的gcd 那么群G''={0,g,2g,...}一定是G'的一个子群 由于G'-G''中的所有元素均不是g的倍数,故G'∩(G'-G'')为空 G'=G'' 由此可得G'是以g为最小生成元的一个循环子

zoj 3621 Factorial Problem in Base K 数论 s!后的0个数

Factorial Problem in Base K Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3621 Description How many zeros are there in the end of s! if both s and s! are written in base k which is not nece

ZOJ 2283 Challenge of Wisdom 数论,Dilworth Theorem,求最长反链 难度:2

Challenge of Wisdom Time Limit: 2 Seconds      Memory Limit: 32768 KB Background "Then, I want to know whether you're a wise boy!" Problem "I have a great deal of lands. They're divided into N*M grids (N, M <= 1,000,000,000). When you ar

[zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)

Power of Fibonacci Time Limit: 5 Seconds      Memory Limit: 65536 KB In mathematics, Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers of the following integer sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, .