hdu 3509 Buge's Fibonacci Number Problem

点击此处即可传送 hdu 3509

题目大意:F1 = f1, F2 = f2;;
F(n) = a*F(n-1) + b*F(n-2);
S(n) = F1^k + F2^k +….+Fn^k;
求S(n) mod m;
解题思路:
1:首先一个难题就是怎么判断矩阵的维数(矩阵的维数是个变量)
解决方法:开一个比较大的数组,然后再用一个公有变量记一下就行了,具体详见代码;
2:k次方,找规律;
具体上代码吧:

/*
2015 - 8 - 16 晚上
Author: ITAK

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 54;
LL mod, size;//size = 矩阵大小
typedef struct
{
    LL m[maxn][maxn];
} Matrix;

Matrix P;
Matrix I;
//正常快速幂
LL quick_mod(LL a, LL b, LL c)
{
    LL ans = 1;
    if(b == 0)
        return 1;
    while(b)
    {
        if(b & 1)
            ans = (ans*a)%c;
        b >>= 1;
        a = (a*a)%c;
    }
    return ans;
}
//矩阵乘法
Matrix matrix_mul(Matrix a, Matrix b)
{
    int i, j, k;
    Matrix c;
    for(i=0; i<size; i++)
    {
        for(j=0; j<size; j++)
        {
            c.m[i][j] = 0;
            for(k=0; k<size; k++)
            {
                c.m[i][j] += ((a.m[i][k]%mod)*(b.m[k][j]%mod))%mod;
            }
            c.m[i][j] %= mod;
        }
    }
    return c;
}
//矩阵的快速幂
Matrix quick_pow(LL m)
{
    Matrix b=P, ans=I;
    while(m)
    {
        if(m & 1)
            ans = matrix_mul(ans, b);
        m >>= 1;
        b = matrix_mul(b, b);
    }
    return ans;
}
//组合数。。。
LL c[50][50];
int main()
{
    memset(c, 0, sizeof(c));
    for(int i=0; i<50; i++)
    {
        c[i][0] = 1;
        c[i][i] = 1;
    }
    for(int i=1; i<50; i++)
        for(int j=1; j<i; j++)
            c[i][j] = c[i-1][j] + c[i-1][j-1];
    int t;
    Matrix tmp;
    LL f1, f2, a, b, k, n, m ;
    LL sum, ans1, ans2;
    //scanf("%d",&t);
    cin>>t;
    while(t--)
    {
        sum = 0;
        cin>>f1>>f2>>a>>b>>k>>n>>mod;
        //scanf("%lld%lld%lld%lld%lld%lld%lld",&f1,&f2,&a,&b,&k,&n,&mod);
        memset(P.m, 0, sizeof(P.m));
        memset(I.m, 0, sizeof(I.m));
        if(k == 0)
            printf("%lld\n",n%mod);
        else
        {
            if(n == 1)
                cout<<quick_mod(f1, k, mod)<<endl;
            else if(n == 2)
                cout<<(quick_mod(f1,k,mod) + quick_mod(f2,k,mod))%mod<<endl;
            else
            {
                size = k+2;
                //矩阵赋值
                for(int i=0; i<size; i++)
                    I.m[i][i] = 1;
                P.m[0][0] = 1;
                P.m[0][size-1] = 1;
                for(int i=1; i<size-1; i++)
                    P.m[0][i] = 0;
                for(int i=1; i<size; i++)
                    for(int j=size-i,w=0; j<size; j++,w++)
                    {
                        P.m[i][j]=(((c[i-1][w]%mod)*quick_mod(a,w,mod))%mod)*quick_mod(b,i-1-w,mod);
                        P.m[i][j] %= mod;
                    }
                tmp = quick_pow(n-1);
                sum = (sum+(tmp.m[0][0]%mod)*(quick_mod(f1,k,mod)))%mod;
                for(int i=1; i<size; i++)
                {
                    ans1 = (quick_mod(f1,size-1-i,mod)*quick_mod(f2,i-1,mod))%mod;
                    ans2 = (ans1*(tmp.m[0][i]%mod))%mod;
                    sum = (sum+ans2)%mod;
                }
                cout<<sum%mod<<endl;
            }
        }
    }
    return 0;
}
时间: 2024-08-05 17:51:29

hdu 3509 Buge&#39;s Fibonacci Number Problem的相关文章

hdu 1250 Hat&amp;#39;s Fibonacci

点击此处即可传送hdu 1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your

HDOJ/HDU 1250 Hat&amp;#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

hdu 3306 Another kind of Fibonacci

点击此处即可传送hdu 3306 **Another kind of Fibonacci** Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2005 Accepted Submission(s): 787 Problem Description As we all known , the Fibonacci series : F(0) =

求四百万以内Fibonacci(number)数列偶数结果的总和

又对啦...开心~~~~ 只是代码可能不符合PEP标准什么的... Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibo

hdu 5349 MZL&amp;#39;s simple problem

hdu 5349 的传送门 Problem Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : delete the minimum number (if the set is empty now,then ignore it) 3 : query the

hdu 1009 FatMouse&amp;#39; Trade

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 55094    Accepted Submission(s): 18478 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

hdu 5334 MZL&amp;#39;s xor

hdu 5334 的传送门 MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to know the xor of all (Ai+Aj)(1≤i,j≤n) The xor of an array B is defined as B1 xor B2-xor Bn Input Multiple test cases, the first line contains an integer T(no

【HDU 4738 Caocao&amp;#39;s Bridges】BCC 找桥

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一个n个节点m条边的无向图(可能不连通.有重边),每条边有一个权值.判断其连通性,若双连通,输出-1:若非连通,输出0:否则,输出权值最小的桥的权值. 思路:进行双连通域分解,记下连通块的个数和所有桥的情况,对应输出结果即可. 注意对重边的处理.这里我按照上一道题学到的姿势如法炮制:先把所有边按"字典序"排序(u, v, w),这样重边聚集在一起了,然后扫描一遍,发现重边即

hdu 5280 Senior&amp;#39;s Array

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5280 问题描述 某天学姐姐得到了一个数组A ,在这个数组的所有非空区间中,她找出了一个区间和最大的,并把这个区间和定义为这个数组的美丽值. 但是她觉得这个数组不够美,于是决定修理一下这个数组. 学姐姐将会进行一次操作,把原数组中的某个数修改为P (必须修改). 最后她想使得修改后的数组尽可能美丽.请你帮助她计算经过修理后,这个数组的美丽值最大能是多少? #include <iostream> #i