zoukankan      html  css  js  c++  java
  • HDU 1568 double 快速幂

    Fibonacci

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4844    Accepted Submission(s): 2245


    Problem Description
    2007年到来了。经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列
    (f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i>=2))的值全部给背了下来。
    接下来,CodeStar决定要考考他,于是每问他一个数字,他就要把答案说出来,不过有的数字太长了。所以规定超过4位的只要说出前4位就可以了,可是CodeStar自己又记不住。于是他决定编写一个程序来测验zouyu说的是否正确。
     
    Input
    输入若干数字n(0 <= n <= 100000000),每个数字一行。读到文件尾。
     
    Output
    输出f[n]的前4个数字(若不足4个数字,就全部输出)。
     
    Sample Input
    0
    1
    2
    3
    4
    5
    35
    36
    37
    38
    39
    40
     
    Sample Output
    0
    1
    1
    2
    3
    5
    9227
    1493
    2415
    3908
    6324
    1023
     
    Author
    daringQQ
     
    Source
    题意:求斐波那契数列的前n位数
    题解:斐波那契的通项公式为
     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 #include<bits/stdc++.h>
     8 #include<map>
     9 #include<set>
    10 #include<cmath>
    11 #include<queue>
    12 #include<bitset>
    13 #include<math.h>
    14 #include<vector>
    15 #include<string>
    16 #include<stdio.h>
    17 #include<cstring>
    18 #include<iostream>
    19 #include<algorithm>
    20 #pragma comment(linker, "/STACK:102400000,102400000")
    21 using namespace std;
    22 #define  A first
    23 #define B second
    24 const int mod=1000000007;
    25 const int MOD1=1000000007;
    26 const int MOD2=1000000009;
    27 const double EPS=0.00000001;
    28 typedef __int64 ll;
    29 const ll MOD=1000000007;
    30 const int INF=1000000010;
    31 const ll MAX=1ll<<55;
    32 const double eps=1e-14;
    33 const double inf=~0u>>1;
    34 const double pi=acos(-1.0);
    35 typedef double db;
    36 typedef unsigned int uint;
    37 typedef unsigned long long ull;
    38 int a[25];
    39 double change(double a)
    40 {
    41     while(a-10.0>=eps)
    42     {
    43         a/=10;
    44     }
    45     return a;
    46 }
    47 double pow(double a,int b)
    48 {
    49     double ans=1.0;
    50     while(b)
    51     {
    52         if(b&1){
    53             ans*=a;
    54             ans=change(ans);
    55         }
    56         a*=a;
    57         a=change(a);
    58         b>>=1;
    59     }
    60     return ans;
    61 }
    62 int main()
    63 {
    64     int n;
    65     a[0]=0;
    66     a[1]=1;
    67     for(int i=2;i<=20;i++)
    68         a[i]=a[i-1]+a[i-2];
    69     double base1=(1.0+sqrt(5.0))/2.0,base2=(1.0-sqrt(5.0))/2.0;
    70     while(scanf("%d",&n)!=EOF)
    71     {
    72         if(n<=20)
    73             printf("%d
    ",a[n]);
    74         else
    75         {
    76             double ans=(1.0)/sqrt(5.0)*pow(base1,n);
    77             if(ans-1.0>=eps)
    78                 ans*=1000;
    79             else
    80                 ans*=10000;
    81             printf("%d
    ",(int)ans);
    82         }
    83     }
    84     return 0;
    85 }
     
  • 相关阅读:
    springboot调优
    韩立刚计算机网络笔记-第08章 传输层
    韩立刚计算机网络笔记-第07章 网络层协议
    韩立刚计算机网络笔记-第06章 动态路由和静态路由
    韩立刚计算机网络笔记-第05章 IP地址和子网划分
    韩立刚计算机网络笔记-第04章 数据链路层
    韩立刚计算机网络笔记-第03章 GNS网络模拟器
    韩立刚计算机网络笔记-第02章 物理层
    韩立刚计算机网络笔记-第01章 计算机网络详解
    算法很美 笔记 12.图结构
  • 原文地址:https://www.cnblogs.com/hsd-/p/6017294.html
Copyright © 2011-2022 走看看