标签:des style blog http color java os io
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3515 Accepted Submission(s): 1601
Mean:
略
analyse:
这题需要用到fibonacci数列的公式:F(n)=(1/√5)*{[(1+√5)/2]^n - [(1-√5)/2]^n}
在这个公式中后半部分: - [(1-√5)/2]^n 的误差在0~1之间,但对于n比较大时,他的误差就越小,所以一般情况可以忽略;
公式就变为了:F(n)=(1/√5)*[(1+√5)/2]^n] ;
这题要我们输出前四位,直接用公式是肯定会超的,所以要变形:
由于m^n=10^(n*log10(m));
先将n*log10(m)算出来,取其小数部分,再10的乘方,得整数部分为最m^n第一位数,继续乘10直到为4为数为止
Time complexity:O(n)
Source code:
//Memory Time // 1347K 0MS // by : Snarl_jsb #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack> #include<map> #include<string> #include<climits> #include<cmath> #define MAX 1100 #define LL long long using namespace std; int main() { // freopen("C:\\Users\\ASUS\\Desktop\\cin.txt","r",stdin); // freopen("C:\\Users\\ASUS\\Desktop\\cout.txt","w",stdout); int a[25]; a[0]=0,a[1]=1; for(int i=2;i<=21;i++) a[i]=a[i-1]+a[i-2]; int n; while(~scanf("%d",&n)) { if(n<=20) { printf("%d\n",a[n]); continue; } double x=sqrt(5*1.0); double y=log10(1/x); double z=n*log10(((x+1)/2));double p=y+z; double q=p-(int)p;double xx=pow(double(10),q); for(int i=1;;++i) { xx*=10; if(xx>10000) break; } printf("%d\n",((int)xx)/10); } return 0; }
数论 --- 斐波纳挈数列公式的变形,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/acmer-jsb/p/3925594.html