【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;
}
┆凉┆暖┆降┆等┆幸┆我┆我┆里┆将┆ ┆可┆有┆谦┆戮┆那┆ ┆大┆始┆ ┆然┆
┆薄┆一┆临┆你┆的┆还┆没┆ ┆来┆ ┆是┆来┆逊┆没┆些┆ ┆雁┆终┆ ┆而┆
┆ ┆暖┆ ┆如┆地┆站┆有┆ ┆也┆ ┆我┆ ┆的┆有┆精┆ ┆也┆没┆ ┆你┆
┆ ┆这┆ ┆试┆方┆在┆逃┆ ┆会┆ ┆在┆ ┆清┆来┆准┆ ┆没┆有┆ ┆没┆
┆ ┆生┆ ┆探┆ ┆最┆避┆ ┆在┆ ┆这┆ ┆晨┆ ┆的┆ ┆有┆来┆ ┆有┆
┆ ┆之┆ ┆般┆ ┆不┆ ┆ ┆这┆ ┆里┆ ┆没┆ ┆杀┆ ┆来┆ ┆ ┆来┆