HDU3117 Fibonacci Numbers(矩阵快速幂)

传送门


题目大意:求斐波那契数列f(n),如果位数的个数大于8那么要输出前4四位和后四位,没有到8位的时候直接输出。


解题思路:当n=40时f(n)的结果位数就超过8位了,所以n<40可以直接求解;

                  当n>=40时,后四位可以用矩阵快速幂求解,对1000取模即可。

                  前四位的求法参考了别人的博客,很详细:

                  http://blog.sina.com.cn/s/blog_9bf748f301019q3t.html


/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(a),_ed=(b);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
#define   mem(x,v)      memset(x,v,sizeof(x))
typedef pair pii;
typedef pair pll;
typedef vector vi;
typedef vector vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}

const int N = 2;
LL mod;

struct Mat
{
    LL mat[N][N];
};

Mat operator * (Mat a,Mat b)
{
    Mat c;
    memset(c.mat,0,sizeof c.mat);
    for(int k=0;k=mod)c.mat[i][j] %= mod;
            }
        }
    }
    return c;
}

Mat operator ^(Mat a,LL k)
{
    Mat c;
    memset(c.mat,0,sizeof c.mat);
    for(int i=0;i>= 1;
    }
    return c;
}


int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	//ios::sync_with_stdio(0);
	//cin.tie(0);
	LL n;
	while(~scanf("%I64d",&n))
    {

        Mat a,res;
        a.mat[0][0] = 1 ; a.mat[0][1] = 1;
        a.mat[1][0] = 1 ; a.mat[1][1] = 0;
        mod = 1e9;
        if(n<=39)
        {
            res = a^n;
            printf("%I64d\n",res.mat[0][1]);
        }
        else
        {
             double tmp;
             double s = (sqrt(5.0)+1.0)/2.0;
             tmp = -0.5*log(5.0)/log(10.0)+((double)n)*log(s)/log(10.0);
             tmp -= floor(tmp);
             tmp = pow(10.0,tmp);
             while(tmp < 1000)
                 tmp *= 10;
             mod = 1e4;
             res = a^n;
             printf("%4d...%04d\n",(int)tmp , (int)res.mat[0][1]);
        }
    }
	return 0;
}


你可能感兴趣的:(~~~~~acm~~~~~,矩阵快速幂)