zoukankan      html  css  js  c++  java
  • 1070 普通递归关系

    1070 普通递归关系

     

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 大师 Master
     
     
    题目描述 Description

    考虑以下定义在非负整数n上的递归关系

    f(n) = f0 (if n = 0)

           = f1 (if n = 1)

           = a*f(n-1)+b*f(n-2)  otherwise

    其中a,b是满足以下两个条件的常数:

    (1) a2+4b>0

    (2) |a-sqrt(a2+4b)| <= 2   // sqrt是根号的意思

    给定f0,f1, a, b和n,请你写一个程序计算fn,可以假定fn是绝对值不超过109的整数(四舍五入)。

    输入描述 Input Description

    输入文件一行依次给出5个数,f0, f1, a, b和n, f0,f1是绝对值不超过109,n是非负整数,不超过109。另外,a、b是满足上述条件的实数,且|a|,|b|<=106。

    输出描述 Output Description

    输出f(n)

    样例输入 Sample Input

    【样例输入1】

    0 1 1 1 20

    【样例输入2】

    0 1 -1 0 1000000000

    【样例输入3】

    -1 1 4 -3 18

    样例输出 Sample Output

    【样例输出1】

    6765

    【样例输出2】

    -1

    【样例输出3】

    387420487

    数据范围及提示 Data Size & Hint

    见输入描述

    分类标签 Tags 

    //矩阵乘法大水题 
    #include<cstdio>
    #include<iostream>
    using namespace std;
    typedef double real;
    typedef long long ll;
    ll n;
    struct matrix{real s[2][2];}A,F;
    matrix operator *(const matrix &a,const matrix &b){
        matrix c;
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                c.s[i][j]=0;
                for(int k=0;k<2;k++){
                    c.s[i][j]+=a.s[i][k]*b.s[k][j];
                }
            }
        }
        return c;
    }
    matrix fpow(matrix a,ll p){
        matrix ans;
        for(int i=0;i<2;i++)for(int j=0;j<2;j++) ans.s[i][j]=(i==j);
        for(;p;p>>=1,a=a*a) if(p&1) ans=ans*a;
        return ans;
    }
    int main(){
        cin>>F.s[1][0]>>F.s[0][0]>>A.s[0][0]>>A.s[0][1]>>n;
        A.s[1][0]=1;
        if(F.s[1][0]==0.0&&F.s[0][0]==0.0){puts("0");return 0;}
        if(n>1) F=fpow(A,n-1)*F;
        printf("%d",(int)F.s[0][0]);
        return 0;
    }
  • 相关阅读:
    知名网站的技术发展历程
    自己的代码仓库源码
    重构着讲解设计原则与模式——从DIP中“倒置”的含义说接口的正确使用
    jQuery Colorbox插件
    测试是否支持DOMAttrModified
    python(贪吃蛇)
    界面简洁,无广告,无插件,无网络却功能强播放器
    Asp.Net MVC 4 Web API
    理解MVC底层运行机制
    BS单点登陆(SSO)实现代码展示
  • 原文地址:https://www.cnblogs.com/shenben/p/6612364.html
Copyright © 2011-2022 走看看