洛谷 P2759 奇怪的函數
阿新 • • 發佈:2017-09-12
函數 正整數 algorithm 一個 奇怪 思路 using 描述 pre
P2759 奇怪的函數
題目描述
使得 x^x 達到或超過 n 位數字的最小正整數 x 是多少?
輸入輸出格式
輸入格式:
一個正整數 n
輸出格式:
使得 x^x 達到 n 位數字的最小正整數 x
輸入輸出樣例
輸入樣例#1:11
輸出樣例#1:10
說明
n<=2000000000
思路:根據換底公式 可以推得,當x*log10(x)==n-1時x^x恰好為n位數,所以二分查找即可,在特判一下為1的情況。
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,mid; int main(){ //freopen("Lemon_Soda.in","r",stdin); //freopen("Lemon_Soda.out","w",stdout); scanf("%d",&n); int l=0,r=1000000000,prel,prer; if(n==1){ cout<<1;return 0; } while(l<r){ prel=l;prer=r; int mid=(l+r)/2; if(mid*log10(mid)<n-1) l=mid; else if(mid*log10(mid)>n-1) r=mid; if(l==prel&&r==prer) break; } cout<<prer; }
洛谷 P2759 奇怪的函數