【ZOJ2277】The Gate to Freedom

BUPT2017 wintertraining(16) #4 E
ZOJ - 2277

题意

输出\(n^n\)的首位的数字。

题解

用科学计数法表示\(n^n=k\cdot 10^b\),那么\(n log_{10} n=log_{10} k+b\),b就是\(n^n\)的位数,因此是\(\lfloor n log_{10} n\rfloor\)
\(k=10^{n log_{n}-b}\)取k的整数部分即可。
我比赛的时候没想到这样做,于是转为小数,再用快速幂暴力做的。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
int main() {
    while(~scanf("%d",&n))
        printf("%d\n",(int)pow(10,n*log10(n)-(int)(n*log10(n))));
    return 0;
}

快速幂暴力

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define ll long long
using namespace std;
double pow(double a,int b){
    double ans=1;
    while(b){
        if(b&1){
            ans=ans*a;
        }
        a=a*a;
        while(a<0.01)a*=10;
        b>>=1;
    }
    return ans;
}
int main() {
    int n;
    while(~scanf("%d",&n)){
        double t=n;
        while(t>=1)t/=10;
        double ans=pow(t,n);
        while(ans<1)ans*=10;
        
        printf("%d\n",(int)ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/flipped/p/6372329.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值