zoukankan      html  css  js  c++  java
  • bestcoder#29 1002 矩阵连乘快速幂解fib数列

    bestcoder#29 1002 矩阵连乘快速幂解fib数列

    GTY's birthday gift

    Accepts: 103
    Submissions: 585
    Time Limit: 2000/1000 MS (Java/Others)
    Memory Limit: 65536/65536 K (Java/Others)
    Problem Description

    FFZ's birthday is coming. GTY wants to give a gift to ZZF. He asked his gay friends what he should give to ZZF. One of them said, 'Nothing is more interesting than a number multiset.' So GTY decided to make a multiset for ZZF. Multiset can contain elements with same values. Because GTY wants to finish the gift as soon as possible, he will use JURUO magic. It allows him to choose two numbers a and b(a,bS), and add a+b to the multiset. GTY can use the magic for k times, and he wants the sum of the multiset is maximum, because the larger the sum is, the happier FFZ will be. You need to help him calculate the maximum sum of the multiset.

    Input

    Multi test cases (about 3) . The first line contains two integers n and k (2n100000,1k1000000000). The second line contains n elements ai (1ai100000)separated by spaces , indicating the multiset S .

    Output

    For each case , print the maximum sum of the multiset (mod 10000007).

    Sample Input
    3 2
    3 6 2
    Sample Output
    35

    题意:给定一个集合,每次从集合中找出两个元素a,b,将a+b加进集合,求重复k次之后元素的最大和
    思路:
    显然每次会从可重集中选择最大的两个进行操作,设这两数为a,b(a>=b),操作之后的数一定是操作后集合中最大的,下一次选取的数一定是a+ba,这就形成了一个类似于斐波那契数列的东西,矩阵乘法快速幂求前n项和即可,转移矩阵如下
    Sn    1 1 1  Sn-1
    Fn  =  0 1 1 * Fn-1
    Fn-1   0 1 0  Fn-2
    推导时先填上前后项,中间的01矩阵直接配即可
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    const int maxn=1000100;
    const int MOD=10000007;
    
    long long n,k;
    long long fib[maxn];
    
    int main()
    {
        while(scanf("%lld%lld",&n,&k)!=EOF){
            long long a,b,sum=0;
            for(int i=0;i<n;i++){
                scanf("%lld",&fib[i]);
                sum+=fib[i];
            }
            sort(fib,fib+n);
            a=fib[n-1],b=fib[n-2];
            long long res[3][3]={{1,0,0},{0,1,0},{0,0,1}};
            long long A[3][3]={{1,1,1},{0,1,1},{0,1,0}};
            while(k){
                long long s[3][3];
                if(k&1){
                    memset(s,0,sizeof(s));
                    for(int i=0;i<3;i++){
                        for(int j=0;j<3;j++){
                            for(int k=0;k<3;k++){
                                s[i][j]+=res[i][k]*A[k][j];
                                s[i][j]%=MOD;
                            }
                        }
                    }
                    memcpy(res,s,sizeof(s));
                }
                memset(s,0,sizeof(s));
                for(int i=0;i<3;i++){
                    for(int j=0;j<3;j++){
                        for(int k=0;k<3;k++){
                            s[i][j]+=A[i][k]*A[k][j];
                            s[i][j]%=MOD;
                        }
                    }
                }
                memcpy(A,s,sizeof(s));
                k>>=1;
            }
            long long ans=res[0][0]*(a+b)+res[0][1]*a+res[0][2]*b;
            ans+=sum-a-b;
            cout<<ans%MOD<<endl;
        }
        return 0;
    }
    View Code



    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    JDBC中的PreparedStatement相比Statement的好处
    说出一些数据库优化方面的经验?
    数据库三范式是什么?
    用jdom解析xml文件时如何解决中文问题?如何解析?
    我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
    MVC的各个部分都有那些技术来实现?如何实现?
    JSP和Servlet有哪些相同点和不同点,他们之间的联系是什么?
    黑盒测试和白盒测试是软件测试的两种基本方法,请分别说明各自的优点和缺点!  
    串行(serial)收集器和吞吐量(throughput)收集器的区别是什么?
    说几个常见的编译时异常类?
  • 原文地址:https://www.cnblogs.com/--560/p/4359574.html
Copyright © 2011-2022 走看看