zoukankan      html  css  js  c++  java
  • P1349 广义斐波那契数列

    题目描述
    
    广义的斐波那契数列是指形如an=p*an-1+q*an-2的数列。今给定数列的两系数p和q,以及数列的最前两项a1和a2,另给出两个整数n和m,试求数列的第n项an除以m的余数。
    
    输入输出格式
    
    输入格式:
    输入包含一行6个整数。依次是p,q,a1,a2,n,m,其中在p,q,a1,a2整数范围内,n和m在长整数范围内。
    
    输出格式:
    输出包含一行一个整数,即an除以m的余数。
    
    输入输出样例
    
    输入样例#11 1 1 1 10 7
    输出样例#16
    说明
    
    数列第10项是55,除以7的余数为6。
    题干

    这道题是一旦非常简单的矩阵加速。

    但我却用了好久才A。

    因为调用函数的时候忘了使用函数名,只写了括号,编译过了就认为没错。。。。。。

    一定要注意!!!!

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<climits>
    #include<string>
    #include<cstdlib>
    #include<ctime>
    #define LL unsigned long long
    #define MOD 1000000007
    using namespace std;
    struct node{
        LL v[3][3];
    }x,ans,b,p,d;
    LL n,m,a1,a2;
    node ch(node x,node y)
    {
        for(int i=1;i<=2;i++)
        for(int j=1;j<=2;j++)
        {
            p.v[i][j]=0;
            for(int k=1;k<=2;k++)
            p.v[i][j]=(p.v[i][j]+x.v[i][k]* y.v[k][j])%m;
        }
        return p;
    }
    void fastlow()
    {
        while(n)
        {
            if( n % 2 ==1)    ans=ch(ans,x);
            x=ch(x,x);n/=2;
        }
    }
    int main()
    {
        scanf("%lld%lld",&x.v[1][1],&x.v[2][1]);x.v[1][2]=1;
        scanf("%lld%lld",&d.v[1][2],&d.v[1][1]);
        scanf("%lld%lld",&n,&m);
        ans=ch(d,x);n-=3;
        fastlow();
        cout<<ans.v[1][1]%m<<endl;    
        return 0;
    }
    代码
  • 相关阅读:
    python中自定义模块导入
    EditText------Android
    Fragment类实现
    Android文件访问
    python中pip使用国内镜像提高安装速度
    esri/geometry包 (arcgis api for js)
    【CSDN 编辑器 MarkDowm 使用技巧】
    for 循环 :从指定下标开始,并指定步长
    【车牌识别】-车牌中字符分割代码详解
    【 Linux 常用命令】
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/7374316.html
Copyright © 2011-2022 走看看